Warning using session start();

I have encounter this warning before

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home3/store/includes/connection.php:39) in /home3/store/includes/connection.php on line 30

Could I use the function ob_start(); before session star() and use it anywhere I want? or do I have to star it before any out any ways?

I once used ob _start() function before a header() and it worked. It started the hearder even though there were other outputs before the header() function.

I would recommend not to use buffering (ob_start()) at first hand. Better to start session before any html header outputs. But if you are really compelled to use that then you can use buffering ob_start().

Could I use the function ob_start(); before session star()

yes, but

or do I have to star it before any out any ways?

this way is way better
it’s a matter of logic. and proper architecture.

Check this post: http://www.satya-weblog.com/2007/05/warning-cannot-modify-header.html

I have the set up above before any html or any other output.

session_start();
<?php header('Content-type: text/html; charset=utf-8');?>
<?php function GetCartId()
{
if(isset($_COOKIE["cartId"]))
{
return $_COOKIE["cartId"];
}
else
{

setcookie("cartId", session_id(), time() + ((3600 * 24) * 30)); //line 12
return session_id();
}
}
?><!

But still throwing the warning below

Warning: Cannot modify header information - headers already sent by (output started at /home3/store/includes/header3.php:2) in /home3/store/includes/header3.php on line 12

What is causing the warning is the setcookie() function which is mention in the tutorial that satya has recommended in the previous post along with hearder();. Now since setcookie() is inside a if and else statement I don’t see how can I put it on the top as session star(); is. If I put it in the top then the else statement won’t have any effect on the setcookie() function. Is that a good place to use ob_star();?

Thank you guys!


<?php session_start();
<?php header('Content-type: text/html; charset=utf-8');?>
<?php function GetCartId()
{
if(isset($_COOKIE["cartId"]))
{
return $_COOKIE["cartId"];
}
else
{
ob_start();
setcookie("cartId", session_id(), time() + ((3600 * 24) * 30)); //line 12
return session_id();
}

}

?>

I have used ob_start(); inside the else statement and it won’t work it will still throw the warnings!

Even though you may not think of them this way, because visually you can’t see them, a “new line” is actually one, or sometimes two, characters. Characters are “output”. The php error message gives you a hint at which line number you sent output on. Characters outside of php tags are automatically output.

i understand.

If so much as an errant space character appears in your PHP files before the session_start() is called it will fail.

I notice you are not closing the php tags


 <?php session_start(); 

Also you only need one block of php, looks like you want three

I have solve by using ob_star() not the best to this but I need to do it so because my options were limited.

thank you.