vishalonne Posted August 3, 2012 Share Posted August 3, 2012 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 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? Quote Link to comment https://forums.phpfreaks.com/topic/266632-hit-counter-code-giving-error/ Share on other sites More sharing options...
jazzman1 Posted August 3, 2012 Share Posted August 3, 2012 Have you ever read this topic -> http://forums.phpfreaks.com/index.php?topic=37442.0 Quote Link to comment https://forums.phpfreaks.com/topic/266632-hit-counter-code-giving-error/#findComment-1366547 Share on other sites More sharing options...
vishalonne Posted August 3, 2012 Author Share Posted August 3, 2012 Hi 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?? My problem is simple for you but for new one like me it confusing Quote Link to comment https://forums.phpfreaks.com/topic/266632-hit-counter-code-giving-error/#findComment-1366560 Share on other sites More sharing options...
scootstah Posted August 3, 2012 Share Posted August 3, 2012 Sessions are unique to the user, so they would only ever see their page views, but nobody else's. So, you would need to save the page views to a database, or a flat file or something. Quote Link to comment https://forums.phpfreaks.com/topic/266632-hit-counter-code-giving-error/#findComment-1366562 Share on other sites More sharing options...
vishalonne Posted August 3, 2012 Author Share Posted August 3, 2012 I found this code from Google <?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 I have to put this code where I want to display the counter like this <p>This web site has had <b><?php include("counter.php"); ?></b> hits since January 1st, 2007.</p> This will open a txt file and write in it. Is this OK? Quote Link to comment https://forums.phpfreaks.com/topic/266632-hit-counter-code-giving-error/#findComment-1366567 Share on other sites More sharing options...
scootstah Posted August 3, 2012 Share Posted August 3, 2012 Sure that will work, but you can simplify it a bit using file_get_contents and file_put_contents. <?php file_put_contents('hitcount.txt', ((int) file_get_contents('hitcount.txt')) + 1); Quote Link to comment https://forums.phpfreaks.com/topic/266632-hit-counter-code-giving-error/#findComment-1366569 Share on other sites More sharing options...
vishalonne Posted August 3, 2012 Author Share Posted August 3, 2012 Thanx Do you mena this My Code $hits = trim(fgets($handle)) + 1; Your Code file_put_contents('hitcount.txt', ((int) file_get_contents('hitcount.txt')) + 1); Am I correct Quote Link to comment https://forums.phpfreaks.com/topic/266632-hit-counter-code-giving-error/#findComment-1366573 Share on other sites More sharing options...
scootstah Posted August 3, 2012 Share Posted August 3, 2012 You don't need any of your code, just the one line I wrote. I mean, either way works fine, but file_get_contents() and file_put_contents() is the preferred method. Quote Link to comment https://forums.phpfreaks.com/topic/266632-hit-counter-code-giving-error/#findComment-1366575 Share on other sites More sharing options...
vishalonne Posted August 3, 2012 Author Share Posted August 3, 2012 Well Thank you. I done with my old code because I don't know what is benefit of using the way to suggested, as I am a learner, but I will obliviously try to know what is the difference. Thanx once again for cooperation Quote Link to comment https://forums.phpfreaks.com/topic/266632-hit-counter-code-giving-error/#findComment-1366576 Share on other sites More sharing options...
scootstah Posted August 3, 2012 Share Posted August 3, 2012 You get the same result either way, but the benefit is less code. For example to write data to a file you don't have to call fopen(), fwrite(), and fclose() - you simply call file_put_contents(). It's just a bit easier, is all. Quote Link to comment https://forums.phpfreaks.com/topic/266632-hit-counter-code-giving-error/#findComment-1366577 Share on other sites More sharing options...
vishalonne Posted August 3, 2012 Author Share Posted August 3, 2012 Thank you Scootstah for clearing my doubt. Quote Link to comment https://forums.phpfreaks.com/topic/266632-hit-counter-code-giving-error/#findComment-1366580 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.