PHP的setcookie()函数不起作用。怎么了?

时间:2015-01-02 22:14:49

标签: php setcookie

我真的需要一些帮助。我写了这个脚本来跟踪我网站的流量。我 正在使用 Cookie来帮助服务器知道客户当天是否已经访问了一个页面,这使我能够通过两(2)个计数器/类别跟踪访问者:UNIQUE命中和" REFRESH"命中。

我将信息存储在date命名的目录中。例如,位于www.example.com/counter/unique/2015/12/31/about的文件会存储62这样的数字,这意味着我在2015年12月31日的about.php页面中获得了62次独特点击。

每个页面调用脚本counter.php。一切似乎工作正常,但我的两个计数器继续读取相同的数字,就好像每次点击都是唯一的。当我看到两组数字时,我知道出了什么问题,我通过多次刷新索引页面来确认它。这意味着没有设置cookie。 counter.php中的PHP脚本测试cookie以查看它是否已设置。我一直在检查我的浏览器设置中的cookie,它没有显示任何cookie。我多次刷新每一页并多次重新检查cookie,但仍然没有运气!

世界上到底发生了什么?!?它以前工作过!

以下显示了每个页面如何调用counter.php脚本:

http://www.example.com/index.php

<?php
    include_once("log/counter.php");
?>
<!doctype html>
<html>
...
</html>

以下是我的名为counter.php的PHP代码:

http://www.example.com/log/counter.php

<?php

    // THIS LINE WAS NOT PART OF MY ORIGINAL SCRIPT
    ob_start();

    // THIS IS THE PAGE THAT IS CALLING THIS SCRIPT,
    // FOR EXAMPLE: ABOUT, INDEX, CONTACT, ETC.
    $page = basename($_SERVER["SCRIPT_FILENAME"], ".php");

    // THE DIRECTORY WHERE THIS SCRIPT IS LOCATED
    // RELEVANT TO THE CALLING PAGE
    $cwd = dirname(__FILE__) . "/counter/";

    date_default_timezone_set('America/Chicago');
        // TIMEZONE FOR HOUSTON, TEXAS. THIS WAY, ALL
        // DATE & TIME INFO IS SET IN THAT TIMEZONE

    $currentYear = date("Y");   // EX: 2015
    $currentMonth = date("m");  // EX: 12
    $currentDay = date("d");    // EX: 31

    // THIS PREPARES THE DIRECTORY FOR TODAY'S DATE
    $today = $currentMonth."/".$currentDay."/".$currentYear;
    $current = $currentYear."/".$currentMonth."/".$currentDay."/";

    $currentHour = date("H");
    $currentMinute = date("i");
    $currentSecond = date("s");

    // THE FOLLOWING IS USED FOR THE COOKIE EXPIRATION PARAM

    $secondsRemaining = 60 - $currentSecond;
    $minutesRemaining = 59 - $currentMinute;
    $hoursRemaining = 23 - $currentHour;

    $totalSecondsRemaining = ($hoursRemaining * 60 * 60) + ($minutesRemaining * 60) + $secondsRemaining;

    if ($totalSecondsRemaining<=0) { $totalSecondsRemaining = 86400; }
        // 86400 SECONDS = 24 HOURS / 1 DAY

    $ucFile = $cwd . "unique/" . $current . $page;      // UNIQUE-HITS COUNTER
    $rcFile = $cwd . "refresh/" . $current . $page;     // REFRESH-HITS COUNTER

        // MAKE SURE ALL DIRECTORIES EXIST
        if (!file_exists($cwd."unique/")){
            mkdir($cwd."unique", 0755);
        }
        if (!file_exists($cwd."unique/".$currentYear)){
            mkdir($cwd."unique/".$currentYear, 0755);
        }
        if (!file_exists($cwd."unique/".$currentYear."/".$currentMonth)){
            mkdir($cwd."unique/".$currentYear."/".$currentMonth, 0755);
        }
        if (!file_exists($cwd."unique/".$currentYear."/".$currentMonth."/".$currentDay)){
            mkdir($cwd."unique/".$currentYear."/".$currentMonth."/".$currentDay, 0755);
        }
        if (!file_exists($cwd."refresh/")){
            mkdir($cwd."refresh", 0755);
        }
        if (!file_exists($cwd."refresh/".$currentYear)){
            mkdir($cwd."refresh/".$currentYear, 0755);
        }
        if (!file_exists($cwd."refresh/".$currentYear."/".$currentMonth)){
            mkdir($cwd."refresh/".$currentYear."/".$currentMonth, 0755);
        }
        if (!file_exists($cwd."refresh/".$currentYear."/".$currentMonth."/".$currentDay)){
            mkdir($cwd."refresh/".$currentYear."/".$currentMonth."/".$currentDay, 0755);
        }
        // ALL DIRECTORIES NOW EXIST. SO FAR, NO PROBLEMS!


    // UNIQUE COUNTER...
    if (!isset($_COOKIE[$page])){          // THIS ALWAYS GETS CALLED...
        if (file_exists($ucFile)){         // IF PAGE HAS BEEN COUNTED...
            $file = fopen($ucFile, "r+");  //  1. OPEN THE COUNTER FILE FOR PAGE
            $count = fgets($file);         //  2. GET THE CURRENT COUNT
            fclose($file);                 //  3. CLOSE THE FILE
            $file = fopen($ucFile, "w");   //  4. RE-OPEN FILE AND CLEAR IT
            fputs($file, $count+1);        //  5. REPLACE WITH CURRENT COUNT+1
        }
        if (!file_exists($ucFile)){        // IF THIS IS THE FIRST TIME TODAY...
            $file = fopen($ucFile, "w");   //  1. CREATE A COUNTER FOR THIS PAGE
            fputs($file, "1");             //  2. PUT 1 AS THE CURRENT COUNT
        }

        $works = setcookie($page, "Today is ".$today, $totalSecondsRemaining);
            // SET A COOKIE INDICATING THAT THIS PAGE HAS BEEN
            // VISITED ALREADY BY THIS GUEST.
    }


    // REFRESH COUNTER...
    if (file_exists($rcFile)){             // IF PAGE HAS BEEN COUNTED...
        $file = fopen($rcFile, "r+");      //  (REPEAT STEPS ABOVE, 1-5)
        $count = fgets($file);
        fclose($file);
        $file = fopen($rcFile, "w");
        fputs($file, $count+1);
    }
    if (!file_exists($rcFile)){
        $file = fopen($rcFile, "w");
        fputs($file, "1");
    }

    // AGAIN, NOT PART OF THE ORIGINAL SCRIPT.
    ob_end_flush();

    // ALWAYS RETURNS TRUE...
    echo "<!-- $works -->";
?>

我已经尝试了我能想到的一切,让这个脚本再次运行。据我所知,我从未改变过代码中的任何内容;它只是停止工作一天。

所以,到目前为止我已经检查过的是:

  • 我知道在发送标头之前有 NO 输出。
  • 我知道PHP认为cookie正在设置,因为setcookie()返回true
  • 我知道将来会设置Cookie到期日期。
  • 我知道有效期是大于PHP的整数最大值
    • PHP整数最大值约为32位,我的不超过5个字符
  • 我知道www.example.comexample.com都不会改变脚本的行为

我也尝试了以下内容:

setcookie($page, "Today is ".$today, $totalSecondsRemaining);
setcookie($page, "Today is ".$today, $totalSecondsRemaining, "/");
setcookie($page, "Today is ".$today, $totalSecondsRemaining, "/", "");

setcookie($page, "Today is ".$today, $totalSecondsRemaining, "/", "mywebsite.com");
setcookie($page, "Today is ".$today, $totalSecondsRemaining, "/", ".mywebsite.com");
setcookie($page, "Today is ".$today, $totalSecondsRemaining, "/", "*.mywebsite.com");
setcookie($page, "Today is ".$today, $totalSecondsRemaining, "/", "www.mywebsite.com");

setcookie($page, "Today is ".$today, $totalSecondsRemaining, "/", "mywebsite.com", 0);
setcookie($page, "Today is ".$today, $totalSecondsRemaining, "/", ".mywebsite.com", 0);
setcookie($page, "Today is ".$today, $totalSecondsRemaining, "/", "*.mywebsite.com", 0);
setcookie($page, "Today is ".$today, $totalSecondsRemaining, "/", "www.mywebsite.com", 0);

setcookie($page, "Today is ".$today, $totalSecondsRemaining, "/", "mywebsite.com", false);
setcookie($page, "Today is ".$today, $totalSecondsRemaining, "/", ".mywebsite.com", false);
setcookie($page, "Today is ".$today, $totalSecondsRemaining, "/", "*.mywebsite.com", false);
setcookie($page, "Today is ".$today, $totalSecondsRemaining, "/", "www.mywebsite.com", false);

我将问题隔离到PHP setcookie()函数。任何帮助都非常非常感谢。

P.S。

不可否认,我确信将这些信息存储在MySQL数据库中会更好,但是一旦我解决了这个问题,我就会继续解决这个问题。

1 个答案:

答案 0 :(得分:2)

  

我知道将来会设置cookie到期日期。我知道到期了   date不大于PHP的整数最大大小PHP整数最大值   值大约是32位,我的不超过5个字符

呃,cookie的到期时间不应该是自纪元以来的秒数吗? 5位数的到期日期是1970年1月2日早期,所以我认为您的有效期可能不是五个字符或更少,也可能是将来。

http://php.net/manual/en/function.setcookie.php

  

这是一个Unix时间戳,因此是自纪元以来的秒数。   换句话说,您最有可能使用time()函数设置它   加上你希望它到期之前的秒数

<?php
setcookie("hiworld", "true", time()+300);
?>
Hi, world!


curl -v danf.us/t.php
* Adding handle: conn: 0x7ff05180d000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7ff05180d000) send_pipe: 1, recv_pipe: 0
* About to connect() to danf.us port 80 (#0)
*   Trying 66.191.143.117...
* Connected to danf.us (66.191.143.117) port 80 (#0)
> GET /t.php HTTP/1.1
> User-Agent: curl/7.30.0
> Host: danf.us
> Accept: */*
>
< HTTP/1.1 200 OK
* Server nginx/1.0.10 is not blacklisted
< Server: nginx/1.0.10
< Date: Fri, 02 Jan 2015 22:31:19 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Keep-Alive: timeout=20
< X-Powered-By: PHP/5.3.13-pl0-gentoo
< Set-Cookie: hiworld=true; expires=Fri, 02-Jan-2015 22:36:19 GMT
<
Hi, world!
* Connection #0 to host danf.us left intact