Here, we’ll show you how you can count visits of a specific visitor with PHP cookies. The following script will say “welcome” on a visitor’s first time visit.
If that visitor has already visited, then it will give the visit number when the visitor was on the website last time.
// set the variable to 0, it'll matter only if the cookie for the variable is not set $countVisit = 0; // if cookie is set for the variable, it'll go to $countVisit and get added by 1; otherwise it'll show 0 for tha variable if(isset($_COOKIE['countVisit'])){ $countVisit = $_COOKIE['countVisit']; $countVisit ++; } // if the last visist cookie is set, it'll pass the value to $lastVisit, and it'll be displayed below; if(isset($_COOKIE['lastVisit'])){ $lastVisit = $_COOKIE['lastVisit']; } // set cookie for countVisit setcookie('countVisit', $countVisit+1, time()+3600); // set cookie for last visit setcookie('lastVisit', date("d-m-Y H:i:s"), time()+3600); // show the respected values // is the variable is not set, say 'welcome', otherwise show the info about visit number and last visit date if($countVisit == 0){ echo "Welcome"; } else { echo "This is your visit number ".$countVisit; echo '<br>'; echo "Last time, you were here ".$lastVisit; }
The post PHP Tip: How to Count Visits With Cookie and Display Welcome Message appeared first on WPULTI: Web Design Magazine, Tutorials, Themes, Inspiration.