I've done a login page in a site for some company and every seemed to be alright until I notice a minor issue, important anyway. The session starts an everything works ok, except that I can't redirect from the login page if the session is already started. I mean if I type the address:
mydomain/myfolder/login.php
It stays there and can't redirect to the main page, because it looks like the session data is not available to this page. Here's the code I'm using:
<?php
$usuarios = simplexml_load_file('__usuarios__mensajes.xml');
$goLogin = false;
$nameOK = false;
$passOK = false;
$messagesPage = "index.php";
if($_SESSION["loggedIn"])$passOK = true;
if((isset($_POST["MM_insert"]) && $_POST["MM_insert"] == "form1")){
$nombre = $_POST["user"];
$password = $_POST["password"];
foreach($usuarios->usuario as $usuario){
$attr = $usuario->attributes();
if($nombre == $attr["nombre"]){
$nameOK = true;
if($password == $attr["password"]){
$goLogin = true;
$passOK = true;
$displayName = (string)$attr["displayName"];
}
break;
}
}
}
if($passOK){
session_start();
$_SESSION["usuario"]= $_POST["user"];
$_SESSION["loggedIn"] = true;
$_SESSION["userDisplayName"]= $displayName;
header(sprintf("Location: %s", $messagesPage));
}
?>
Before you use $_SESSION
, use session_start()
first. So it should be like this:
session_start();
if($_SESSION["loggedIn"])$passOK = true;
you have to check for session validation fil first line after
if(!isset($_SESSION)){ session_start(); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.