-1

I stuck this code at the top of my login page which should redirect the user back to the home page if they are already logged in, but it doesn't work, it simply does nothing, even though I know the cookies has been set. To be extra sure the cookie had been set I went into chrome settings and checked all the cookies that my website has stored and sure enough, my cookie was set there. I have no idea why this isn't working, it's probably just a silly little error I made, but I've been programming all night, and I figure a fresh set of eyes might spot something I might have missed.

Code:

<?php
    if(isset($_COOKIE['user'])){
        header("Location: index.php?content=home");
    }
?>

How the cookie is assigned:

<?php
$user = $_POST['user'];
$pass = $_POST['pass'];

$redir = $_GET['redir'];

include('con.php');

$query = mysql_query("SELECT * FROM users WHERE name='{$user}'");
$numrows = mysql_num_rows($query);
$array = mysql_fetch_array($query);

if($user==""||$pass==""){
    header('Location: ../login.php?error=Please fill out EVERYTHING&redir=' . $redir);
}

else if($numrows < 1){
    header('Location: ../login.php?error=User does not exist&redir=' . $redir);
}

else if($pass !=$array['pass']){
    header('Location: ../login.php?error=Invalid user/pass combo&redir=' . $redir);
}

else{
    setcookie("user", $array['ID']);
    header('Location: ../index.php?content=' . $redir);
}

?>

EDIT: Below is what i am getting when i print_r($_COOKIE)

Array
(
    [_okbk] => cd4=true,vi5=0,vi4=1381557924453,vi3=active,vi2=false,vi1=false,cd8=chat,cd6=0,cd5=away,cd3=false,cd2=0,cd1=0,
    [_ok] => 5197-288-10-3215
    [olfsk] => olfsk8855679365806282
    [wcsid] => aqVZBde4DlLmegpG5L3JS16nDXl0aIA9
    [hblid] => oBSPj2E8hdamA4nK5L3JS16nDXrqAaPK
    [_oklv] => 1381563654520,aqVZBde4DlLmegpG5L3JS16nDXl0aIA9
)
Jordan LaPrise
  • 1,088
  • 1
  • 11
  • 20

1 Answers1

2

You are setting cookie for 0 seconds like setcookie("user", $array['ID']);. 3rd parameter in setcookie is $expire and by default it is =0.

setcookie("user", $array['ID'], 3600, '/'); # set cookie for 1 hour and for whole domain
Glavić
  • 42,781
  • 13
  • 77
  • 107
  • I think this may be the problem, but is there any way to make it never expire? – Jordan LaPrise Oct 12 '13 at 08:21
  • No. You can just use a far future date. http://stackoverflow.com/questions/3290424/set-a-cookie-to-never-expire – Glavić Oct 12 '13 at 08:22
  • So there's no way to keep the cookie until I manually delete it? – Jordan LaPrise Oct 12 '13 at 08:24
  • No. You can set it far in the future, or re-set it on intervals. – Glavić Oct 12 '13 at 08:26
  • I'm pretty sure it was the domain setting that worked, because when I looked up cookies in my browser I had cookies stored for mydomain.com and www.mydomain.com – Jordan LaPrise Oct 12 '13 at 08:33
  • The cookie persists till the session expires . I think the problem was due to the path . I think you were setting the cookie in one path and accessing at a different path . However your problem is solved with Glavic's answer which I think addressed path issue . – Uours Oct 12 '13 at 08:33
  • @Uours: I kinda predicted that after he will save timeout issue, that he will come back with question, why must he set cookie for every path/subfolder ;) – Glavić Oct 12 '13 at 08:36