Page 1 of 1

Problem with session_start()

Posted: Sun Apr 04, 2010 5:54 pm
by Myamo
Hey,

I have following code:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  xml:lang="de" lang="de">
	<head>
		<title>Codetest</title>
	</head>
	<body>
		<?php 
			session_start();
			require_once 'objecttest.php';
			$o = new objecttest();
			$_SESSION['object'] = $o;
		?>
...
Now when I open this in a webbrowser, I get following warnings:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /var/chroot/home/rond71/public_html/index.php:7) in /var/chroot/home/rond71/public_html/index.php on line 8

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/chroot/home/rond71/public_html/index.php:7) in /var/chroot/home/rond71/public_html/index.php on line 8

What's wrong?

Re: Problem with session_start()

Posted: Sun Apr 04, 2010 7:15 pm
by requinix
You cannot use session_start() or header() or a few other functions if you've outputted anything.

Move it to the beginning of the file.

Re: Problem with session_start()

Posted: Sun Apr 04, 2010 7:23 pm
by samwho
Mm, rule of thumb is to put anything header/session related before ANYTHING else :)

If you're unfamiliar with how HTTP headers work it's pretty cool and interesting to look up.

Start here: http://en.wikipedia.org/wiki/List_of_HTTP_headers and look some of them up. Notably, you want to be looking at the Cookie header regarding your current problem as that's what is giving you the error. Sessions are based on a cookie data (or if cookies are disabled, URL-passed arguments).

Re: Problem with session_start()

Posted: Sun Apr 04, 2010 7:37 pm
by Myamo
Ok, another question, how do I carry objects over from one file to another?

Re: Problem with session_start()

Posted: Sun Apr 04, 2010 7:56 pm
by samwho
I actually haven't used OOP with PHP before but I would imagine you keep them in a separate .php file and use the require_once statement when you need to use them? :)