Pawan_Agarwal Posted June 16, 2013 Share Posted June 16, 2013 I am using this code <?php session_start(); // store session data $_SESSION['views']=1; ?> <html> <body> <?php //retrieve session data echo "<br><br>Pageviews=". $_SESSION['views']; if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo "<br><br>Views=". $_SESSION['views']; ?> </body> </html> it is giving error, I don't know how to remove it. Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\prac\session1.php:2) in C:\xampp\htdocs\prac\session1.php on line 3 Quote Link to comment https://forums.phpfreaks.com/topic/279229-session-in-php/ Share on other sites More sharing options...
xenLiam Posted June 16, 2013 Share Posted June 16, 2013 Put ob_start() after opening <?php. This is probably due to the whitespace you have. As such: <?php ob_start(); // Output buffering session_start(); // store session data $_SESSION['views']=1; ?> <html> <body> <?php //retrieve session data echo "<br><br>Pageviews=". $_SESSION['views']; if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo "<br><br>Views=". $_SESSION['views']; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/279229-session-in-php/#findComment-1436234 Share on other sites More sharing options...
trq Posted June 16, 2013 Share Posted June 16, 2013 Put ob_start() after opening <?php. Better still, find what is causing the output and remove it. Output buffering has its uses, this hack is not one of them. Quote Link to comment https://forums.phpfreaks.com/topic/279229-session-in-php/#findComment-1436246 Share on other sites More sharing options...
Pawan_Agarwal Posted June 16, 2013 Author Share Posted June 16, 2013 That was quick help, now I am stuck in another issue.......... I am developing counter on website, that will show the total number of visitors visited the website, however, the code I have created keeps on increment when I click same or different web pages <?php ///session_start(); $username = "********"; $password = "********"; $hostname = "********"; $database = "********"; $conn = mysql_connect($hostname, $username, $password, $database) or die("Could not connect: ".mysql_error()); $selected = mysql_select_db("*******",$conn) or die("Could not select database"); $result = mysql_query("SELECT * from visitors"); if(!$result) {echo(die("cannot execute:").mysql.error());} $total; /// $total will contain all the total visitors in website $i;//////temp variable //fetch thw data from the database while ($row = mysql_fetch_array($result)) { $total=$row['Total']; } $_SESSION['views'] = $total; if(isset($_SESSION['views'])) { // store session data $total=$total+1; $result = mysql_query("UPDATE visitors SET Total='$total'") ; } $i=$total; $str="0000000".(string)$i; $len1=strlen($str); echo"<div class=\"desc2\">Visitor:"; while($len<$len1) { echo " <img src='/counter/crt/".$str[$len].".gif'/>"; $len++; } ?> </div> =================================================== I am looking for something that will update the visitor number as soon as website is hitted, after that it should remain static, but the code I have written is increasing the counter as I am clicking the website again and again...... Quote Link to comment https://forums.phpfreaks.com/topic/279229-session-in-php/#findComment-1436248 Share on other sites More sharing options...
trq Posted June 16, 2013 Share Posted June 16, 2013 Of course it does. Your not recording wether or not this actual page has already been counted or not. I suggest you start another thread if your stuck. Quote Link to comment https://forums.phpfreaks.com/topic/279229-session-in-php/#findComment-1436252 Share on other sites More sharing options...
Pawan_Agarwal Posted June 17, 2013 Author Share Posted June 17, 2013 When I visit any page of my website, it the counter for visitor number increases on each click, it never stops......... Quote Link to comment https://forums.phpfreaks.com/topic/279229-session-in-php/#findComment-1436358 Share on other sites More sharing options...
trq Posted June 17, 2013 Share Posted June 17, 2013 No kidding, you already said that. Quote Link to comment https://forums.phpfreaks.com/topic/279229-session-in-php/#findComment-1436359 Share on other sites More sharing options...
xenLiam Posted June 24, 2013 Share Posted June 24, 2013 When you're inserting a page view count to the database, log its IP address too. That way, you can check if the IP address is already registered and if it is, don't count it anymore. Not the most fool-proof method, but that works. Quote Link to comment https://forums.phpfreaks.com/topic/279229-session-in-php/#findComment-1437584 Share on other sites More sharing options...
Pawan_Agarwal Posted July 2, 2013 Author Share Posted July 2, 2013 how to block the IP address ?? Quote Link to comment https://forums.phpfreaks.com/topic/279229-session-in-php/#findComment-1438965 Share on other sites More sharing options...
Solution Pawan_Agarwal Posted July 18, 2013 Author Solution Share Posted July 18, 2013 I did it by some way out !! Quote Link to comment https://forums.phpfreaks.com/topic/279229-session-in-php/#findComment-1441254 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.