Page 1 of 1

sessions help

Posted: Mon Jul 26, 2004 2:28 am
by C_Calav
hi guys

anyone explain or help whats going on?

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /var/users/modelair/modelaircraft.co.nz/htdocs/db.php:48) in /var/users/modelair/modelaircraft.co.nz/htdocs/db.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /var/users/modelair/modelaircraft.co.nz/htdocs/db.php:48) in /var/users/modelair/modelaircraft.co.nz/htdocs/db.php on line 41


Code: Select all

<?php

// This page contains the connection routine for the 
// database as well as getting the ID of the cart, etc 

$dbServer = "***"; 
$dbUser = "***"; 
$dbPass = "***"; 
$dbName = "models"; 

function ConnectToDb($server, $user, $pass, $database) 
{ 
// Connect to the database and return 
// true/false depending on whether or 
// not a connection could be made. 

$s = @mysql_connect($server, $user, $pass); 
$d = @mysql_select_db($database, $s); 

if(!$s || !$d) 
return false; 
else 
return true; 
} 

function GetCartId() 
{ 
// This function will generate an encrypted string and 
// will set it as a cookie using set_cookie. This will 
// also be used as the cookieId field in the cart table 

if(isset($_COOKIE["cartId"])) 
{ 
return $_COOKIE["cartId"]; 
} 
else 
{ 
// There is no cookie set. We will set the cookie 
// and return the value of the users session ID 

session_start(); 
setcookie("cartId", session_id(), time() + ((3600 * 24) * 30)); 
return session_id(); 
} 
} 


?>

Posted: Mon Jul 26, 2004 2:35 am
by feyd
line 48 of db.php started writing data to the output buffer, before the header calls could be made from db.php line 41.

Posted: Mon Jul 26, 2004 2:48 am
by C_Calav
thanks feyd,

how can i solve this? by putting the header calls before the output buffer?

following a tutorial here. not very helpful :cry:

Posted: Mon Jul 26, 2004 4:15 am
by C_Calav
been reading up on buffers and headers trying to get my head around this.

anyone help?

thanx heaps :!:

Posted: Mon Jul 26, 2004 10:41 am
by feyd
white-space or any other amount of character output will result (generally) in header's already sent errors. You need to alter your script so it doesn't print anything until all your headers are set up. I know someone will suggest [php_man]ob_start[/php_man], that's not the point though.

Posted: Mon Jul 26, 2004 4:29 pm
by tim
ob_start()!

:lol: :lol:

Posted: Mon Jul 26, 2004 6:27 pm
by C_Calav
thanx feyd and tim,

so i just rearagne my script? can you suggest what to do please? eg what goes where

thanx :!:

Posted: Mon Jul 26, 2004 7:34 pm
by John Cartwright
C_Calav you've been here long enough to know to use the search function... !

Posted: Mon Jul 26, 2004 8:52 pm
by tim
I will have to agree with phen on this one.

going to php.net and typing ob_start in the search function list isnt rocket science.

Althou you could maybe arrange your code so no Output (html, white spaces, etc) isnt present before the code.

hint - put session_start(); as the first line on all your pages (least the ones u wish to carry session values over)

=]

Posted: Tue Jul 27, 2004 1:09 am
by C_Calav
hey guys,

i agree i have been here long enough to use the search! and i use it!

i need to know whats going wrong and what to do to fix it so i can research and try to solve it!

im not new to programming but newish to php, first project. thanx for everyones help its been priceless :D

Posted: Tue Jul 27, 2004 1:13 am
by ol4pr0
feyd already stated youre problem, and gave the solution :)

Posted: Tue Jul 27, 2004 1:23 am
by John Cartwright
Let me help you out a bit when searching...

Normally when i Get an error I will first try and use the forums search:

in this case I did "headers already sent" change click search for all terms... 10 sec later i had identified the problem and it's solution :)

if that fails I go to google.com which 99% of the time unless very unique will solve the problem

if all else fails I read up at php.net and pick at that.