Hola no se si este te sirva es el que casi en todos los proyectos que realizo lo uso.
Inicio de Sesion
En mi caso manejo un Header por area por ejemplo de la Administracion,
Carpeta: Adminis -> header.php
<?php
//initialize the session
}
// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){ $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']); }
if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){ //to fully log out a visitor we need to clear the session varialbles
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']); unset($_SESSION['MM_UserGroup']); unset($_SESSION['PrevUrl']);
$logoutGoTo = "../Pizzeria_Elis/index.php"; /// SI NO SE LOGUEA VUELVE A CAMBIARIAS
if ($logoutGoTo) {
header("Location: $logoutGoTo"); }
}
?>
<?php
}
$MM_authorizedUsers = "1"; /// aQUI ES DOND SE DA EL PERMISO EN MI CASO 1
$MM_donotCheckaccess = "false";
// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;
// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers); $arrGroups = Explode(",", $strGroups); $isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
$isValid = true;
}
if (($strUsers == "") && false) {
$isValid = true;
}
}
return $isValid;
}
$MM_restrictGoTo = "../Pizzeria_Elis/index.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized
("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { $MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&"; $MM_referrer .= "?" . $QUERY_STRING;
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer); header("Location: ". $MM_restrictGoTo); }
?>
Y para el cierre de Sesion en el mismo Header
<a href="<?php echo $logoutAction ?>" class="enlacenav"></a>
<a href="<?php echo $logoutAction ?>" class="enlacenav">Usuario :
<?=$_SESSION["nom"]?>
(salir)</a>
Esto me basta que tenga en uno ya que llamo el header y dentro de el su menu que es distinto para cada Usuario.
Este es en mi index.php ... Es decir donde pueden loguearse.
index.php
<?php require_once('Connections/apl.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
}
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ?
intval($theValue) : "NULL"; break;
case "double":
$theValue = ($theValue != "") ?
doubleval($theValue) : "NULL"; break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$query_Activos_Login_Usuario = "SELECT * FROM usuarios ORDER BY nivel ASC";
$totalRows_Activos_Login_Usuario = mysql_num_rows($Apl_Login_Usuario); ?>
<?php
// *** Validate request to login to this site.
}
$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) { $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}
/// LOS PERMISOS QUE LLEGO A DAR
if (isset($_POST['usuario'])) { $loginUsername=$_POST['usuario'];
$password=$_POST['password'];
$MM_fldUserAuthorization = "nivel";
$MM_redirectLoginSuccess = "../index.php";
$MM_redirectLoginSuccess2 = "../Cajero/index.php";
$MM_redirectLoginSuccess3 = "../Almacen/index.php";
$MM_redirectLoginSuccess4 = "../Cocina/index.php";
$MM_redirectLoginSuccess5 = "../Repartidor/index.php";
$MM_redirectLoginFailed = "index.php";
$MM_redirecttoReferrer = false;
$LoginRS__query=sprintf("SELECT usuario, clave, nivel FROM usuarios WHERE usuario=%s AND clave=%s", GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
if ($loginFoundUser) {
//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;
if (isset($_SESSION['PrevUrl']) && false) { $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
if($loginStrGroup==1) { header("Location: " . $MM_redirectLoginSuccess ); } if($loginStrGroup==2) { header("Location: " . $MM_redirectLoginSuccess2 ); } if($loginStrGroup==3) { header("Location: " . $MM_redirectLoginSuccess3 ); } if($loginStrGroup==4) { header("Location: " . $MM_redirectLoginSuccess4 ); } if($loginStrGroup==5) { header("Location: " . $MM_redirectLoginSuccess5 ); } }
else {
header("Location: ". $MM_redirectLoginFailed ); }
}
?>
y LO MAS IMPORTANTE EL FORM DE LOGIN:
<form action="<?php echo $loginFormAction; ?>" method="post" id="ingreso">
<table width="100" border="0">
<tr>
<td width="84" class="inserta_tablas1"><strong>Usuario:</strong></td>
<td width="57"><input name="usuario" type="text" id="usuario" size="10" /></td>
</tr>
<tr>
<td class="inserta_tablas1"><strong>Contraseña:</strong></td>
<td><input name="password" type="password" id="password" size="10" /></td>
</tr>
<tr>
<td class="inserta_tablas1"> </td>
<td><span class="inserta_tablas1">
<input type="submit" name="button" id="button" value="Enviar" class="boton" onClick="return Ingreso();"/>
</span></td>
</tr>
</table>
</div>
La tabla que se usa es la siguiente:
--
-- Estructura de tabla para la tabla `usuarios`
--
CREATE TABLE IF NOT EXISTS `usuarios` (
`usuario` CHAR(100) COLLATE utf8_bin NOT NULL COMMENT 'Nombre de usuario',
`clave` CHAR(10) COLLATE utf8_bin NOT NULL COMMENT 'Clave de Usuario',
`nivel` INT(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`usuario`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin ROW_FORMAT=FIXED;
Cualquier duda me avisas...
Saludos