Page 1 of 1
Cannot send session cookie
Posted: Tue Jul 19, 2011 11:33 am
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:
Re: Cannot send session cookie
Posted: Tue Jul 19, 2011 11:35 am
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.
Re: Cannot send session cookie
Posted: Tue Jul 19, 2011 11:37 am
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.
Re: Cannot send session cookie
Posted: Tue Jul 19, 2011 2:41 pm
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();
?>
Re: Cannot send session cookie
Posted: Tue Jul 19, 2011 2:53 pm
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
Re: Cannot send session cookie
Posted: Tue Jul 19, 2011 6:03 pm
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
Re: Cannot send session cookie
Posted: Tue Jul 19, 2011 6:12 pm
by Xaleph
Add this to the top:
error_reporting(E_ALL);
ini_set("display_errors", true);
This should give errors, if any at all.
Re: Cannot send session cookie
Posted: Tue Jul 19, 2011 7:21 pm
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"
Re: Cannot send session cookie
Posted: Tue Jul 19, 2011 7:48 pm
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.
Re: Cannot send session cookie
Posted: Tue Jul 19, 2011 8:01 pm
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,