简体   繁体   English

在链接上单击PHP设置cookie后,单击重定向

[英]On link click redirect after cookie is set with PHP

I have a page which has two links. 我有一个包含两个链接的页面。 When you click one of the link than the PHP should redirect you to the clicked page and when you visit back the cookie should remember your previous choice and it should load the specific page and not the page with the two links. 当您单击其中一个链接时,PHP会将您重定向到所单击的页面,而当您回访cookie时,应记住您先前的选择,并且应加载特定页面,而不是带有两个链接的页面。 I was able to set the cookie but I don't know how to use it when I click on the links. 我能够设置cookie,但是当我单击链接时我不知道如何使用它。

Here is the full code: 这是完整的代码:

<?php
    $cookie_name = 'redirect';
    if(!isset($_COOKIE[$cookie_name])) {
        setcookie($cookie_name , 'redirect-link', time()+(60*60*24*365), "/"); 
    } 

    else {
        print 'Cookie with name ' . $cookie_name. ' value is: ' . $_COOKIE[$cookie_name];
    }
?>

 <html> <head></head> <body> <br/> <a href="https://link1.com">link1</a> <a href="https://link2.com">link2</a> </body> </html> 

main page 主页

if(isset($_COOKIE['thechoosenone'])
{
     $url='<baseusl>/'.($_COOKIE['thechoosenone'];
     header('Location: '.$url);
}

on the linked page eg: 在链接页面上,例如:

if(!isset($_COOKIE['thechoosenone'])) {
//create cockie $_COOKIE['thechoosenone'] and asight the url
}else{
   $url='<baseusl>/'.($_COOKIE['thechoosenone'];
   header('Location: '.$url);
   //stop choosing via manual url browsing 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM