January 5, 201313 yr I'm trying to create a session using a login script which assigns pulls a username and an id from the members database and assigns them to $_SESSION variables like so... (just a snippet). $resource = mysql_query("SELECT id,username FROM members WHERE username='{$_POST['username']}' AND password='".md5($_POST['password'])."'"); $row = mysql_fetch_assoc($resource); if($row['username']) { // Store some data in session $_SESSION['username'] = $row['username']; $_SESSION['id'] = $row['id']; header("Location: update.php"); exit; } else $err[]='Wrong username and/or password!'; It appears to work well and redirect me if the login is successful but when trying to print_r the session on the next page (update.php) it's giving me an empty array as if the session never existed (I've also tried echoing the session_name but that also defaults). I'm not sure why this is happening and to troubleshoot I've both echo'd and print_r'd the session on the login page (logging in and then going back to my login page) and it displays the contents perfectly so why aren't they displaying on the update.php page? I've got session_start() at the very top of my update.php but it doesn't seem to resume it. I've tried not to include too much information not to overwhelm but I can provide more if requested; I'm thinking that this may just be a newbie mistake (I'm still learning PHP).
January 5, 201313 yr Author Yeah, that was my initial diagnosis but it's there on both of them (login.php and update.php) before any HTML output.
January 5, 201313 yr If you'r right about the session_start() and everything should work in your oppinion I advice you to check these: - bad server settings: check your php settings with phpinfo() function and search for "session" maybe you notice something, - you may using old php version, if so the session_register() function could help, check it on php.net.
January 5, 201313 yr try printing the values of $row before redirecting to see if there being set also make sure you sanitise your username in the SQL
January 5, 201313 yr Author If you'r right about the session_start() and everything should work in your oppinion I advice you to check these: - bad server settings: check your php settings with phpinfo() function and search for "session" maybe you notice something, - you may using old php version, if so the session_register() function could help, check it on php.net. I'll post a screenshot of my PHP settings after my food but as for my version, it is 5.4.3. try printing the values of $row before redirecting to see if there being set also make sure you sanitise your username in the SQL I can print the values of $row if inserted at the position shown below but any later in the code and it doesn't work but instead I'm still able to print the values of the session which works fine. So $row seems to unset where $_SESSION is assigned the variables of $row but on the next page, $_SESSION doesn't even appear to exist -.-" ... if(!count($err)){ $_POST['username'] = mysql_real_escape_string($_POST['username']); $_POST['password'] = mysql_real_escape_string($_POST['password']); $resource = mysql_query("SELECT id,username FROM members WHERE username='{$_POST['username']}' AND password='".md5($_POST['password'])."'"); $row = mysql_fetch_assoc($resource); if($row['username']) { // Store some data in session $_SESSION['username'] = $row['username']; $_SESSION['id'] = $row['id']; print_r($row); // <------ IF INSERTED HERE <----- //header("Location: update.php"); } else $err[]='Wrong username and/or password!'; } if($err) // Save the error message in the session $_SESSION['msg']['login-err'] = implode('<br />',$err); } ?> Edited January 5, 201313 yr by Alluziion
Create an account or sign in to comment