Cookie example

13.05.2011

The simplest example of using a session and cookies, for example, to calculate page traffic by a specific user. When the user first visits, a message is displayed that he is here for the first time, and on subsequent visits, the user is told that he has already been here and the number of his visits is displayed, and a page refresh link is generated.

test tkest
<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
 <head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  <link rel="icon" href="icon.png"/>
  <title>Cookie example</title>
 </head>
 <body>
  <h2 style="text-align:center;">Cookie example</h2>
  <?php
   if (!isset($_SESSION['counter']))
   {
    echo '<center>This is your first visit.</center>';
    $_SESSION['counter'] = 1;
   }
   else
   {
    ++$_SESSION['counter'];
    echo '<p style="text-align:center;">You've been here before ',$_SESSION['counter'],' times.</p>';
   }
   $url = $_SERVER['PHP_SELF'];
   $sid = sid;
   if (isset($sid) && $sid) $href = $url.'?'.$sid;
   else $href = $url;
   echo '<p style="text-align:center;"><a href="',$href,'">Visit us again!</a></p>';
  ?>
 </body>
</html>

Last in our blog

Internet Marketing
04.11.2019