Sessions...

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
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Sessions...

Post 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.
Skiddles2010
Forum Newbie
Posts: 19
Joined: Tue Jul 06, 2010 11:05 pm

Re: Sessions...

Post 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.
Last edited by Skiddles2010 on Fri Jul 09, 2010 9:35 am, edited 1 time in total.
eruna
Forum Newbie
Posts: 17
Joined: Mon Jun 28, 2010 2:02 pm

Re: Sessions...

Post by eruna »

You need a session_start() before you read and write the session.
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Re: Sessions...

Post by lenton »

OMG, forgot the session start.. Man I feel stupid..

Thanks.
Post Reply