Neste exemplo podemos ver como criar um sistema de autologin com cookies.
---
if (!isset($_SESSION)) {
session_start();
}
//autenticacao automatica
if (isset($_COOKIE['email'])){
$_SESSION['MM_Username'] = $_COOKIE['email'];
$_SESSION['UserGroup'] = $_COOKIE['idacesso'];
$_SESSION['iduser'] = $_COOKIE['iduser'];
header("Location:equipamentos.php");
}
---
//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;
$_SESSION['acesso'] = mysql_result($LoginRS, 0, 'idacesso');
$_SESSION['iduser'] = mysql_result($LoginRS, 0, 'iduser');
//validação automática
if ((isset($_POST['automatico'])) &&
($_POST['automatico']=="s")){
setcookie("email", $loginUsername, time()+31536000);
setcookie("idacesso", $loginStrGroup, time()+31536000);
//setcookie("idacesso", mysql_result($LoginRS, 0, 'idacesso'), time()+31536000);
setcookie("iduser", mysql_result($LoginRS, 0, 'iduser'), time()+31536000);
}
2 comentários:
//initialize the session
if (!isset($_SESSION)) {
session_start();
}
//autenticacao automatica
if(!isset($_REQUEST['flag']) and !isset($_REQUEST['msglogout'])){
if (isset($_COOKIE['email'])){
$flag="ok";
$_SESSION['MM_Username'] = $_COOKIE['email'];
header("Location:index.php?msg=ok&flag=ok");
}
.
.
.
//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;
//validação automática
if ((isset($_POST['automatico'])) &&
($_POST['automatico']=="s")){
setcookie("email", $loginUsername, time()+31536000);
}
.
.
.
$logoutGoTo = "index.php?msglogout=ok";
Para fazer logout:
//initialize the session
if (!isset($_SESSION)) {
session_start();
}
//autenticacao automatica
if (!isset($_REQUEST['flag']) and !isset($_REQUEST ['msglogout'])and !isset($_REQUEST['teste'])){
if (isset ($_COOKIE ['email'])){
$flag="ok";
$_SESSION['MM_Username'] = $_COOKIE['email'];
header("Location:index.php?msg=ok&flag=ok");
}
}
// ** 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']);
setcookie("email", "");
$logoutGoTo = "index.php?msglogout=ok";
if ($logoutGoTo) {
header("Location: $logoutGoTo");
exit;
}
}
?>
Enviar um comentário