Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with PHP $_SESSION;
    primarykey
    data
    text
    <p>I'm using SESSIONS to store data from a database when a user logs in. However, when I query a database on another page the SESSION variables change without me assigning new values to them.</p> <p>Does anyone know what the problem could be?</p> <p>The login page that saves the variables:</p> <pre><code>enter code here session_start(); if($_POST['login_button']) { require_once('includes/config.inc.php'); $username = $_POST['login_username']; $password = $_POST['login_password']; $hash_pwd = sha1($password); if(($username != NULL) &amp;&amp; ($password != NULL)) { $conn = mysql_connect('localhost', 'admin', 'root') or die(mysql_error()); mysql_select_db('main') or die(mysql_error()); $check_users = "SELECT * ". "FROM users ". "WHERE email = '{$username}' AND password = '{$hash_pwd}' ". "LIMIT 1"; $result = mysql_query($check_users); $counted = mysql_num_rows($result); if($counted != 1) { header("location:login.php?fail"); } else { while ($row = mysql_fetch_array($result)) { $useR_login_un = $row['name']; $useR_login_user_id = $row['user_id']; $useR_login_login_time = $row['login_times']; $useR_login_user_email = $row['email']; $useR_login_course = $row['course']; $useR_login_campus = $row['campus']; $useR_login_login_time = $useR_login_login_time + 1; $useR_login_datenow = date("Y-m-d") . " " . date("H:i:s"); $useR_login_sid = sha1($useR_login_user_id) . rand() . md5($pinch_o_salt); $update_login_times = "UPDATE users ". "SET login_times = '{$useR_login_login_time}', last_login = '{$useR_login_datenow}' ". "WHERE email = '{$useR_login_user_email}' AND user_id = '{$useR_login_user_id}'"; mysql_query($update_login_times); mysql_close($conn); $_SESSION['sid'] = sha1($useR_login_user_id) . $pinch_o_salt; $_SESSION['user_id'] = $useR_login_user_id; $_SESSION['username'] = $useR_login_user_email; $_SESSION['name'] = $useR_login_un; $_SESSION['course'] = $useR_login_course; if($useR_login_login_time == 0) { session_write_close(); header("Location: first_run.php"); exit; } else { session_write_close(); header("Location: home.php"); exit; } } } } else { header("Location: login.php?fail"); exit; } } </code></pre> <p>EDIT: It stores the variables and when i go to another page, the variables appear, but when i refresh, some of them are lost some of them stay the same, and some change to information from another user in the database.</p> <p>EDIT2: I've realised that this only happens when I query the database again.</p> <p>The thing that's causing problems is (ON home.php): </p> <pre><code>$conn = mysql_connect('localhost', 'ggordan_admin', 'valeg0r') or die(mysql_error()); mysql_select_db('ggordan_wmin') or die(mysql_error()); $query = "SELECT * ". "FROM users ". "WHERE course = '".$_SESSION['course']."' ". "LIMIT 3"; $result = mysql_query($query); $numrows = mysql_num_rows($result); echo $numrows; while ($my = mysql_fetch_array($result)) { $id = $row['user_id']; $name = $row['name']; } </code></pre> <p>If I comment this out, it doesn't seem to happen.</p> <p>EDIT : I've realised that it has something to do with variable clashes on another script. Should that be happening</p> <pre><code>$conn = mysql_connect('localhost', 'ggordan_admin', 'valeg0r') or die(mysql_error()); mysql_select_db('ggordan_wmin') or die(mysql_error()); $query = "SELECT * ". "FROM users ". "WHERE course = '".$_SESSION['course']."' ". "LIMIT 3"; $result = mysql_query($query); $numrows = mysql_num_rows($result); echo $numrows; while ($my = mysql_fetch_array($result)) { // if i change $id to $user_id it breaks the login script $id = $row['user_id']; $name = $row['name']; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload