Home  >  Article  >  Backend Development  >  PHP code example using cookies to count visitor login times

PHP code example using cookies to count visitor login times

WBOY
WBOYOriginal
2016-07-25 08:59:03827browse
  1. $_COOKIE["counter"]?($c=$_COOKIE["counter"]+1):($c=1);
  2. setCookie("counter",$c, time()+60);
  3. echo "Welcome to your "."".$c."visit cookie";
  4. / /by bbs.it-home.org
  5. ?>
Copy the code

The following is the code description for this example for your reference.

In the above code, first, the browser requests a resource (this php page) and sends the following HTTP header content to the server:

GET http://localhost/index.php HTTP/1.1 HOST:localhost Accept:*/* Accept-language:zh-cn Accept-Encoding:gzip,deflate User-Agent:Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1;SV1) Connection:Keep-Alive

**The page program (index.php) creates the cookie, and the server transmits the following HTTP header content to the browser:

HTTP/1.1 200 OK Server:Apache/2.2.6 (Win32) PHP/5.2.6 Date:Fri,23 Mar 2009 23:15:55 GMT Connection:Keep-Alive Content-Length:65 Content-Typt:text/html Set-Cookie:VisitorCount=1; expires=Thr,30-Jul-2010 16:00:00 GMT;domain=localhost;path=/ Cache-control:private

GET http://localhost/index.php HTTP/1.1 This will save a cookie file on the client and save the $c variable. When requested again, the data in the cookie will be passed to the server, such as the following HTTP request header:

Accept:*/* Accept-language:zh-cn Pragma:no-cache User-Agent:Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1; SV1) Host:localhost Connection:Keep-Alive Cookie:VisitorCount=1

With this analysis, I hope it will help everyone understand and master the usage of cookies in php.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn