Use cookies and JS to calculate the number of visits

Source: Internet
Author: User

Global $ count;

$ Count = 1;

If (! Isset ($ _ COOKIE ["visittime"]) {setcookie ("visittime", date ("y-m-d H: I: s "));

Setcookie ("visitcount", 1); echo "welcome to visit your website for the first time! ";

}

Else {

Setcookie ("visittime", date ("y-m-d, H: I: s"), time () + 60 ); $ count = $ _ COOKIE [visitcount] + 1;

Setcookie ("visitcount", $ count); echo "the last time you visited the Website:". $ _ COOKIE [visittime];

Echo "<br> ";

}
Echo "Your Number". $ _ COOKIE [visitcount]. "The Website access time is:". date ("y-m-d H: I: s ");

?>

The following is a simple js Code for calculating the number of accesses.

Script description:
Step 1: Add the following code to the <body> Area

<Script language = "JavaScript">
<! -- Begin
Function getCookieVal (offset ){
Var endstr = document. cookie. indexOf (";", offset );
If (endstr =-1)
Endstr = document. cookie. length;
Return unescape (document. cookie. substring (offset, endstr ));
}
Function GetCookie (name ){
Var arg = name + "= ";
Var alen = arg. length;
Var clen = document. cookie. length;
Var I = 0;
While (I <clen ){
Var j = I + alen;
If (document. cookie. substring (I, j) = arg)
Return getCookieVal (j );
I = document. cookie. indexOf ("", I) + 1;
If (I = 0)
Break;
}
Return null;
}
Function SetCookie (name, value ){
Var argv = SetCookie. arguments;
Var argc = SetCookie. arguments. length;
Var expires = (2 <argc )? Argv [2]: null;
Var path = (3 <argc )? Argv [3]: null;
Var domain = (4 <argc )? Argv [4]: null;
Var secure = (5 <argc )? Argv [5]: false;
Document. cookie = name + "=" + escape (value) +
(Expires = null )? "": ("; Expires =" + expires. toGMTString () +
(Path = null )? "": ("; Path =" + path) +
(Domain = null )? "": ("; Domain =" + domain) +
(Secure = true )? "; Secure ":"");
}
Function DisplayInfo (){
Var expdate = new Date ();
Var visit;
Expdate. setTime (expdate. getTime () + (24x60*60*1000*365 ));
If (! (Visit = GetCookie ("visit ")))
Visit = 0;
Visit ++;
SetCookie ("visit", visit, expdate, "/", null, false );
Var message;
If (visit = 1)
Message = "Welcome to my page! ";
If (visit = 2)
Message = "I see you came back! ";
If (visit = 3)
Message = "Oh, its you again! ";
If (visit = 4)
Message = "You must be curious! ";
If (visit = 5)
Message = "Youre practically a regular! ";
If (visit = 6)
Message = "You need a holobby! ";
If (visit = 7)
Message = "Nothing better to do? ";
If (visit = 8)
Message = "Dont you ever sleep? ";
If (visit = 9)
Message = "Get a life !!! ";
If (visit> = 10)
Message = "Rent is due on the 1st of the month! ";
Alert ("n" + "your browser has accessed this page" + visit + "times" + "n" + "n" + message );
}
Function ResetCounts (){
Var expdate = new Date ();
Expdate. setTime (expdate. getTime () + (24x60*60*1000*365 ));
Visit = 0;
SetCookie ("visit", visit, expdate, "/", null, false );
History. go (0 );
}
// End -->
</Script>
<FORM>
<CENTER>
<Input name = "update" TYPE = "BUTTON" VALUE = "" OnClick = "history. go (0)">
<Input name = "reset" TYPE = "BUTTON" VALUE = "re-count" OnClick = "ResetCounts ()">
</CENTER>
</FORM>


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.