jainsy Posted March 3, 2010 Share Posted March 3, 2010 Hi guys, Admittedly I didn't really write all of this, I'm just modifying some script Dreamweaver includes for users to register and login etc. Anyway, what I'm trying to do below is to obtain the users id as stored as userID in the MySQL table and set it as a session variable $_SESSION['MM_UserID'] so I can use it later to insert records into a products table and link this back to the relevant user. However, after the user has logged in and I use <?php echo ($_SESSION['MM_UserID']); ?> I get nothing appear at all.. Code is as follows; <?php // *** Validate request to login to this site. if (!isset($_SESSION)) { session_start(); } $loginFormAction = $_SERVER['PHP_SELF']; if (isset($_GET['accesscheck'])) { $_SESSION['PrevUrl'] = $_GET['accesscheck']; } if (isset($_POST['loginEmail'])) { $loginUsername=$_POST['loginEmail']; $password=$_POST['loginPassword']; $MM_fldUserAuthorization = "userID"; $MM_redirectLoginSuccess = "account.php"; $MM_redirectLoginFailed = "sorry.php"; $MM_redirecttoReferrer = false; mysql_select_db($database_conn, $conn); $LoginRS__query=sprintf("SELECT userEmail, userPassword, userID FROM users WHERE userEmail=%s AND userPassword=%s", GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"), GetSQLValueString($userID, "text")); $LoginRS = mysql_query($LoginRS__query, $conn) or die(mysql_error()); $loginFoundUser = mysql_num_rows($LoginRS); if ($loginFoundUser) { $loginStrGroup = mysql_result($LoginRS,0,'userID'); //declare three session variables and assign them $_SESSION['MM_UserID'] = $loginUserID; $_SESSION['MM_Username'] = $loginUsername; $_SESSION['MM_UserGroup'] = $loginStrGroup; if (isset($_SESSION['PrevUrl']) && false) { $MM_redirectLoginSuccess = $_SESSION['PrevUrl']; } header("Location: " . $MM_redirectLoginSuccess ); } else { echo "<font color=#ff0000>Either your username or password were incorrect, please check and try again!</font><br><br>"; } } ?> First time posting, liking the community. Thanks in advance!! Link to comment https://forums.phpfreaks.com/topic/194028-login-script-setting-session-variables-a-small-problem/ Share on other sites More sharing options...
teamatomic Posted March 3, 2010 Share Posted March 3, 2010 At the beginning of each and every page you wish to use session variables you need session_start(); HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/194028-login-script-setting-session-variables-a-small-problem/#findComment-1021041 Share on other sites More sharing options...
jainsy Posted March 4, 2010 Author Share Posted March 4, 2010 yeah i have session_start() at the top of the page. However, If i have <?php echo ($_SESSION['MM_Username']); ?> then the username appears fine but with <?php echo ($_SESSION['MM_UserID']); ?> nothing appears. Which why I was guessing the script above was not passing the UserID into the session but I couldnt work out why, this is really bugging me!!! ?? Link to comment https://forums.phpfreaks.com/topic/194028-login-script-setting-session-variables-a-small-problem/#findComment-1021326 Share on other sites More sharing options...
lAZLf Posted March 4, 2010 Share Posted March 4, 2010 Look through your code carefully. You never set the variable "$loginUserID" which is supposed to be the value of your session variable. Always make sure you set your variables Link to comment https://forums.phpfreaks.com/topic/194028-login-script-setting-session-variables-a-small-problem/#findComment-1021351 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.