Using Session Variables

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
commandercool
Forum Newbie
Posts: 2
Joined: Fri May 02, 2008 12:07 am

Using Session Variables

Post by commandercool »

Hi,

For some reason session variables aren't working for me. I checked my php info and it is on. I used the simple code on PHP's site to check if session is working, and it does not carry variables to another page. I printed session IDs and they match on both pages; just variables aren't being carried over. I'm using latest version of PHP with latest version of apache. Any ideas?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Using Session Variables

Post by Zoxive »

Code snippets of what you have would help greatly.

Always assume the code is wrong before you jump to conclusions.
commandercool
Forum Newbie
Posts: 2
Joined: Fri May 02, 2008 12:07 am

Re: Using Session Variables

Post by commandercool »

I'm just using examples from PHP's website.

Code: Select all

<?php
// page1.php
 
session_start();
 
echo 'Welcome to page #1';
 
$_SESSION['favcolor'] = 'green<br>';
$_SESSION['animal']   = 'cat<br>';
$_SESSION['time']     = time();
 
echo "<br>";
echo "session id: " . session_id();
echo "<br>";
echo $_SESSION['favcolor'];
echo $_SESSION['animal'];
echo $_SESSION['time'];
 
// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';
 
// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>
and

Code: Select all

<?php
// page2.php
 
session_start();
 
echo 'Welcome to page #2<br />';
 
echo "<br>";
echo "session id: " . session_id();
echo "<br>";
echo $_SESSION['favcolor']; // green
echo $_SESSION['animal'];   // cat
echo date('Y m d H:i:s', $_SESSION['time']);
 
// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';
?>
 
Post Reply