Page 1 of 1

Sessions...

Posted: Fri Jul 09, 2010 9:24 am
by lenton
I have two files: test.php and test2.php

test.php:

Code: Select all

<?php
$_SESSION['user'] = "test";
header('Location: test2.php');
?>
test2.php:

Code: Select all

<?php
  if(isset($_SESSION['user']))
  {
    echo "WORKED";
  }
  else
  {
    echo "FAILED";
  }
?>
I go to test.php and in test2.php it echos FAILED..

Why doesn't the session variable carry over to test2.php? Thanks for your help.

Re: Sessions...

Posted: Fri Jul 09, 2010 9:30 am
by Skiddles2010
Use session_start(); at the top of both pages to tell PHP that you're utilizing sessions, otherwise you're just throwing that variable into the abyss, never to be heard from again. After starting the session you can then start throwing variables like user into it and also validate that $_SESSION['user'] isset.

Re: Sessions...

Posted: Fri Jul 09, 2010 9:32 am
by eruna
You need a session_start() before you read and write the session.

Re: Sessions...

Posted: Fri Jul 09, 2010 9:32 am
by lenton
OMG, forgot the session start.. Man I feel stupid..

Thanks.