Page 1 of 1

Session Variable

Posted: Sat Apr 12, 2008 11:33 am
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

Re: Session Variable

Posted: Sat Apr 12, 2008 11:45 am
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....

Re: Session Variable

Posted: Sat Apr 12, 2008 12:05 pm
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

Re: Session Variable

Posted: Sat Apr 12, 2008 12:28 pm
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.

Re: Session Variable

Posted: Sat Apr 12, 2008 12:34 pm
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