Undefined Session Variables - just starting out

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
outtaMyBlood
Forum Newbie
Posts: 5
Joined: Sat Mar 14, 2009 3:55 am

Undefined Session Variables - just starting out

Post by outtaMyBlood »

I have tried testing the php files below, and for some reason when i view the pages in my browser (firefox & IE have the same problem) i get the following error: Notice: Undefined variable: _SESSION in C:\wamp\www\moviesite\moviesite.php on line 20.

My cookies settings in Firefox & IE are to accept cookies from everyone, including 3rd parties and to keep them until they expire. I have used test scripts to check if sessions are working on my computer (the script is in the 3rd code block) and it shows that sessions are working (i double checked the tmp folder and a cookie has been created as well). I don't think there's anything wrong with the code, but possibly some sort of setting that will not allow session variables to be defined... i'm lost. I'm using WAMP with winXP and have php5.2.9 & apache2.2.11 installed. I've tried searching the net, but can't find an answer. If anyone could help me figure out why this might be happening it would be a BIG help. Thanks.

Code: Select all

<? php
 session_start();
 $_SESSION['username'] = 'Joe12345';
 $_SESSION['authuser'] = 1;
?>
 
<html>
 <head>
  <title>Find my Favorite Movie!</title>
 </head>
 
 <body>
  <?php
   $myfavmovie = urlencode('Life of Brian');
   echo "<a href=\"moviesite.php?favmovie=$myfavmovie\">";
   echo 'Click here to see information about my favorite movie!';
   echo '</a>';
  ?>
 </body>
</html>

Code: Select all

<? php
 session_start();
 
 //check to see if user has logged in with a valid password
 if ($_SESSION['authuser'] != 1)
 {
  echo 'Sorry, but you don\'t have permission to view this page!';
  exit();
 }
?>
 
<html>
 <head>
  <title>My Movie Site - <?php echo $_GET['favmovie']; ?></title>
 </head>
 
 <body>
  <?php
   echo 'Welcome to our site, ';
   echo $_SESSION['username'];
   echo '! <br/>';
   echo 'My favorite movie is ';
   echo $_GET['favmovie'];
   echo '<br/>';
   $movierate = 5;
   echo ' My movie rating for this movie is: ';
   echo $movierate;
  ?>
 </body>
</html>

Code: Select all

<?php
session_start();     // the FIRST THING YOU DO!
 
// to see sessions "not work", comment out the "session_start();" line
 
if (!isset($_SESSION['test'])) {
  echo "First activation: setting session variable";
  $_SESSION['test'] = 1;
} else
  echo "SESSIONS ARE WORKING! activation: ", (++$_SESSION['test']);
?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Undefined Session Variables - just starting out

Post by Benjamin »

What's the output of this on moviesite.php please:

Code: Select all

 
<?php echo '<pre>' . print_r($_SESSION, true) . '</pre>'; ?>
 
outtaMyBlood
Forum Newbie
Posts: 5
Joined: Sat Mar 14, 2009 3:55 am

Re: Undefined Session Variables - just starting out

Post by outtaMyBlood »

astions wrote:What's the output of this on moviesite.php please:

Code: Select all

&nbsp;
<?php echo '<pre>' . print_r($_SESSION, true) . '</pre>'; ?>
&nbsp;
This is the output:
Welcome to our site,
Notice: Undefined variable: _SESSION in C:\wamp\www\moviesite\moviesite.php on line 20
!
My favorite movie is
Notice: Undefined index: favmovie in C:\wamp\www\moviesite\moviesite.php on line 23

My movie rating for this movie is: 5
Notice: Undefined variable: _SESSION in C:\wamp\www\moviesite\moviesite.php on line 30

I put that line of code near the bottom of my code like this:

Code: Select all

<? php
 session_start();
 
 //check to see if user has logged in with a valid password
 if ($_SESSION['authuser'] != 1)
 {
  echo 'Sorry, but you don\'t have permission to view this page!';
  exit();
 }
?>
 
<html>
 <head>
  <title>My Movie Site - <?php echo $_GET['favmovie']; ?></title>
 </head>
 
 <body>
  <?php
   echo 'Welcome to our site, ';
   echo $_SESSION['username'];
   echo '! <br/>';
   echo 'My favorite movie is ';
   echo $_GET['favmovie'];
   echo '<br/>';
   $movierate = 5;
   echo ' My movie rating for this movie is: ';
   echo $movierate;
  ?>
  
  <?php echo '<pre>' . print_r($_SESSION, true) . '</pre>'; ?>
 </body>
</html>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Undefined Session Variables - just starting out

Post by Benjamin »

Put this right after session_start() and post the output please:

Code: Select all

 
if (!isset($_SESSION)) {
    echo 'session not set<br>';
} else {
    echo '<pre>' . print_r($_SESSION, true) . '</pre>';
}
 
outtaMyBlood
Forum Newbie
Posts: 5
Joined: Sat Mar 14, 2009 3:55 am

Re: Undefined Session Variables - just starting out

Post by outtaMyBlood »

output:
'; } else { echo '

' . print_r($_SESSION, true) . '

'; } //check to see if user has logged in with a valid password if ($_SESSION['authuser'] != 1) { echo 'Sorry, but you don\'t have permission to view this page!'; exit(); } ?> Welcome to our site,
Notice: Undefined variable: _SESSION in C:\wamp\www\moviesite\moviesite.php on line 26
!
My favorite movie is
Notice: Undefined index: favmovie in C:\wamp\www\moviesite\moviesite.php on line 29

My movie rating for this movie is: 5
Notice: Undefined variable: _SESSION in C:\wamp\www\moviesite\moviesite.php on line 36

Here's my updated code:

Code: Select all

<? php
 session_start();
 
if (!isset($_SESSION)) {
echo 'session not set<br>';
} else {
echo '<pre>' . print_r($_SESSION, true) . '</pre>';
}
 
 //check to see if user has logged in with a valid password
 if ($_SESSION['authuser'] != 1)
 {
  echo 'Sorry, but you don\'t have permission to view this page!';
  exit();
 }
?>
 
<html>
 <head>
  <title>My Movie Site - <?php echo $_GET['favmovie']; ?></title>
 </head>
 
 <body>
  <?php
   echo 'Welcome to our site, ';
   echo $_SESSION['username'];
   echo '! <br/>';
   echo 'My favorite movie is ';
   echo $_GET['favmovie'];
   echo '<br/>';
   $movierate = 5;
   echo ' My movie rating for this movie is: ';
   echo $movierate;
  ?>
  
  <?php echo '<pre>' . print_r($_SESSION, true) . '</pre>'; ?>
 </body>
</html>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Undefined Session Variables - just starting out

Post by Benjamin »

Change all your <? to <?php and report back please.
outtaMyBlood
Forum Newbie
Posts: 5
Joined: Sat Mar 14, 2009 3:55 am

Re: Undefined Session Variables - just starting out

Post by outtaMyBlood »

Knew there was something wrong there, sorry bout that.

output:
Array
(
[test] => Joe1234
)


Notice: Undefined index: authuser in C:\wamp\www\moviesite\moviesite.php on line 11
Sorry, but you don't have permission to view this page!
outtaMyBlood
Forum Newbie
Posts: 5
Joined: Sat Mar 14, 2009 3:55 am

Re: Undefined Session Variables - just starting out

Post by outtaMyBlood »

you solved this problem for me pretty fast and i didn't even notice it, haha. after sleeping on it and coming back in the morning i noticed that i had the same problem with the <? php on BOTH files... wonderful how a little error like that cost me hours of confusion. Thanks a lot to astions for being so awesome and helping me out!
Post Reply