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.
Does anyone know what the problem could be?
The login page that saves the variables:
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) && ($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; } }
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.
EDIT2: I've realised that this only happens when I query the database again.
The thing that's causing problems is (ON home.php):
$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'];
}
If I comment this out, it doesn't seem to happen.
EDIT : I've realised that it has something to do with variable clashes on another script. Should that be happening
$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'];
}