Jump to content

Please help this is driving me insane!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Carlo717

Carlo717

    Newbie

  • Members
  • Pip
  • 9 posts
I'm trying to create a simple wall script like on Facebook wall. Eventually this will be in a database, but for now I want to test it. I have a form down the page which has a name field and message field in it. If there is a name in the session it does not show the name field and instead echos the session name.

Thiss works well for one message, it keeps the name in the session and onl shows the messsage box... but then second time it tells me no name has been set and starts all over again.


		

if(isset($_POST['Submit']))

{

	if(isset($_SESSION['Name'])&&(!empty($_SESSION['Name'])))

	echo "Session name ".$_SESSION['Name']."<br />";

		

	elseif(empty($_POST['Name']))

	echo "Please enter a name. <br />";

	elseif(!empty($_POST['Name']))

	{

	session_start();

	$Name=$_POST['Name'];

	$_SESSION['Name']=$Name;

	}

	

	if(empty($_POST['Msg']))

			echo "Your message was empty.";

	else

	{

		$Msg=$_POST['Msg'];

	}

	


}

	



#2
Carlo717

Carlo717

    Newbie

  • Members
  • Pip
  • 9 posts
Got it, the session_start() tag had to be placed at the top of the page. Can anyone explain why this is?

#3
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
$_SESSION array is not populated with session data until session_start() is called. This is why you have to call session_start() at the begining of the page (before the first access to $_SESSION). If not, then isset($_SESSION['<anything>']) always returns false.

#4
Carlo717

Carlo717

    Newbie

  • Members
  • Pip
  • 9 posts

dbug said:

$_SESSION array is not populated with session data until session_start() is called. This is why you have to call session_start() at the begining of the page (before the first access to $_SESSION). If not, then isset($_SESSION['<anything>']) always returns false.

Yeah but the first time the $session was used it had been called (if no session was there and if the name had been posted a session was started ... but this didnt work and the session_start had to be above everything.

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
session_start() also sends the header to the browser containing the session cookie (just a few identifying codes, not the info stored in $_SESSION, those is saved locally on the server), and they have to be sat before any other output is done.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall