MaxyOne Posted June 7, 2011 Share Posted June 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238660-session-variables/ Share on other sites More sharing options...
gristoi Posted June 7, 2011 Share Posted June 7, 2011 do you have session_start at the top of your index page Quote Link to comment https://forums.phpfreaks.com/topic/238660-session-variables/#findComment-1226445 Share on other sites More sharing options...
MaxyOne Posted June 7, 2011 Author Share Posted June 7, 2011 Yes, i have. Quote Link to comment https://forums.phpfreaks.com/topic/238660-session-variables/#findComment-1226450 Share on other sites More sharing options...
TeNDoLLA Posted June 7, 2011 Share Posted June 7, 2011 How about the code you posted, is it also in index.php after the session_star()t? Quote Link to comment https://forums.phpfreaks.com/topic/238660-session-variables/#findComment-1226453 Share on other sites More sharing options...
PFMaBiSmAd Posted June 7, 2011 Share Posted June 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238660-session-variables/#findComment-1226454 Share on other sites More sharing options...
MaxyOne Posted June 7, 2011 Author Share Posted June 7, 2011 index.php include login.php, where i have the html form, the above code is posted in login_process.php. Ive added exit; after header....no changes. Quote Link to comment https://forums.phpfreaks.com/topic/238660-session-variables/#findComment-1226456 Share on other sites More sharing options...
TeNDoLLA Posted June 7, 2011 Share Posted June 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238660-session-variables/#findComment-1226459 Share on other sites More sharing options...
MaxyOne Posted June 8, 2011 Author Share Posted June 8, 2011 I don't send any session variable to login_process.php The $_SESSION['level'] is extracted from database in login_process.php acording to users level. Quote Link to comment https://forums.phpfreaks.com/topic/238660-session-variables/#findComment-1226826 Share on other sites More sharing options...
Drummin Posted June 8, 2011 Share Posted June 8, 2011 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']; Quote Link to comment https://forums.phpfreaks.com/topic/238660-session-variables/#findComment-1226852 Share on other sites More sharing options...
MaxyOne Posted June 8, 2011 Author Share Posted June 8, 2011 Ive added session_start(); on both pages, index.php, and login_processor.php Now it works fine. Thank you for your support guys. Quote Link to comment https://forums.phpfreaks.com/topic/238660-session-variables/#findComment-1226892 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.