Hi I'm learning PHP. Using xampp i'm testing codes, and register_globals is ON. Just wrote a simple code to test session, but it doesn't work i don't know why !? Admin with pass 123 logs in correctly but when i click to go to admin area, it returns me to the login page, because the session doesn't seem to be set:
This is the check.php page:
<?php
session_start();
$u = $_POST['tfUser'];
$p = $_POST['tfPass'];
if (($u == "admin") && ($p == "123"))
{
print ("Okay! Go to Admin area "."<a href='admin.php'>Click Here</a>");
$_SESSION["testSession"] = $mySession;
}
else
{
print ("Nope !");
}
?>
This is the admin.php page:
<?php
session_start();
if (isset($_SESSION["testSession"]))
{
echo "Hello !";
}
else
{
echo '<script language="javascript">
window.location.href="login.php";
</script>';
return;
}
?>
Check that $mySession
is defined and isn't null. isset
will return false if the variable is set to null
use this
<?php
session_start();
$u = $_POST['tfUser'];
$p = $_POST['tfPass'];
if (($u == "admin") && ($p == "123"))
{
print ("Okay! Go to Admin area "."<a href='admin.php'>Click Here</a>");
$_SESSION["testSession"] = $u; // or something else (string, or any other var which must be having some values)
}
else
{
print ("Nope !");
}
You are using $mysession
which is not initialized, so it will be set to null.
Set your error reporting on:
error_reporting(E_ALL);
ini_set('display_errors', '1');
You get a similar error as your mySession var isn't defined.
Notice: Undefined variable: hey in /test.php on line 8
Oh, and as long as you are leaning PHP, get rid of the JavaScript and go with:
header('Location: login.php');
It will have the same result, even if someone has JavaScript turned off.
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.