Cannot send session cookie

C++, C#, Java, PHP, ect...
Post Reply
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Cannot send session cookie

Post by vitinho444 »

Its the first time i get this error when i try to make a start session for a browser game:

Code: Select all

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\xampp\htdocs\games\space\game.php:4) in C:\xampp\xampp\htdocs\games\space\game.php on line 7
whats that?

PS: the piece of code of the error is:

Code: Select all

session_start();
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Cannot send session cookie

Post by Xaleph »

The real error is this: you already sent headers. That means your server decided to flush the page before YOU as a programmer want it to do that.

Below session_start(); type this: ob_start();

and, on the end of the script type this: ob_flush(); this should flush the page only after that command, all code in between ob_start en ob_flush will now be executed before anything else.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Cannot send session cookie

Post by Xaleph »

Oh more 1 thing:

There`s a difference in PHP`s $_SESSION and $_COOKIE. Even though both are kinda like the same, the $_SESSION is a cookie that only runs in the running cache of the browser. That means, a session only lives for as long as that page/site is open and the browser is open. Once the browser is closed, so long is your session ( which is a good thing, really.. ) Anyway, I work with sessions too, cookies are privacy crackers hihihaha.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Cannot send session cookie

Post by vitinho444 »

same thing here :(

heres the game.php

Code: Select all

<title>
Space Merch ª Overview
</title>
<?php
include 'connect.php';
include 'time.php';
session_start();
ob_start();
?>

<!--Style-->
<head>
<link rel="stylesheet" type="text/css" href="estilo.css" /><center>
</head>

<?php

if (isset($_SESSION['player'])) 

  {

    $player=$_SESSION['player'];

    $info="SELECT * from players where username='$player'";

    $info2=mysql_query($info) or die("Database Error 9B");

    $info3=mysql_fetch_array($info2);

     // echo "<input type='hidden' name='id' value='$info3[id]'>";

	 //$updaterefresh="update gamers set justattacked=0 where ID='$playerinfo3[id]'";

      //mysql_query($updaterefresh) or die("Died");

	 }

	else

	{

	echo "You are not Logged in Space Merch! <br> Please <A href='enter.php'>Login</a><br>";

  exit;

  }
?>


<?php
// Declarar as variaveis do painel de controlo!

$user = $info3['name'];
$gold = $info3['gold'];
$race = $info3['race'];
$planets = $info3['planets']; //Number of planets
$fuel = $info3['fuel'];  // fuel
//naves
$ship1 = $info3['fighters']; // Ship1 > fighters > as naves que atacam
$ship2 = $info3['merchs']; // Ship2 > Merchs > as naves ke transportam
$ship3 = $info3['bx9']; // Ship3 > BX9 > Bombardeiros

$defense1 = $info3['defense1'];
$defense2 = $info3['defense2'];
$defense3 = $info3['defense3'];

//tropas
$troops = $info3['troops'];

// Admin?
$admin = $info3['admin'];


// Niveis dos edificios
$hq = $info3['hqlevel'];  //headquarters
$hangar = $info3['hangarlevel']; // hangar
$lab = $info3['lablevel']; // lab
$rcenter = $info3['rcenterlevel']; //research center
$storage = $info3['storagelevel']; //storage

// ----------------------------------------------------------- //
// ----------------------------------------------------------- //


?>

<?php
///////////////////////////////////////////////////////////////////////////////////////
//////////////////////////PVP//////////////////////////////////////////////////////////










///////////////////////////////////////////////////////////////////////////////////////
//////////////////////////PVP//////////////////////////////////////////////////////////

?>


<?php
echo "<h4><a href='logout.php'>Logout</h4></a>";
echo "Server Time: " . $time;
echo "<br><h4>Player Info:</h4>";
echo "<b>Name: " . $user . "</b>";
echo "<br>Race: " . $race;
echo "<br><h4>Resources: </h4>";
echo "Gold: " . $gold;
echo "<br>Fuel: " . $fuel;
echo "<h4>Buildings: </h4>";
echo "Headquarters Level: " . $hq;
print "<form method='post' action='levelup/addhq.php'>
<input type='submit' value='Level Up HeadQuarters'>
</form>";
echo "Hangar Level: " . $hangar;
print "<form method='post' action='levelup/addhangar.php'>
<input type='submit' value='Level Up Hangar'>
</form>";
echo "Lab Level: " . $lab;
print "<form method='post' action='levelup/addlab.php'>
<input type='submit' value='Level Up Lab'>
</form>";
//echo "<br>Research Level: " . $rcenter;

echo "Storage Level: " . $storage;
print "<form method='post' action='levelup/addstorage.php'>
<input type='submit' value='Level Up Storage'>
</form>";


echo "<br><h4>Ships:</h4>";
echo "Fighters: " . $ship1;
echo "<br>Merchs: " . $ship2;
echo "<br>BX9: " . $ship3;
echo "<br><h4>Defense: </h4>";
echo "Shield1: " . $defense1;
echo "<br>Cannon 1: " . $defense2;
echo "<br>Cannon 2: " . $defense3;


// Links

echo "<br><br><br>Shops:<br>";

echo "<a href='buildings.php'>Buildings</a><br>";
echo "<a href='fuelshop.php'>Fuel Shop</a><br>";
echo "<a href='buyships.php'>Airship Hangar</a><br>";
echo "<a href='defense.php'>HomeLand Security</a><br>";
echo "<a href='out.php'>Expedition Center</a><br>";
echo "<a href='arena.php'>PVP</a><br>";
ob_flush();
?>
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Cannot send session cookie

Post by Xaleph »

Like i said, output has already started. You already return HTML like the <title> tag, et cetera.

Do this:

<?php
session_start();
ob_start();

?>
<title> et cetera
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Cannot send session cookie

Post by vitinho444 »

Man i even deleted all the html stuff from my page:

Code: Select all

<?php
include 'connect.php';
session_start();

if (isset($_POST['submit']))
{
  $player=$_POST['player'];
  $password=$_POST['password'];
  $player=strip_tags($player);
  $password=strip_tags($password);
  $password=md5($password);

  $query = "select name,password from players where name='$player' and '$password'";
  $result = mysql_query($query) or die("Could not query players");
  $result2 = mysql_fetch_array($result);
  if ($result2)
  { 
    $_SESSION['player']=$player;
	$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
    $name = $playerinfo3['name'];
	
    echo "<h1>Welcome " . $name . "<br>";
    echo '<meta http-equiv="refresh" content="0; URL=game.php">';
  }
  else
  {
   echo "Wrong username or password.<A href='login.php'>Try Again</a>";
  }
}
?>
It keeps sending me to wrong username, event if the username is right.. i never got this error with this code because its old! i think the right thing to say now is: WTF IS GOING ON WITH THIS? xD

Hope you know the error
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Cannot send session cookie

Post by Xaleph »

Add this to the top:

error_reporting(E_ALL);
ini_set("display_errors", true);

This should give errors, if any at all.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Cannot send session cookie

Post by vitinho444 »

yes it shows that thing again..

Code: Select all

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\xampp\htdocs\browser\enter.php:2) in C:\xampp\xampp\htdocs\browser\enter.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\xampp\htdocs\browser\enter.php:2) in C:\xampp\xampp\htdocs\browser\enter.php on line 4
Wrong username or password.Try Again
It poits to line 2 where its: "<?php"
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Cannot send session cookie

Post by Xaleph »

Well, that should have been line 1.

The page should always start with <?php so you don`t confuse the interpreter. But regardless, the ob_start(); method is only a means to an end. It works, that`s not the problem, however you shouldn`t have to use it.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Cannot send session cookie

Post by vitinho444 »

hey man, i "fixed" it already, i unisntalled xampp and im currently using wampp, its lighter and simpler

The error is gone, thanks a lot for your time and sorry to take your time with this stupid question, but hey, we learn with our errors ;)

thanks a lot,
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Post Reply

Return to “Coding”