Visitor Count for Website in PHP

Hello,
Here I am writing a small program in PHP for  checking how much user have visited our website using COOKIES, MYSQL and IP address of the user.
First we have to create cookies for the new user.

This the code we will use for that: (I have make a cookies for 30 days)
setcookie('ipaddress',$_SERVER['REMOTE_ADDR'],time()+(60*60*24*30));

Secondly we have to create a table on database to store the number of Visitors.
I have created a table i.e. usercounter with following attribute:  (Initially Insert value with o and 1 on useripid )

  • useripid   (INT type - AUTO-INCREMENT)
  • lastaddress   (VARCHAR    -   Last visitor IP)
  • userqua   (INT)   (Counter for visitor)  (Initially 0)
  • lastmodified   (TIMESTAMP)   (Last modified time for database)
Now we have to check whether user are coming on website for first time or He has already visited.
if(isset($_COOKIE['ipaddress'])) {
// check is the same ip
if($_COOKIE['ipaddress']==$_SERVER['REMOTE_ADDR']) {  // nothing to do.
// check is the same ip
 } else { // diffrent IP
// update the database

$sqlupdate="UPDATE `userip` SET `lastaddress`='{$_SERVER['REMOTE_ADDR']}',`userqua`=(`userqua`+1),`lastmodified`=now() WHERE  `useripid` =1;";
setcookie('ipaddress',$_SERVER['REMOTE_ADDR'],time()+(60*60*24*30));
$resultupdate = $dbLink->query($sqlupdate);

// update the database 
} // diffrent IP
} else  {  // first time user
// set the cookies
setcookie('ipaddress',$_SERVER['REMOTE_ADDR'],time()+(60*60*24*30));
// update on database

$sqlupdate="UPDATE `userip` SET `lastaddress`='{$_SERVER['REMOTE_ADDR']}',`userqua`=(`userqua`+1),`lastmodified`=now()
WHERE  `useripid` =1"; 
$resultupdate = $dbLink->query($sqlupdate);
// update on database

} // first time user

//  check the counter
$sqlcheck = "SELECT `userqua` FROM  `userip` where `useripid`=1";  // query
$resultcheck=$dbLink->query($sqlcheck); // run the query
if($resultcheck) { if($resultcheck->num_rows == 0) { // no result or zero results
$userquata="Refresh";
} else { $rowcheck = $resultcheck->fetch_assoc();  $userquata = $rowcheck ['userqua'];
} } ?>
// check the counter

-----------------------------------------------------------------------------------------------------------



Comments

Popular posts from this blog

Function Oriented File Upload PHP

Remote File Download and upload directly to Server using PHP

Page Unload Show Notification to user