Jump to content

Session in PHP


Go to solution Solved by Pawan_Agarwal,

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/279229-session-in-php/
Share on other sites

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>
Link to comment
https://forums.phpfreaks.com/topic/279229-session-in-php/#findComment-1436234
Share on other sites

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......
Link to comment
https://forums.phpfreaks.com/topic/279229-session-in-php/#findComment-1436248
Share on other sites

  • 2 weeks later...
  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.