I have two scripts, one for processing a login and one for registering a login, they both create a sessiod id and insert it into both a cookie and a database. The DB insert works just fine, however, the cookies are not set:
login:
//...Stuff...
$sql = "SELECT * FROM dm_users WHERE username = '$username' LIMIT 1";
$res = mysql_query($sql);
if(mysql_num_rows($res) == 0) {
header("Location: denied.php?u");
die();
}
while($r = mysql_fetch_assoc($res)){
$brought = $r['password'];
}
if (PassHash::check_password($brought, $password)) {
if($remember == 1) {
$time = time() + (10 * 365 * 24 * 60 * 60);
} else {
$time = 0;
}
$val = uniqid() . time();
setcookie("dmsesid",$val,$time);
$sql = "INSERT INTO dm_sessions (username,sesid,`timestamp`) VALUES ('$username','$val',now())";
$r = mysql_query($sql);
$sql2 = "UPDATE dm_users set lastlogin = NOW() WHERE username = '$username'";
$r2 = mysql_query($sql2);
header('Location: ../characters.php');
} else {
//...More stuff...
Register process works in much the same way, but only with the $time = 0; option.
Any help would be greatly appreciated!
I think you have to set the path for the cookie
setcookie("dmsesid",$val,$time, '/');
From PHP manual:
The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain . If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain . The default value is the current directory that the cookie is being set in.