Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

session_start() problem 1

Status
Not open for further replies.

tshey

Technical User
Dec 28, 2005
78
AU
I have created a cart, it works perfect on my mac, but when I test it on my pc it comes up with a warning:
Warning: session_start(): Cannot send session cookie-headers already sent by (cart.php)
Also another warning:
Warning: Cannot modify header information - headers already sent by (cart.php)

What does this all mean?
 
Sounds like you are trying to start the session when you have already output something to the screen.

You need to make sure that the top lines of the files in question are as follows:
<?
session_start();

Make sure there is no whitespace before this and you should be OK.

The problem may also be that you ae including the cart from another file, after something has been output to the screen. ou'll have to check your code and see where in the overall scheme of things the session is being initialised.

Richard
 
This is the data.php file that contains the session start errors. It is added into the cart with <?php include ('data.php') etc.
Code:
<?php

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

	$dbServer = "localhost";
	$dbUser = "sifmedia_admin";
	$dbPass = "robchrisvac";
	$dbName = "sifmedia_vacuumshop";

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

?>
 
The actual script line that produces the output that causes the error is not necessarily in this file. If this file is included from another file, then it could very easily be a line in the including file.

On my system, given the following scripts:

test_bar.php:
Code:
<?php
print 'a';
include ('test_bar2.php');
?>

test_bar2.php:
Code:
<?php
session_start();
?>

When I point my browser to test_bar.php, I get the following error:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/sites/test/html/test_bar.php:2) in /home/sites/test/html/test_bar2.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/sites/test/html/test_bar.php:2) in /home/sites/test/html/test_bar2.php on line 2

The part of the warning that is most interesting to me is the part that reads, "output started at /home/sites/test/html/test_bar.php:2".

Does your error message have similar text?



Want the best answers? Ask the best questions! TANSTAAFL!
 
I reckon the problem is because in the cart.php (function GetCartId() function)that is included in other pages you have a session_start() command. in the original file that calls cart.php you must have some output echoed to the screen before you include the cart.php file and this is what is causing the error message.

I would suggest you have the session_start() as the first line in the main file (after the php opening brace). I suppose you could always use ob_start() before the session_start() to clear the headers but I'm not sure if this will cause other problems or not. Maybe someone else will be able to advise on this.

Richard
 
thankyou for the posts guys, you are wonderful. I think it was a spacing problem, it know works, I tried everything listed above and it is now clear of errors. Thankyou again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top