-1

Possible Duplicate:
Headers already sent by PHP

I am trying to create a sessions based hit counter. And my code doesn't seem to work. Whenever, i reload the page. The output is "YOU HAVE VIEWED THE PAGE 0 TIMES".

    <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
 <?php
session_start();

if(!isset($_SESSION['count']))
    $_SESSION['count'] = 0;
else
    $_SESSION['count']++;

 echo "You Have Viewed this page ", $_SESSION['count'], " times.";  

 echo session_id(); ?>
</body>
</html>

When, i run this code on my localhost. The code simply doesn't work. And when i run it on my webserver(hostgator), i get following errors.

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/rohitink/public_html/uiet.org/s4/index.php:8) in /home/rohitink/public_html/uiet.org/s4/index.php on line 9

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/rohitink/public_html/uiet.org/s4/index.php:8) in /home/rohitink/public_html/uiet.org/s4/index.php on line 9
You Have Viewed this page 0 times.788141cfa1329643918f87f5f6c9eb5a

Please Give me details on How to Fix this error. Thank you very much.

Community
  • 1
  • 1
  • 1
    Please read some of the millions [“Cannot send session cookie - headers already sent” questions](http://stackoverflow.com/search?q=%22Cannot+send+session+cookie+-+headers+already+sent%22). – Gumbo Sep 23 '12 at 06:32
  • 1
    Either the `session_start()` function must be in the very first line in your PHP file (trimming leading and trailing white species) or you may set `output_buffering = on` in your php.ini file. – Lion Sep 23 '12 at 06:33

2 Answers2

1

You need to put the session_start() at the top of that page:

<?php session_start(); ?>
<html>
....

The reason is that session_start() sets a cookie on the client, and cookies can only be set if there has been no output yet.

Petah
  • 45,477
  • 28
  • 157
  • 213
1

you have to start the session on the very first line of the page even he space caused the warning

enter image description here

and for header already send error check this Headers already sent by PHP

Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143