PHP - 具有302代码响应的setCookies问题

I get the following issue with this code : The localhost page caused too many redirections. Status code is 302 Found. After deleting last else, I come succesfull to my site with 200 OK respone but then $user is not defined.

if (empty($_GET['unique_user'])) {
  if (!empty($_COOKIE['unique_user_D_']) && mysqli_num_rows(mysqli_query($connection, "SELECT `id` FROM `users` WHERE `hash`='".myFunction($_COOKIE['unique_user_D_'])."' LIMIT 1"))!=0) {
    header('Location: ./?unique='.$_COOKIE['unique_user_D_']);
    exit();  
  }
  newUser($add);
}
else { 
  if (mysqli_num_rows(mysqli_query($connection, "SELECT `id` FROM `users` WHERE `hash`='".myFunction($_GET['unique_user'])."' LIMIT 1"))!=0) {
    $user=mysqli_fetch_array(mysqli_query($polaczenie, "SELECT * FROM `users` WHERE `hash`='".myFunction($_GET['unique_user'])."' LIMIT 1"));
    $unique=myFunction($_GET['unique_user']);
    setcookie('unique_user_D_',myFunction($_GET['unique']),(time()+60*60*24*365*5),'/');  
  }
  else {
    setcookie('unique_user_D_',false,1),'/');
    header('Location: ./');    
    exit();
  }
}

The reason is that you are calling:

header('Location: ./');    
exit();

Which redirects the current page to the Location: ./ (HTTP Code 302) and I guess the Location is the same path as this code = leading to Too Many Redirects since you are always redirecting to the same page.

Removing the code (header and exit) should solve your problem.