Session Variable

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
ftsoft
Forum Newbie
Posts: 3
Joined: Sat Apr 12, 2008 11:25 am

Session Variable

Post by ftsoft »

Passing date as session variable to another page doesn't seem to work. I have the following code

page1

<?php
$theDate = isset($_REQUEST["date2"]) ? $_REQUEST["date2"] : "";

$_SESSION["mySessionDateVariable"] = $theDate;
$myDate=$_SESSION["mySessionDateVariable"];
?>

<div align="center">
<input name="tvar" type="text" value="<?=$myDate ?>" id="tvar" />

page2

<?php
$user=$_SESSION['MM_Username'];
$aDate = $_SESSION['mySessionDateVariable'];

?>
<body class="oneColElsCtr">
<h1><a href="http://www.frankcooley.com"><img src="images/Banner.gif" width="753" height="235" /></a></h1>
<h1>Enter a Ride for This Date</h1>
<input type="text" name="temp" value="<?=$theDate ?>" size="32" />

The first text box prints the date. the second is blank. If I substitute a string like "04-12-2008" for the mySessionDateVariable on the page1, it works in both instances. I'm only a sometime php user, so I am a little confused.

Frank
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Session Variable

Post by EverLearning »

You need to use session_start() in both files for sessions to work. Put it at the beggining of the file.

Code: Select all

<?php
session_start();
//rest of your code....
ftsoft
Forum Newbie
Posts: 3
Joined: Sat Apr 12, 2008 11:25 am

Re: Session Variable

Post by ftsoft »

Thanks for the reply. Yes, I have session start in both files. Otherwise it wouldn't work with the string, which it does. If I define the variable as $_SESSION["mySessionDateVariable"] = "04-12-2008" instead of $_SESSION["mySessionDateVariable"] = $theDate; then it works. I would suspect the $theDate value, but it prints out correctly using the $myDate variable on the first page.

Frank
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Session Variable

Post by EverLearning »

Code: Select all

$theDate = isset($_REQUEST["date2"]) ? $_REQUEST["date2"] : "";
it expects to find some value in $_POST['date2'] or $_GET['date2']. Are you accessing the page1.php with url like page1.php?date2=04-12-2008 or submiting some form data to that page? If not $_REQUEST['date2'] will be empty.
Echo the value of $theDate right after the assignement to confirm its value.
ftsoft
Forum Newbie
Posts: 3
Joined: Sat Apr 12, 2008 11:25 am

Re: Session Variable

Post by ftsoft »

I'm sorry, I haven't made myself clear. the code

$theDate = isset($_REQUEST["date2"]) ? $_REQUEST["date2"] : "";

$_SESSION["mySessionDateVariable"] = $theDate;
$myDate=$_SESSION["mySessionDateVariable"];
?>

<div align="center">

<form name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
<input name="tvar" type="text" value="<?=$myDate ?>" id="tvar" />

on the first page does what you suggest and works correctly, indicating that $theDate does have a value and the session variable contains it. Why it doesn't get to the second page, but DOES get to the second page if it's just set as a string is what I can't fathom. I'm perfectly willing to admit that I may be overlooking something quite basic though.

Frank
Post Reply