Header Problem

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

mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

There is obviosly somethign wron with the $_SESSION code in both the error message code and in the code which calls it; php code 1 and 3 in above post.

VBut can anyone spot what the problem is - why isnt it calling the error message.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

session_start() by default tries to set a cookie with the session id.
a cookie is a http header.
http headers have to be sent before the reponse body.
any output belongs to the reponse body.
at line 2 of /export/TSG-Z/web-source/organisation/plCMS/organisation some was output.
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

So with regards to displaying the error message are you saying

Code: Select all

<?
if(isset($_SESSION['errmsg'])){
echo $_SESSION['errmsg'];
unset($_SESSION['errmsg']);
}
?>
this needs to be at the top of the homepage with no spaces. For the error message to show??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

no, only session_start() and additional header() calls have to be made before any output (via echo,print ect and characters outside of a php block) is sent to the client.
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Volka im really sorry but ive read your post over and over again. I dont understand.

Lets keep it simple.

What should I change on this code to make the error message appear.

The code listed above works fine it throws the unauthorized user back to the homepage it just doesnt show them the error message.

What should I change to make this error message appear?
richo
Forum Commoner
Posts: 58
Joined: Sun Aug 06, 2006 11:56 am

Post by richo »

Do you know for sure the session is working properly? Maybe you need to start the session on each page (ie - the one that is creating the session and the one that is detecting if it has been set).


See here to find out more:

http://uk2.php.net/function.session-start

(i'm afraid i can't try your code out as i don't have access to php localhost right now)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

mohson wrote:Lets keep it simple.
as simple as it gets ;)

Code: Select all

<?php
echo 'xyz';
session_start(); // won't work, output before session_start(), warning: cannot modifiy yadda yadda
?>

Code: Select all

<?php
session_start(); // session_start before any output for this htttp-request, no problem.
echo 'xyz';
?>
php tells you exactly which line of code caused the output:
mohson wrote:headers already sent by (output started at /export/TSG-Z/web-source/organisation/plCMS/organisation:2 in export/TSG-Z websource/organisation/PL/CMS/Uorgs.htm on line 6.
Post Reply