tags:

views:

56

answers:

2

I am creating few pages (including login page) which should only be accessible by a sysadmin.

In login.php after the user's credentials have been verified. I set a session variable like so:

mysql_connect("localhost", $values['uname'], $values['password']) or 
die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("somedb") or die (mysql_error());

session_start();
$_SESSION['level'] = 'admin';
header('Location: /admin/index.php');

And then in the index.php page I am doing the following:

<?php 
if($_SESSION['level'] !== 'admin'){
    header("location:../admin/login.php");
}
?>

But it does not seem to work. Everytime I am being redirected to login.php (even after successfully logging in via login.php).
Is there something wrong with this approach and how I am going about this?

Basically on page other than login.php I need a way to make sure user is logged in...

+3  A: 

Are you doing session_start() before checking or setting $_SESSION['level']?

cletus
I did that now but still the same problem. I'll edit the original post accordingly.
You're not calling session_start() before checking $_SESSION['level'] in the second script. That's what I mean by "checking or setting".
cletus
+1  A: 

You needed to initialize Sessions in every pages you wanted to work with Sessions. Put session_start() at the top of the page (before sending the header).

Ei Maung
did not know that it has to be there on each page. thanks