【问题标题】:Wordpress, site redirect using cookiesWordpress,使用 cookie 进行网站重定向
【发布时间】:2013-03-19 06:41:54
【问题描述】:

我在下面有这个网站,我正在玩一些 Wordpress 和 php cookie。 http://johnnylai.me/lotus

我想要做的是,当用户第一次进来时,我希望他们在首页的两个链接中选择一个(这是一个单独的 WP 安装)。下次同一用户返回该站点时,我不希望他们再看到首页(b 4 cookie 已删除或过期),我希望他们直接访问两个站点之一 - http://johnnylai.me/lotus/virksonhed 或 @ 987654323@ - 取决于第一次点击的内容。

我知道我需要一些 cookie 的东西,但我不确定将文件放在哪里以及如何正确放置。

我正在考虑在 WP 安装中名为 function.php 的文件中的 sulition/php 代码,该文件具有首页(http://johnnylai.me/lotus),但不知道这是否是正确的方法。

一些代码示例会很好:)

如有任何帮助,谢谢!

<?php
    $expire=time()+60*60*24*30;
    setcookie("cat", "/cat1", $expire); 

    // But this is something with categoies in WP, that's not what i need.
?>

还有

<?php
   function has_auth_cookie()
   {
     // See if cookie is set
     if(isset($_COOKIE['lotus'])){
       // Do nothing
       header('Location: johnnylai.me/lotus/???');
     }
       else
     { 
       // Do Something else 
       header('Location: johnnylai.me/lotus/'); 
     }

   }
   add_action('http://johnnylai.me/lotus/????', 'has_auth_cookie');
   ?>

【问题讨论】:

    标签: php wordpress cookies


    【解决方案1】:

    注意事项:

    1. 当您执行 header() 并将它们保留在现场时,您可以使用本地路径

      header('位置:/lotus/');

    2. 因为 $_COOKIE 是一个数组,所以当您检查 $_COOKIE 而不是 isset() 时使用

      if(!empty($_COOKIE['cookiename']))

    3. 如果出于某种原因您希望某人再次看到主页,则需要取消设置 cookie。

      setcookie("cookiename", "", time() - 3600);

    4. 如果人们不接受 cookie,除非您有其他识别方式,否则他们不会被重定向。

    5. 要设置 cookie,您必须先传递它。您无法在设置 cookie 的页面上进行标头重定向,因为标头已经发送。

    PHP 代码:

    <?php
      if(empty($_COOKIE['TestCookie'])){
        if(!empty($_GET['c'])){
          $cVar = $_GET['c'];
          //time to set some cookies
          switch($cVar){
            case '2':
            setcookie("TestCookie", '2');
            echo "Cookie 2 set. Reload the page.";
            break;
    
            default:
             //also covers hacking attempts
                setcookie("TestCookie", '1');
                echo "Cookie 1 set. Reload the page.";  
            }
    
        } else {
        // They're possibly a first time visitor or someone offsite 
    
        echo '<!doctype html>
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Cookie Check</title>
    </head>
    
    <body>
    <a href="?c=1">Set cookie 1</a>
    <a href="?c=2">Set cookie 2</a>
    </body>
    </html>';
    
        }
    
    } else {
        switch($_COOKIE['TestCookie']){
    
        case '2':
        //Send them to location 2
        header('Location: http://www.google.com/');
        break;
    
        default:
        //Send the to location 1
        header('Location: http://www.yahoo.com/');      
        }
    }
    
    ?>
    

    【讨论】:

    • cpattersonv1 感谢您的解释和代码示例。我会看看我能不能让它工作。如果我将php代码放入function.php并放入主题中,它会起作用吗?
    • 这里还有一个专门为 WordPress 添加 cookie 的参考:stackoverflow.com/questions/6183162/…
    • 这取决于您使用的主题。您可以随时尝试并查看它是否损坏,如果损坏则恢复备份。如果它不起作用,您可能需要查看主题以查看之前是否触发了某些内容。
    • 我会将其设置为包含我自己,这样您就不必担心如果更新会丢失代码。
    • 感谢 cpattersonv1。我仍然不确定将每个代码放在哪里,但我会尝试一下。是的,第一部分转到function.php,但其余部分我不确定。感谢您的尝试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多