Problem with session_start()

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
Myamo
Forum Newbie
Posts: 3
Joined: Sun Apr 04, 2010 5:45 pm

Problem with session_start()

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with session_start()

Post 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.
samwho
Forum Newbie
Posts: 15
Joined: Sat Apr 03, 2010 8:06 am

Re: Problem with session_start()

Post 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).
Myamo
Forum Newbie
Posts: 3
Joined: Sun Apr 04, 2010 5:45 pm

Re: Problem with session_start()

Post by Myamo »

Ok, another question, how do I carry objects over from one file to another?
samwho
Forum Newbie
Posts: 15
Joined: Sat Apr 03, 2010 8:06 am

Re: Problem with session_start()

Post 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? :)
Post Reply