I made a login script, and it works perfectly fine, it logs me in and out fine. But when I try to echo the $_SESSION['username'] it appears blank. Below are my script, can anyone help?
checklogin.php
<?
session_start();
require('in/conn.php');
$username = $_POST['username'];
$password = $_POST['password'];
//check if the username is in database
function confirmUsername($username, $password)
{
global $connect;
if(!get_magic_quotes_gpc())
{
$username = addslashes($username);
}
//verify username
$query = "SELECT password FROM users WHERE username='".$_POST['username']."'";
$result = mysql_query($query, $connect);
if(!$result || (mysql_numrows($result) != 1))
{
return 1;
}
//get password
$pword = mysql_fetch_array($result);
$pword['password'] = stripslashes($pword['password']);
$password = stripslashes($password);
//verify password
if($password == $pword['password'])
{
return 0;
} else {
return 2;
}
}
//if they sent the login information, check if they are correct
if(isset($_POST['submit']))
{
if(!$username || !$password)
{
die("You did not fill in the required fields.");
}
//check username and password
$md5pass = md5($password);
$result = confirmUsername($username, $md5pass);
//check errors
if($result == 1)
{
die("That username does not exist in our database.");
}
else if($result == 2)
{
die("The password you entered is incorrect, please try again.");
}
$date=date('F d, Y');
$lastlog="UPDATE users
SET last_login = '$date'
WHERE username = '$username'";
$updatelog=mysql_query($lastlog) or die("Invalid Query: [$lastlog] " . mysql_error());
//now if they are all correct, set their session
$username = stripslashes($username);
session_register('username');
session_register('password');
$_SESSION['username'] = $username;
$_SESSION['password'] = $md5pass;
session_register('login');
$_SESSION['login'] = 1;
echo "<script>window.location='link.php';</script>";
return;
}
?>
The login form is on another page.
So can anyone tell me why is it that the $_SESSION['username'] when being called turn blank?
The other page looked like this wheere I tried to call the session:
<?
session_start();
require('conn.php');
require('checklogin');
if($login == 1){
echo $_SESSION['username'];
} else {
echo "This area is for authorized members only";
?>
Thanks 🙂