joequery
11/4/2015 - 8:22 AM

PHP Cookie example

PHP Cookie example

<?php
$num_visits = $_COOKIE['page_load_count'];
if(isset($_GET['reset']) || !isset($num_visits)){
    $num_visits = 0;
}
$num_visits++;

setcookie('page_load_count', $num_visits);

?>

<h1>Hello</h1>
<p>You have visited this page <?= $num_visits ?> times</p>
<p><a href="?reset=true">Reset</a></p>

<?php if($num_visits >= 5): ?>
    <h2>Wow!</h2>
    <p>You sure come to this page a lot.</p>
<?php endif; ?>