• Report Links
    We do not store any files or images on our server. XenPaste only index and link to content provided by other non-affiliated sites. If your copyrighted material has been posted on XenPaste or if hyperlinks to your copyrighted material are returned through our search engine and you want this material removed, you must contact the owners of such sites where the files and images are stored.
  • Home
  • -
  • New Pastes

Unique Page Views

  • Thread starter XMAN
  • Start date Sep 12, 2021
X

XMAN

Well-known member
Joined
Jul 12, 2021
Messages
33,393
Reaction score
248
Points
63
  • Sep 12, 2021
  • #1
Unique Page Views 07-26-2020, 05:56 AM #1
Wassup guys, here is some php code I made for my website to count the amount of unique page views.I tried my best to explain each line of code with a comment above.

Create a new file "count.php" and add the following code: 
PHP Code:

   
/* get the IP address of the user */
   $getra = $_SERVER['REMOTE_ADDR'];

   /* Encrypt the IP address with sha1 in brackets; also get it ready for the next line with \n */ 
   $printip = "<" . sha1($getra) . ">\n";

   /* get the file name where we are printing our encrypted IPs */
   $getfile = "ipfile.txt";


   /* Open that file with read/write access; If it doesn't exist it will create it. */
   $ipfile = fopen($getfile, "a+") or die("Unable to open file!");

   /* count the amount of lines in the ipfile.txt(AKA: counting how many IP's are on the list) */
   $countipfile = count(file($getfile));


   /* read the file we opened */
   $ipget = fread($ipfile, filesize($getfile));

   /* if it contains the ip address already, do nothing. 
   If it doesn't have the IP address on file, write it & +1. */
   if(strpos($ipget, $printip) !== false){ 
   }else{
       fwrite($ipfile, $printip);

       /* since the new visitor wont see his view on the counter until he refreshes his browser; */
       $countipfile++; /* We account for his IP that wasn't initially counted in the list by adding 1 (because it wasn't there) */
       /* Once he refreshes, his IP is already on the list and counted. */
   }

   /* write the amount of unique visits. */
   $theCount = "Unique Website Views: ". $countipfile . "";
   
   ?>

Now, whatever page you want to record the amount of visitors on, add the following code (preferably to the head):
(So if someone views a page with this code in it, their IP will be logged)
PHP Code:
include count.php; ?>

Now to print your unique page counter, use the following code (make sure the line of code above is included somewhere in the page):
PHP Code:
echo $theCount; ?>

I hope you guys enjoy this, if you have any questions comment below and I will try to help you
 
Upvote 0 Downvote
You must log in or register to reply here.
Share:
Facebook Twitter Reddit Pinterest Tumblr WhatsApp Email
  • Home
  • -
  • New Pastes
  • Terms and rules
  • Privacy policy
  • Help
  • Home
AMP generated by AMPXF.com
Menu
Log in

Register

  • Home
    • Go Premium
  • Go Premium / Advertise
  • New Ad Listings
  • What's new
    • New posts
    • New Ad Listings
    • Latest activity
  • Members
    • Registered members
    • Current visitors
X

Privacy & Transparency

We use cookies and similar technologies for the following purposes:

  • Personalized ads and content
  • Content measurement and audience insights

Do you accept cookies and these technologies?

X

Privacy & Transparency

We use cookies and similar technologies for the following purposes:

  • Personalized ads and content
  • Content measurement and audience insights

Do you accept cookies and these technologies?