Click here to Skip to main content
14,695,503 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Sir
My name is Sarfaraz. I am new to PHP and i found some code for counting the number of visitors of my website which is working just fine when opening that page but when I am trying to embed it in footer.php I am getting some errors. I just wanted to know how to embed it in footer.php.
I am including this footer.php in my website also.
I am getting the folowing error when embeding this in footer.php

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\xxx\index.php:144) in C:\xampp\htdocs\xxx\includes\counter.php on line 2
You are visitor number 9 to this site
Thank you
Posted
Updated 14-Jan-14 15:40pm
v2
Comments
Sergey Alexandrovich Kryukov 14-Jan-14 16:10pm
   
"I am getting some errors" is not informative. This is not a good question; we cannot figure out what can help you; there is nothing special you need to know; just fix your bugs...
—SA

1 solution

Let do some detective work, I think you footer file is called counter.php which contains session_start on line 2. You have included this file into the footer of index.php which also has its own session_start. This could be one of the reason.
If not, check this out: warning-session-start-function-session-start-cannot-send-session-cache-lim[^]

++++++++++++++++++++++++++++++++++++++++++++

This is added in response to your further queries:
1. The php code shown by you is using a text file "counter.txt" to store the visitor number, the "$_SESSION['hasVisited']" is redundant here, it is not needed, the following code will do, assuming you save it as "counter.php" in a folder called "includes":
<?php
$counter_name = "counter.txt";
if (!file_exists($counter_name))
{
    $f = fopen($counter_name, "w");
    fwrite($f,"0"); fclose($f);
}
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
$counterVal++;
$f = fopen($counter_name, "w");
fwrite($f, $counterVal);
fclose($f);
echo "visitor number $counterVal";
?>

Your next query on how to include this "counter.php" into another php file, say "index.php" which is located along side the "includes" folder, adapt this:
<?php

echo "This is the header content<br>";
echo "This is the main content<br>";
echo "This is the footer content<br>";
include "./includes/counter.php";
?>

Now when you launch the"index.php", the visitor number will increase at every refresh. A text file called "counter.txt" would have been created in along side the "index.php".
   
v4
Comments
sarfarazbhat 15-Jan-14 8:38am
   
Thank you sir
I got the answer. But now after commenting session_Start(); My counter counts when visiting from one page to other.
My code is

<!--?php session_start();
$counter_name = "counter.txt";
if (!file_exists($counter_name)) {
$f = fopen($counter_name, "w");
fwrite($f,"0");
fclose($f);
}
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
if(!isset($_SESSION['hasVisited'])){
$_SESSION['hasVisited']="yes";
$counterVal++;
$f = fopen($counter_name, "w");
fwrite($f, $counterVal);
fclose($f);
}
echo "visitor number $counterVal";
??-->

Please help in how to include this file in header.php.
Thank you
Peter Leow 15-Jan-14 9:54am
   
See my addition to solution 1.
sarfarazbhat 15-Jan-14 12:57pm
   
Thank you very much sir for your kind help.
Thank you.
Peter Leow 15-Jan-14 21:03pm
   
Consider giving me a 5?
sarfarazbhat 18-Jan-14 1:51am
   
sure!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900