Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hit Counter Code giving ERROR 1

Status
Not open for further replies.

vishalonne

Technical User
Jul 29, 2012
49
0
0
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 = 1Here is the Code - Counter.php
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
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
 
session_start() must be in your script before any output is sent to the browser. not that even a blank line is considered output.

so this will not work
Code:
<!--- non printing html --->
<?php session_start(); ?>

nor will this
Code:
<?php session_start(); ?>

but this will work
Code:
<?php session_start(); ?>

if you do not wish to recast your code to do this then a work around is to create a file called sessionstart.php with this in

Code:
<?php if (session_id() == '') session_start(); ?>

then edit your php.ini file to add this
Code:
auto_prepend_file "/path/to/sessionstart.php"

if you are using apache with php in sapi mode you can also add this declaration (in suitably modified form) in a local htaccess file on a per-directory basis.
 
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??

 
Hi

vishallone said:
I just want to keep track how many of visitors came to this page.
Are you sure you want to count how many hits one visitor generates ? That is what you can count using sessions. Because :
[ul]
[li]Sessions are strictly bound to one visitor. You can not mix values from more visitors' sessions. So each visitor will have its own counter.[/li]
[li]Sessions are volatile. After expiration, session data is lost. So each visitor's counter will be reset after the session cookie expires or get deleted.[/li]
[/ul]
Usually counters use permanent storage, like file or database. And they may additionally use sessions to stop spinning the counter on page refreshes.

Feherke.
[link feherke.github.com/][/url]
 
Hi Feherke

I simply want the number of hits on a form.php page ata the bottom gtom google I found this -
I am putting this code on counter.php
PHP:
<?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 bottom of form.php I will put this -
PHP:
<p>This web site has had <b><?php include("counter.php"); ?></b> hits.</p>

If you remeber me then I hope you may remember my form where we had a long discussion regarding validation using Java Script.
 
Hi

Now this is completely different thing. This will indeed count all hits to that page - same for every visitor, no resets. I suppose this one already works for you.


Feherke.
[link feherke.github.com/][/url]
 
Yes You are right I don't want specific user wise counter. Hopefully its Okay. Now some one recommended me to use this - using file_get_contents() and file_put_contents() will simplify

PHP:
<?php
file_put_contents('hitcount.txt', ((int) file_get_contents('hitcount.txt')) + 1);

What is the benefit using the above code
 
Hi

vishalonne said:
What is the benefit using the above code
The web server is serving the requests parallelly, on multiple threads. So is possible that multiple instances of your PHP script will run in the same time.

Supposing hitcount.txt's initial content is 2012, with two hits served in parallel may happen this :
Code:
[small]hitcount | instance 1                               | instance 2
    .txt | code executed                     | $hits| code executed                     | $hits
---------+-----------------------------------+------+-----------------------------------+------
2012     | $filename = 'hitcount.txt';       | null |
2012     | $handle = fopen($filename, 'r');  | null |
2012     | $hits = trim(fgets($handle)) + 1; | 2013 | $filename = 'hitcount.txt';       | null
2012     | fclose($handle);                  | 2013 | $handle = fopen($filename, 'r');  | null
2012     |                                   | 2013 | $hits = trim(fgets($handle)) + 1; | 2013
2012     | $handle = fopen($filename, 'w');  | 2013 | fclose($handle);                  | 2013
2012     | fwrite($handle, $hits);           | 2013 |                                   | 2013
2013     | fclose($handle);                  | 2013 | $handle = fopen($filename, 'w');  | 2013
2013     |                                          | fwrite($handle, $hits);           | 2013
2013     |                                          | fclose($handle);                  | 2013[/small]
Note that the above is just a schematic presentation of what may happen, not necessarily how it will happen.

To avoid such problems the [tt]flock()[/tt] function could be used. That will ensure that only on process accesses the file at a time. But as free hosting services often disable [tt]flock()[/tt] for performance reasons, it was not so frequently popularized.

Your second code has the advantage of using only two builtin functions for all file operations. That reduces the possibility of the shown race condition.


Feherke.
[link feherke.github.com/][/url]
 
Thanx for clearing my doubt

So should I understand that for optimization purpose that code is hlpful. If yes can you tell what modification I must do to achive this.
And how to use flock() as I am not using free server so expected I can use flock()

HEre the existing code
PHP:
<?php

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

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

echo $hits;

?>
 
Hi

vishalonne said:
And how to use flock() as I am not using free server so expected I can use flock()

HEre the existing code
That code is not really suitable for using with [tt]flock()[/tt]. It needs to both read and write the file using a single file handle :
PHP:
[teal]<?php[/teal]

[navy]$filename[/navy] [teal]=[/teal] [green][i]'hitcount.txt'[/i][/green][teal];[/teal]
[navy]$handle[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]fopen[/color][teal]([/teal][navy]$filename[/navy][teal],[/teal] [green][i]'c+'[/i][/green][teal]);[/teal]
[COLOR=darkgoldenrod]flock[/color][teal]([/teal][navy]$handle[/navy][teal],[/teal] LOCK_EX[teal]);[/teal]
[navy]$hits[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]trim[/color][teal]([/teal][COLOR=darkgoldenrod]fgets[/color][teal]([/teal][navy]$handle[/navy][teal]))[/teal] [teal]+[/teal] [purple]1[/purple][teal];[/teal]
[COLOR=darkgoldenrod]rewind[/color][teal]([/teal][navy]$handle[/navy][teal]);[/teal]
[COLOR=darkgoldenrod]fwrite[/color][teal]([/teal][navy]$handle[/navy][teal],[/teal] [navy]$hits[/navy][teal]);[/teal]
[COLOR=darkgoldenrod]flock[/color][teal]([/teal][navy]$handle[/navy][teal],[/teal] LOCK_UN[teal]);[/teal]
[COLOR=darkgoldenrod]fclose[/color][teal]([/teal][navy]$handle[/navy][teal]);[/teal]

[b]echo[/b] [navy]$hits[/navy][teal];[/teal]
Note that if your counter will ever decrease in size, you need a [tt]ftruncate()[/tt] call somewhere between the [tt]fgets()[/tt] and [tt]fwrite()[/tt].

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top