Saturday, August 3, 2019

WT 6 - PHP - NUMBER OF VISITORS

6. WRITE A PHP PROGRAM TO KEEP TRACK OF THE NUMBER OF VISITORS VISITING THE WEB PAGE AND TO DISPLAY THIS COUNT OF VISITORS, WITH PROPER HEADINGS.

 SOLUTION  1 

1. If xampp is in the system, save the program in C:\xampp\htdocs (start apache in xampp control panel)

2. If no xampp in the system, save the program in C:\Program Files\Apache Group\Apache2\htdocs   & Open Google Chrome & type http://localhost/prog6.php

prog6.php

<?php
echo "<h1> REFRESH PAGE </h1>" ;
$file = 'count.txt' ;
$c = file_get_contents($file) ;
file_put_contents($file, $c+1);
echo "The number of users visited : ".$c ;
?>

STEPS:( click on image to zoom )






OUTPUT :( click on image to zoom )



 SOLUTION  2 

prog6.php

<?php
print "<h3> REFRESH PAGE </h3>";
$name="counter.txt";
$file = fopen($name,"r");
$hits= fscanf($file,"%d");
fclose($file);
$hits[0]++;
$file = fopen($name,"w");
fprintf($file,"%d",$hits[0]);
fclose($file);
print "Total number of views: ".$hits[0];
?>

OUTPUT :

REFRESH PAGE


Total number of views: 10


No comments:

Post a Comment