Jump to content

Session Variables


MaxyOne

Recommended Posts

Hi ther guys.

I have a little problem with a session variable.

Here's the code:

if($user == $row['username'] && MD5($pass) == $row['password']) {

        $_SESSION['level'] = $row['level'];

        header('Location:http://localhost/index.php');

                            } else { echo 'Wrong username or password.';}

 

After i get redirected by location, the $_SESSION['level'] is empty.

If i comment header and i add an echo for $_SESSION['level'] or $row['level'], the code works fine.

What is wrong with my code?

The session value doesent pass from this page to my index.php.

The session is started in index.php.

Thank you for your time.

Link to comment
Share on other sites

In the code you posted above, is there anything else after that code on the page that could be clearing the session variables, because you need an exit; statement after a header() redirect to prevent the remainder of the code on the page from executing.

 

Are you developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php detected errors will be reported and displayed? You could have an error with the session_start() statement on either page that would prevent the session variable(s) from working.

Link to comment
Share on other sites

index.php include login.php, where i have the html form, the above code is posted in login_process.php.

So you set the $_SESSION -variable in login_process.php file? If thats the case I think you should have also session_start() in that file.

Link to comment
Share on other sites

From what I see, you are starting the session on the page login_process.php and so the session should start on this page before setting a session and not "started" on any other pages.

if($user == $row['username'] && MD5($pass) == $row['password']) {
session_start();
$_SESSION['level'] = $row['level'];
header('Location:http://localhost/index.php');
} else { echo 'Wrong username or password.';}

After the redirect to index.php you should be able to "pick up" that session and use the info, as in

$level=$_SESSION['level'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.