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);

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);

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.