-1

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

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

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

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

一些代码示例会很好:)

有任何帮助,谢谢!

<?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');
   ?>
4

1 回答 1

0

笔记:

  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/');      
    }
}

?>
于 2013-03-28T19:11:42.767 回答