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

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

<?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

<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

You've outputted HTML to the page before calling session start.

To resolve the problem, you need to call session_start(); at the beginning of your file.

Or you could try something like:

<?php 
session_start(); 
$_SESSION['views'] = isset($_SESSION['views']) ? $_SESSION['views'] + 1 : 1;
?>
<div class='sfm_cr_box' style='padding:3px; width:350px'>
    <hr />
    <div class="center">
        <?php echo $_SESSION['views']; ?>
    </div>
</div>

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 how many of visitors came to this page.

Is it possible without session??

The session will only record the number of times a single visitor visits the page.

To record the number of visits across all visitors, you'll need to use a persistent solution - e.g. database or text file.

It's only the start_session(); call that needs to go at the top of the page. The output can be displayed anywhere.

To create what you want, you should create a PHP script that adds one to the number existing in a text file somewhere.

  1. In the HTML file, you simply echo the contents of the file where needed.

    <small>This page has been visited <?php $count = file_get_contents("INSERT_PATH_TO_THE_TEXT_FILE"); echo $count; ?> times!</small>
    
  2. At the end of the same HTML page, insert a function that:

Opens a text file, reads it's contents in a variable
Adds one to the number read
Overwrites the existing file with the new number (after adding one to the existing number)
Closes the file

Hope that's clear!

Is this code okay as you suggested

<?php

$filename = 'hitcount.txt';
$handle = fopen($filename, 'r');
$hits = trim(fgets($handle)) + 1;
fclose($handle);

$handle = fopen($filename, 'w');
fwrite($handle, $hits);
fclose($handle);

// Uncomment the next line (remove //) to display the number of hits on your page.
//echo $hits;

?>

And at the end of my form.php page I will add this

<p>This web site has had <b><?php include("counter.php"); ?></b> hits since January 1st, 2007.</p>

<?php
function increment_hit_count()
{
    $count = file_get_contents('hit_count.txt');
    file_put_contents('hit_count.txt', $count + 1);
}

function get_hit_count()
{
    return file_get_contents('hit_count.txt');
}
?>


<?php increment_hit_count(); ?>
<p>This site has had <?php echo get_hit_count(); ?> hits since January 2007.</p>

vishalonne, the code you wrote is exactly what I suggested!

Good luck!

Thank you very much for for guidance

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.