Possible Duplicate:
PHP session seemingly not working
I’m currently coding my own CMS for fun but when I use $_SESSION, it doesn’t work. The session isn’t saved…
There’s my code:
<?php
include('header.php');
if (isset($_SESSION['logged_in']))
{
$link = 'profile.php';
$link_name = 'Profile';
}
else
{
$link = 'login.php';
$link_name = 'Login';
}
if (isset($_POST['action']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$password = md5($password);
$user = mysql_query("SELECT * FROM users WHERE `username`='".$username."'");
if (mysql_num_rows($user) == 1)
{
while ($userinfo = mysql_fetch_array($user))
{
if ($userinfo['banned'] != true)
{
$_SESSION['user_id'] = $userinfo['id'];
$_SESSION['username'] = $userinfo['username'];
$_SESSION['logged_in'] = "true";
header('Location: index.php');
}
else
{
header('Location: login.php?error=banned');
}
}
}
else if (mysql_num_rows($user) == 0)
{
header('Location: login.php?error=not-found');
}
}
?>
In the code, I get the user information in my database then I check if the user isn’t banned. If not, I set my $_SESSION[] and I redirect to the home…
You need
session_start()at first.