Jump to content

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
https://forums.phpfreaks.com/topic/238660-session-variables/
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
https://forums.phpfreaks.com/topic/238660-session-variables/#findComment-1226454
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
https://forums.phpfreaks.com/topic/238660-session-variables/#findComment-1226852
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.