0

我有一个网站,我想向用户显示“您最近的访问”,如果用户第一次访问网站,这将发生在用户不止一次访问网站时,但如果它再次出现,无论他之前搜索过什么显示在一个盒子里。为此,我在 cookie 上工作,但这不能正常工作,我的代码如下:

    <?php
       $pageName = basename($_SERVER['SCRIPT_NAME']);
       $site_name = 'www.xyz.com';
       setcookie('site_name',$site_name,time()+(60*60));
     ?>

      <body style="margin:0 auto; width:400px">
      <br /><h2>Cookie Example</h2>

     <?php
        // print_r($_COOKIE);
        echo "<strong>Cookie has been Created:</strong>&nbsp;&nbsp;&nbsp;".$site_name;
        echo "<br>";
        if(isset($_COOKIE['site_name']))
        {
        echo "<br>";
        echo "<strong>wellcome back</strong>";
        echo "<br>";echo "<br>";echo "<br>";
    }
    else
    {
        echo "<br>";
        echo "<strong>Wellcome</strong>";
    }

    ?>

       <a href="visit1.php">Visit 1</a>&nbsp;&nbsp;|&nbsp;&nbsp;
       <a href="visit2.php">Visit 2</a>&nbsp;&nbsp;|&nbsp;&nbsp;
       <a href="visit3.php">Visit 3</a>

有没有其他方法可以获取访问者的数据。

4

2 回答 2

0
  1. Pages (which appear in the history) should be represented as short as possible. Assign a unique ID to every page.

  2. You should work out a method, which stores a series of page IDs in a simple string. You should write routines which converts an array of pages to that string, and also one which creates an array of that string.

  3. That string should be stored in cookie.

  4. You should write functions, which creates an empty history array (yes, it's trivial), adds an item to the history (it's not, you have handle duplications), and the ones also mentioned: convert history array to string, forth and back.

  5. So, if these routines are ready, you can add "add page to history" function on your each page.

Even such simple things should be designed, else you're lost.

于 2013-10-29T07:21:21.700 回答
-1

您需要处理 IP 地址,这将是您在 dbtable 中的唯一条目,将针对该 IP 地址的搜索存储在另一个 dbtable 中。在每次访问时向他显示结果首先检查用户在您的 dbtable 中是否有条目,如果没有,则第一个条目将进入该表,并且将在链接到该主表的第二个 dbtable 中输入适当的搜索。如果您在 cookie 上工作,如果 cookie 在任何情况下被删除,您可能会丢失用户数据,并且您必须每次都设置 cookie,并且必须维护该特定 cookie 的数据库条目。而是将获取的值存储到 cookie 中。您无需在每次访问页面时都获取表格。

于 2013-10-29T07:07:54.213 回答