Sesquipedalian Posted December 24, 2007 Share Posted December 24, 2007 Hey everyone, I'm trying to get back into PHP, so I'm making a really simple login (not too secure either).. The only problem is, it doesn't seem to remember one of the session variables... index.php: <? session_start(); include ('homepage.php'); ?> homepage.php: <? include ('class.php'); echo '<h1>Login</h1><br />'; if (!$_POST && !$_GET) { if (!isset($_POST['user']) && !isset($_POST['pass'])) { $login->disp_form(); } } else if (isset($_POST['user']) && isset($_POST['pass'])) { $login->check('sesq', md5('sesq'), $_POST['user'], md5($_POST['pass'])); } else if ($_GET['login'] == 'accepted') { include ('links.php'); } ?> class.php: <? class login { function disp_form() { echo '<center><form action="?login=submit" method="POST">' .'<input type="text" name="user"></input><br />' .'<input type="password" name="pass"></input><br />' .'<input type="submit" value="Login" />'; } function check($user, $pass, $form_user, $form_pass) { if ($user == $form_user && $pass == $form_pass) { $_SESSION['login'] = 'yes'; $_SESSION['user'] = $user; echo '<center><a href="?login=accepted">Proceed...</a>'; } else { echo 'Incorrect.'; } } } $login = new login(); ?> links.php: <? echo '<center>Welcome, '.$_SESSION['user']; ?> It displays $_SESSION['user'] as sesq, which its supposed to.. but if I try to make it echo $_SESSION['login'] it displays Object id #1 and not 'yes' like it should. $_SESSION['login'] is what I'm going to determine if they are logged in. I know I could use $_SESSION['user'] to check if they are logged in, but I still want to know why it doesn't seem to maintain the fact that $_SESSION['login'] == 'yes'? Thanks, - Sesq Quote Link to comment https://forums.phpfreaks.com/topic/82992-solved-session-will-only-remember-one-variable/ Share on other sites More sharing options...
papaface Posted December 24, 2007 Share Posted December 24, 2007 Are you putting session_start(); in links.php? session_start(); Must be on every single page you want to register or use sessions in any way. Quote Link to comment https://forums.phpfreaks.com/topic/82992-solved-session-will-only-remember-one-variable/#findComment-422110 Share on other sites More sharing options...
marcus Posted December 24, 2007 Share Posted December 24, 2007 Did you even bother looking at his code layout? Index includes homepage, while homepage includes links. Quote Link to comment https://forums.phpfreaks.com/topic/82992-solved-session-will-only-remember-one-variable/#findComment-422113 Share on other sites More sharing options...
papaface Posted December 24, 2007 Share Posted December 24, 2007 Did you even bother looking at his code layout? Index includes homepage, while homepage includes links. Yeah I did, but didnt notice! Jesus. You obviously make no mistakes. Quote Link to comment https://forums.phpfreaks.com/topic/82992-solved-session-will-only-remember-one-variable/#findComment-422116 Share on other sites More sharing options...
Sesquipedalian Posted December 24, 2007 Author Share Posted December 24, 2007 Are you putting session_start(); in links.php? session_start(); Must be on every single page you want to register or use sessions in any way. I've tried that before, and it doesn't seem to matter. Besides, if that was the problem, why does can it remember what $_SESION['user'] is? I changed it to make it $_SESSION['auth'], and it works. Still curious of why it wouldn't work though with $_SESSION['login']? Quote Link to comment https://forums.phpfreaks.com/topic/82992-solved-session-will-only-remember-one-variable/#findComment-422119 Share on other sites More sharing options...
marcus Posted December 24, 2007 Share Posted December 24, 2007 <?php function session($user, $login = 'yes'){ $_SESSION['user'] = $user; $_SESSION['login'] = $login; } session('marcus'); echo "<pre>\n"; print_r($_SESSION); echo "</pre>\n"; session('billy','no'); echo "<pre>\n"; print_r($_SESSION); echo "</pre>\n"; ?> My results: Array ( [user] => marcus [login] => yes ) Array ( [user] => billy [login] => no ) Quote Link to comment https://forums.phpfreaks.com/topic/82992-solved-session-will-only-remember-one-variable/#findComment-422124 Share on other sites More sharing options...
PFMaBiSmAd Posted December 24, 2007 Share Posted December 24, 2007 If you are having variables overwritten (and when you change the name of one of them it works), it is usually due to register_globals being on. A) Use unique variable and array index names where register_globals are on. B) If register globals are on, after you make sure none of the rest of your code is dependent on them being on, turn register globals OFF. Quote Link to comment https://forums.phpfreaks.com/topic/82992-solved-session-will-only-remember-one-variable/#findComment-422154 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.