PHP not creating $_SESSION variable on session_start();
I have 3 pages in my set up here:
login.php: The login page that contains the username and password fields which submits to
checklogin.php: Which checks to see if the credentials are right which redirects to
login_successful.php: Which displays the user's name
login.php starts with
<?php session_start(); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
include 'content.php';
echo $head;
?>
</head>
Which should initialize a global $_SESSION variable right?
checklogin.php has some code which looks like this:
if (mysql_num_rows($result) < 1)
{
/* Access denied开发者_运维百科 */
echo 'Sorry, your username or password was incorrect!';
}
else
{
$_SESSION['username'] = $username;
/* Log user in */
header("location:login_successful.php");
}
login_successful.php looks like this:
<div id="title" style="margin:5px">
<h2>Welcome back <?php echo $_SESSION['username']; ?>!</p>
</div>
Unfortunately when I go through the login procedure and get to login_successful.php, I get this error "Notice: Undefined variable: _SESSION in C:\wamp\www\login_successful.php on line 15" even though I clearly started a session in login.php.
Why is it doing this?
You need to call session_start()
at the top of each of your scripts
You need to remember session_start() in checklogin.php
精彩评论