将 cookie 设置为下次重定向到相同的点击 url

Set cookies to redirect to the same clicked url next time

我有以下代码

index.php:

    if(isset($_GET['lang']))
    $translate = new Translator($_GET['lang']);    
    else
    $translate = new Translator('en');

当用户发出这样的 get 请求时

index.php?lang=ru

我希望服务器下次记住他的请求并将他从 index.php 重定向到 index.php?lang=ru

使用:

 if(isset($_GET['lang']))
{
   setcookie("lang", $_GET['lang'], time()+60*60*24*30);
   $lang=$_GET['lang'];
}
elseif (isset($_COOKIE['lang'])) 
{ 
     $lang=$_COOKIE['lang'];
}
else
{
      $lang="en";
}
    $translate = new Translator($lang);
<?php 
 /*
 *  first get lang from url and set cookie
 */
 if (isset ( $_GET ['lang'] )) {  
    $lang = $_GET ['lang'];
    setcookie ( 'lang', "$lang", time () + 3600 );
} 

/*
 * second if lang is not set in url then read it from cookie
*/
else if (isset ( $_COOKIE ['lang'] )) {  
    header ( "location:redirect.php?lang=" . $_COOKIE ['lang'] );
}

/*
 * if cookie or url is not set then take default lang value 'en'
 */

else {
    $lang = 'en';

    header ( "location:redirect.php?lang=" . $lang );
}

?>