Hit Counter Code giving ERROR

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Post Reply
vishalonne
New php-forum User
New php-forum User
Posts: 11
Joined: Fri Jul 27, 2012 9:52 am

Hit Counter Code giving ERROR

Post by vishalonne » Fri Aug 03, 2012 12:39 am

Hi All
I am getting an warning on my web page when I uploaded the page on server I just writing a PHP Page Hit Counter-

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/cbsecpsn/public_html/csnip/form_453570.php:7) in /home/cbsecpsn/public_html/csnip/counter.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/cbsecpsn/public_html/csnip/form_453570.php:7) in /home/cbsecpsn/public_html/csnip/counter.php on line 2

This the output - Total page views = 1
Here is the Code -
Counter.php

Code: Select all

<?PHP
session_start();
if(isset($_SESSION['views'])){
$_SESSION['views'] = $_SESSION['views']+ 1;
}else{
$_SESSION['views'] = 1;
}
echo "Total page views = ". $_SESSION['views'];
?>
And I used it like this in Survey_Form.php

Code: Select all

<div class='sfm_cr_box' style='padding:3px; width:350px'>
<?php
echo "<hr><div align=\"center\">";
include_once "counter.php"; // this will include the counter.
echo "</div>";
?>
</div>
What is the problem with the code

faust
New php-forum User
New php-forum User
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: Hit Counter Code giving ERROR

Post by faust » Fri Aug 03, 2012 3:31 am

Code: Select all

<div class='sfm_cr_box' style='padding:3px; width:350px'>
this is your problem. You start session_start(); ALWAYS at the beginning of file. No output before it, not even a single whitespace or space.
Basically, this goes at the top of your file before all html output:

Code: Select all

include_once "counter.php"; // this will include the counter.

vishalonne
New php-forum User
New php-forum User
Posts: 11
Joined: Fri Jul 27, 2012 9:52 am

Re: Hit Counter Code giving ERROR

Post by vishalonne » Fri Aug 03, 2012 5:02 am

Thank you for looking in my problem, actually I want to display hit counter at the bottom of the page. Do I need session...?????
I just want to keep track of numbers of visitors.

Is it possible without session??

kyle04
New php-forum User
New php-forum User
Posts: 94
Joined: Sat Jul 07, 2012 1:36 pm

Re: Hit Counter Code giving ERROR

Post by kyle04 » Fri Aug 03, 2012 5:56 am

You just need to re-arrange your scripts a little :

Counter.php

Code: Select all

<?
if(isset($_SESSION['views'])){
$_SESSION['views'] = $_SESSION['views']+ 1;
}else{
$_SESSION['views'] = 1;
}
?>
Survey_Form.php

Code: Select all

<?PHP
session_start();
include_once ("counter.php");

echo "<div class='sfm_cr_box' style='padding:3px; width:350px'>";
echo "<hr><div align=\"center\">";
echo "Total page views = ". $_SESSION['views'];
echo "</div>";
echo "</div>";

?>

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 8 guests