I need help with writng a php code that reads cookies containing a user's name and the number of times he or she has visited a Web site. Whenever a user visits the site and clicks the submit button, print the value of the cookie count to the page, increment the cookie counter by one, and then reset the counter cookie expiration date to one year from the current date. Help!!! if anyone would explain if the cookies should be set in the top part of the php script then be echo in theHTML body ? I seem not to grasp this process! thanks in advance for your hal.

<?php
if (isset($_GET["firstname"] && $_GET["lastname"])) { #if data is set
   if (!empty( $_GET["firstname"] && (!empty($_GET["lastname"])) {
      $Fname= $_GET["firstname"];
      $Lname= $_GET["lastname"];
      $User= $Fname . " " ."$Lname". ;
         /* Set cookie to last 1 year */
         setcookie("username", $User, time()+(60*60*24*365));
         if (isset($_COOKIE[['username']))
            //echo "Welcome " . $_COOKIE["Username"] !<br />";
	if (!isset($_COOKIE['visits']))
 		$_COOKIE['visits'] = 0;
		$visits = $_COOKIE['visits'] + 1;
	setcookie('visits',$visits,time()+3600*24*365);
   }
#eof if value entered
   else {
        echo "No empty fields allowed <br />";
        header('location: welcome.html');
      }
 } #eof is set
 #data is not set
 else
 {
  echo "You must supply both Firstname and lastname <br />";
  header('location: welcome.html');
 }
 //print_r($_COOKIE);
  
/*else
  echo "Welcome guest!<br />";
   // A way to view all cookies  */

?>
<html>
<head>
<title> Title </title>
</head>
<body>
<?php
if ($visits > 1) {
  echo("This is visit number $visits.");
} else { // First visit
  echo('Welcome to my Website! Click here for a tour!');
}
?>
</body>
</html>

Try something along the lines of the following:

<?php
$_COOKIE['visits']=(isset($_COOKIE['visits']))?$_COOKIE['visits']++:1;
setcookie('visits',$_COOKIE['visits'],time()+3600*24*365);
if (isset($_GET["firstname"]) && $_GET["lastname"]) { #if data is set
   if (!empty($_GET["firstname"]) && !empty($_GET["lastname"]) {
      $Fname= $_GET["firstname"];
      $Lname= $_GET["lastname"];
      $User= $Fname . " " ."$Lname". ;
         /* Set cookie to last 1 year */
         setcookie("username", $User, time()+(60*60*24*365));
         //if (isset($_COOKIE[['username']))
            //echo "Welcome " . $_COOKIE["Username"] !<br />";
   }
#eof if value entered
   else {
        echo "No empty fields allowed <br />";
        header('location: welcome.html');
      }
 } #eof is set
 #data is not set
 else
 {
  echo "You must supply both Firstname and lastname <br />";
  header('location: welcome.html');
 }
 //print_r($_COOKIE);
  
/*else
  echo "Welcome guest!<br />";
   // A way to view all cookies  */

?>
<html>
<head>
<title> Title </title>
</head>
<body>
<?php
if ($visits > 1) {
  echo("This is visit number $visits.");
} else { // First visit
  echo('Welcome to my Website! Click here for a tour!');
}
?>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.