sessions help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

sessions help

Post 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(); 
} 
} 


?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post 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:
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

been reading up on buffers and headers trying to get my head around this.

anyone help?

thanx heaps :!:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

ob_start()!

:lol: :lol:
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post 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 :!:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

C_Calav you've been here long enough to know to use the search function... !
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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)

=]
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post 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
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

feyd already stated youre problem, and gave the solution :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
Post Reply