Hi, i'm trying to write a login page with the following code :

if (login==ok){
setcookie('User[usr]', $row['uid'], time()+3600);
setcookie('User[pwkey]', $row['PwKey'], time()+3600);
header("Location:home.php");


in other pages the following code :

if (isset($_COOKIE['User'])){
foreach ($_COOKIE['User'] as $name => $value){
if ($name=='usr') $user=$value;
if ($name=='pwkey') $userpass=$value;
}
....
checks if info in cookie are equals with the info in db and shows page or redirect to login.php
}


and a logout page with the following code :

setcookie('User[usr]', $row['uid'], time()-3600);
setcookie('User[pwkey]', $row['PwKey'], time()-3600);
unset($_COOKIE["User"]);
header("Location:login.php");


When i try to write a code in login.php that if cookie exists and the info are equal with the info in db to redirect automatically to home.php i have a loop. The same issue when i try to logout.