jim.davidson Posted February 16, 2013 Share Posted February 16, 2013 I'm having a problem with session variable retaining updated values. Here's what I'm doing member_login.php <?php require_once('Connections/rclub.php'); ?> <?php session_start(); $_SESSION['MM_Username'] = 'X'; $_SESSION['MM_UserGroup'] = 0; $_SESSION['user_name'] = 'j'; $_SESSION['Logged_in'] = 'no'; $_SESSION['MM_user_level'] = 0; if (isset($_POST['username'])) { $loginUsername=trim($_POST['username']); $password=$_POST['password']; $MM_fldUserAuthorization = "user_level"; $MM_redirectLoginSuccess = "login_passed.php"; $MM_redirectLoginFailed = "login_failed.php"; $MM_redirecttoReferrer = false; mysql_select_db($database_rclub, $rclub); $LoginRS__query=sprintf("SELECT username, password, user_level, user_id, first_name, last_name, member_id FROM users WHERE username=%s AND password=%s", GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); $LoginRS = mysql_query($LoginRS__query, $rclub) or die(mysql_error()); $loginFoundUser = mysql_num_rows($LoginRS); $row_loginFoundUser = mysql_fetch_assoc($LoginRS); // echo $loginFoundUser; if ($loginFoundUser) { $loginStrGroup = mysql_result($LoginRS,0,'user_level'); $loginMemberId = mysql_result($LoginRS,0,'member_id'); //declare two session variables and assign them $_SESSION['MM_Username'] = $loginUsername; $_SESSION['MM_UserGroup'] = $loginStrGroup; $_SESSION['MM_user_level'] = $loginStrGroup; $_SESSION['member_id'] = $loginMemberId; $_SESSION['user_id'] = $row_loginFoundUser['user_id']; $f_name = $row_loginFoundUser['first_name']; $l_name = $row_loginFoundUser['last_name']; $fullname = trim($f_name); $fullname .= ' '; $fullname .= trim($l_name); $_SESSION['user_name'] = $fullname; echo $_SESSION['MM_Username']; echo $_SESSION['MM_UserGroup']; echo $_SESSION['user_name']; echo $_SESSION['user_id']; header("Location: " . $MM_redirectLoginSuccess ); } else { header("Location: ". $MM_redirectLoginFailed ); } } echo results are: $_SESSION['MM_Username'] = jim.davidson $_SESSION['MM_UserGroup'] = 1 $_SESSION['user_name'] = Jim Davidson $_SESSION['user_id'] = 1 So the session variables have been updated. I then go back,comment out the echos (otherwise get headers sent error) enter the same data and now goes to login_passed.php. login_passed.php <?php //initialize the session if (!isset($_SESSION)) { session_start(); } ?> <?php require_once('Connections/rclub.php'); ?> <?php Here I echo the session variables echo $_SESSION['MM_Username']; echo $_SESSION['MM_UserGroup']; echo $_SESSION['user_name']; echo $_SESSION['user_id']; echo results are: $_SESSION['MM_Username'] = X $_SESSION['MM_UserGroup'] = 0 $_SESSION['user_name'] = j $_SESSION['user_id'] = nothing Three of the session variable reverted back to their original state and the fourth one is empty. Anyone have an idea as to why session varibles are notshowing the updated state? Quote Link to comment https://forums.phpfreaks.com/topic/274576-session-variables-no-holding-values/ Share on other sites More sharing options...
Jessica Posted February 16, 2013 Share Posted February 16, 2013 You need session_start on every page. Not within an if. Quote Link to comment https://forums.phpfreaks.com/topic/274576-session-variables-no-holding-values/#findComment-1412843 Share on other sites More sharing options...
jim.davidson Posted February 16, 2013 Author Share Posted February 16, 2013 Made the change replaced if (!isset($_SESSION)) { session_start(); } with sesson_start(); I still get the same results Quote Link to comment https://forums.phpfreaks.com/topic/274576-session-variables-no-holding-values/#findComment-1412845 Share on other sites More sharing options...
denno020 Posted February 17, 2013 Share Posted February 17, 2013 Pretty sure you also need to make sure that session_start(); is the absolute first line in your php script. Plus, you've typed it wrong in your reply.. so maybe you did in your script too? Quote Link to comment https://forums.phpfreaks.com/topic/274576-session-variables-no-holding-values/#findComment-1412853 Share on other sites More sharing options...
jim.davidson Posted February 17, 2013 Author Share Posted February 17, 2013 ok, I made sure session_start() is the first line and is spelled correctly and I still get the same results. Quote Link to comment https://forums.phpfreaks.com/topic/274576-session-variables-no-holding-values/#findComment-1412897 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.