Jump to content

[SOLVED] $_SESSION variables dissapear after IF statement


vajra_hendry

Recommended Posts

Well I was having such fun with passing Global variables.  Then I introduced an IF statement and it all went wrong.  Soon and the 1st part of the IF statement executes all the Session variables UNSET !!

 

Ok So I start the PHP file with this :

 

<?php

session_start();
$_SESSION['duration'] = $_POST['duration'];
$_SESSION['banner'] = $_POST['banner'];
$_SESSION['price'] = $_POST['price'];
$duration = $_SESSION['duration'];
$banner = $_SESSION['banner'];
$price = $_SESSION['price'];
?>

 

Then HTML

 

Then More PHP :

 

<?php

if(!isset($_POST['upload']))
{
        include("uploader.inc");
}
else
{
        if($_FILES['pix']['temp_name'] == "none")
{
        echo "<b>File didnt upload.  Deal with it k<br>";
        include("uploader.inc");
        exit();
}
        if(!ereg("image",$_FILES['pix']['type']))
{
        echo "<b>Hey you muppet only upload pics ok. Jesus man</b><br>";
        include("uploader.inc");
        exit();
}
else
{
        $destination = "uploads/";
        $destination = $destination . basename( $_FILES['pix']['name']);
        $temp_file = $_FILES['pix']['tmp_name'];
        move_uploaded_file($temp_file,$destination);
        echo "<p><b>Ok it worked man. File uploaded:</b>";
        echo $_FILES['pix']['name'];
        echo $_FILES['pix']['size'];
        echo "<p>ok so if your happy with that then click <a href=\"review.php\">here</a> to go to the review screen";

}
}

?>

 

So after this IF statement all the $_Session['banner']; for EG are gone!!!

 

Help!! :-)  Thanks

Side note: I've always used this in troubleshooting arrays:

foreach($_SESSION as $key=>$value){
     echo "$key => $value<br>\n";
}

 

Never knew of the "print_r" function... huh.

 

I don't have your answer but, will closing the script tag and reopening within the file (?> ... <?php) break a session? Could the server see this as 2 different scripts? Perhaps if you had a "session_start()" after re-opening the script tag?

Also, where did you attempt "print_r($_SESSION)"?

 

You got me curious and I'm grasping at straws...

So after this IF statement all the $_Session['banner']; for EG are gone!!!

 

What makes you believe that?

 

 

What makes me beleive it is that when i do $echo statements on the session variables afterwards they are empty. :-(

Ok thanks for replies here.  This is really helpful.  I tried something which had interesting results.

 

Easier if I just show a before and an after.  So before :

 

$_SESSION['duration'] = $_POST['duration'];

 

After :

 

$_SESSION['duration'] = "Some Text";

 

What was interesting was that the session variables work a treat when I make the above change.  This is however no good to me,  I need the $_POST['duration'] variable to be available, im not interested in passing a string variable that I have to manually set myself (i.e. "Some Text") .  So how do I make the value that stored in $_POST available in a session!?

Ok I did as i was told and put the following code at the end of the IF statement :

 

print_r($_SESSION);

 

Here was the result : (Just to point out that for testing purpose I have configured a $_SESSION['duration'] variable with a text string, see earlier post for details)

 

( [duration] => Some Text [banner] => [price] => ) 

 

So it seems that it works nicely but only if the $_SESSION array is populated without using other variables such as $_POST

Ok thanks.  This is my form :

 

<?php

session_start();

$duration = $_SESSION['duration'];
$banner = $_SESSION['banner'];


?>

<HTML>
<HEAD><TITLE>File Upload Screen</TITLE>
</HEAD>

<BODY>

<OL>
<LI>Enter the name of the picture you want to upload
</li></OL>

<div align="center"><hr>

<form enctype="multipart/form-data" action="steptwo.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<input type="file" name="pix" size="60">
<p><input type="submit" name="upload" value="Upload Banner">

</form>
</body>
</html>

 

This file is called 'uploader.inc' and is referenced in the above code that i posted.

 

I have been reading about sessions in PHP but still cant understand why this doesnt work

 

If its of help I can give you the URL where this is running, although I dont want to break any forum rules.  Let me know if this is appropriate.

 

I really appreciate your help.

well your not passing

duration, banner or price

 

so it can't work!

 

what are you trying to do ?

 

from your PHP code you should have something like

 

<?php

session_start();

$duration = $_SESSION['duration'];
$banner = $_SESSION['banner'];


?>

<HTML>
<HEAD><TITLE>File Upload Screen</TITLE>
</HEAD>

<BODY>

<OL>
<LI>Enter the name of the picture you want to upload
</li></OL>

<div align="center"><hr>

<form enctype="multipart/form-data" action="steptwo.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<input type="file" name="pix" size="60">
<input type="text" name="duration" size="60">
<input type="text" name="banner" size="60">
<input type="text" name="price" size="60">
<p><input type="submit" name="upload" value="Upload Banner">

</form>
</body>
</html>

OMG im tired and I Posted the wrong code.  My appologies.  Perhaps its clearer if I post the the scripts in order.

 

There are 4 files :

 

Stepone.php

Steptwo.php

Uploader.inc

Review.php

 

All I want is for varibles posted from the form in Stepone.php to be available in the subsequent php files right down the review.php

 

 

stepone.php

 

<?php

session_start();


?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Payments Page</title>
<link rel="stylesheet" href="css\payments.css">
</head>

<body>

<div id="steps">
<UL>
<li><Strong>Step One :</strong>
<ul><li>Select Duration</ul>
<li><Strong>Step Two :</strong>
<ul><li>Upload Banner</ul>
<li><Strong>step Three:</strong>
<ul><li>Review,</ul>
<li><Strong>Step Four:</strong>
<ul><li>pay</ul></UL></div>

<div id="process">
To benefit from huge traffic to your site, please follow these four simple steps.<br> Once your order is recieved we can have your advert online in no time at all</div>  

<div id="logo">
<img src="images/lts2_logo.gif"></div>

<div id="content">

<p> Thanks for choosing to advertise with **DOMAIN NAME** You will see significant increases in traffic to your site!  Just completel these simple steps:</P>


<img src="images/your_advert120x240.gif">


<div id="table">
<Table align="center" border="0">
<Caption>Select Duration of Advert</caption>
<TR>

<th>
3 Days
</th>
<th>
7 Days
</th>
<th>
14 Days
</th>
<th>
28 Days
</th>
</tr>


<TR>
<TD>
<FORM ACTION="steptwo.php" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="duration" VALUE=3>
<INPUT TYPE="HIDDEN" NAME="banner" VALUE="120 x 240 Pixels">
<INPUT TYPE="HIDDEN" NAME="price" VALUE=17>
<INPUT TYPE="image" SRC="images/Paypal/17.jpg" ALT="SUBMIT!">
</FORM>


</td>
<td>

<FORM ACTION="steptwo.php" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="duration" VALUE=7>
<INPUT TYPE="HIDDEN" NAME="banner" VALUE="120 x 240 Pixels">
<INPUT TYPE="HIDDEN" NAME="price" VALUE=27>
<INPUT TYPE="image" SRC="images/Paypal/27.jpg" ALT="SUBMIT!">
</FORM>


</td>
<td>

<FORM ACTION="steptwo.php" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="duration" VALUE=14>
<INPUT TYPE="HIDDEN" NAME="banner" VALUE="120 x 240 Pixels">
<INPUT TYPE="HIDDEN" NAME="price" VALUE=47>
<INPUT TYPE="image" SRC="images/Paypal/47.jpg" ALT="SUBMIT!">
</FORM>



</td>
<td>

<FORM ACTION="steptwo.php" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="duration" VALUE=28>
<INPUT TYPE="HIDDEN" NAME="banner" VALUE="120 x 240 Pixels">
<INPUT TYPE="HIDDEN" NAME="price" VALUE=107>
<INPUT TYPE="image" SRC="images/Paypal/107.jpg" ALT="SUBMIT!">
</FORM>

</td>
</tr>


</body>


</html>

 

steptwo.php

 

<?php

session_start();
$_SESSION['duration'] = "Some Text";/* $_POST['duration']; */
$_SESSION['banner'] = $_POST['banner'];
$_SESSION['price'] = $_POST['price'];
$duration = $_SESSION['duration'];
$banner = $_SESSION['banner'];
$price = $_SESSION['price'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Payments</title>
<link rel="stylesheet" href="css\payments.css">
</head>

<body>

<div id="logo">
<img src="images/lts2_logo.gif"></div>


<p>You have selected the following :</p>
<div id="UL1">
<UL>
<LI>To advertise with a <?php echo "$banner"; ?> banner
<LI>To advertise for <?php echo "$duration"; ?> days
<LI>This will cost $<?php echo "$price";?></UL></div>
<?php

print_r($_SESSION);

?>


<p>Now all you need to do is upload your banner and we will put it online for you</P>


<?php

if(!isset($_POST['upload']))
{
        include("uploader.inc");
}
else
{
        if($_FILES['pix']['temp_name'] == "none")
{
        echo "<b>File didnt upload.  Deal with it k<br>";
        include("uploader.inc");
        exit();
}
        if(!ereg("image",$_FILES['pix']['type']))
{
        echo "<b>Hey you muppet only upload pics ok. Jesus man</b><br>";
        include("uploader.inc");
        exit();
}
else
{
        $destination = "uploads/";
        $destination = $destination . basename( $_FILES['pix']['name']);
        $temp_file = $_FILES['pix']['tmp_name'];
        move_uploaded_file($temp_file,$destination);
        echo "<p><b>Ok it worked man. File uploaded:</b>";
        echo $_FILES['pix']['name'];
        echo $_FILES['pix']['size'];
        echo "<p>ok so if your happy with that then click <a href=\"review.php\">here</a> to go to the review screen";
print_r($_SESSION); /* Output b4 file is uploaded Array ( [duration] => Some Text [banner] => 120 x 240 Pixels [price] => 47 ) */ 

/* Output after file uploaded is : screenArray ( [duration] => Some Text [banner] => [price] => ) */

}
}

?>


</BODY>
</HTML>

 

uploader.inc

 

This is INCLUDED or called in Steptwo.php in the IF statement

 

<?php

session_start();

$duration = $_SESSION['duration'];
$banner = $_SESSION['banner'];


?>

<HTML>
<HEAD><TITLE>File Upload Screen</TITLE>
</HEAD>

<BODY>

<OL>
<LI>Enter the name of the picture you want to upload
</li></OL>

<div align="center"><hr>

<form enctype="multipart/form-data" action="steptwo.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<input type="file" name="pix" size="60">
<p><input type="submit" name="upload" value="Upload Banner">

</form>
</body>
</html>

 

review.php:

 

 

<?php
session_start();
?>

<html>

<head>
  <title></title>
</head>

<body>

<?php

$duration = $_SESSION['duration'];
$banner = $_SESSION['banner'];
$price = $_SESSION['price'];

echo "$duration";
echo "$banner";
echo "$price";

?>

</body>

</html>

 

Thats it from start to finish.  Just cant get the variables to pass after that IF statement in 'steptwo.php'.  I dont get it!!

 

/ Hey if I can get this sorted i will make a donation for sure  :)  and hopefully can give something back one day! /

 

Thanks

I think I broke the house rules and posted the whole script in.  Hence I didnt get an answer!  Fair play.

 

So this is the form as requested :

 

<TR>
<TD>
<FORM ACTION="steptwo.php" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="duration" VALUE=3>
<INPUT TYPE="HIDDEN" NAME="banner" VALUE="120 x 240 Pixels">
<INPUT TYPE="HIDDEN" NAME="price" VALUE=17>
<INPUT TYPE="image" SRC="images/Paypal/17.jpg" ALT="SUBMIT!">
</FORM>


</td>
<td>

<FORM ACTION="steptwo.php" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="duration" VALUE=7>
<INPUT TYPE="HIDDEN" NAME="banner" VALUE="120 x 240 Pixels">
<INPUT TYPE="HIDDEN" NAME="price" VALUE=27>
<INPUT TYPE="image" SRC="images/Paypal/27.jpg" ALT="SUBMIT!">
</FORM>


</td>
<td>

<FORM ACTION="steptwo.php" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="duration" VALUE=14>
<INPUT TYPE="HIDDEN" NAME="banner" VALUE="120 x 240 Pixels">
<INPUT TYPE="HIDDEN" NAME="price" VALUE=47>
<INPUT TYPE="image" SRC="images/Paypal/47.jpg" ALT="SUBMIT!">
</FORM>



</td>
<td>

<FORM ACTION="steptwo.php" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="duration" VALUE=28>
<INPUT TYPE="HIDDEN" NAME="banner" VALUE="120 x 240 Pixels">
<INPUT TYPE="HIDDEN" NAME="price" VALUE=107>
<INPUT TYPE="image" SRC="images/Paypal/107.jpg" ALT="SUBMIT!">
</FORM>

</td>
</tr>

 

 

Just to get a hand of how things work, try something simple like this and then expand...

 

<?

 

if(isset($_POST['submit']))

{

print_r($_POST); exit(0);

}

 

echo "<form method=post action=''>";

echo "<input type=text name=blah1><br><br>";

echo "<input type=text name=blah2><br><br>";

echo "<input type=submit name=submit value=submit>";

echo "</form>";

 

?>

 

 

(P.S - Using an image for a submit button can mess up $_POST['submit'], and instead cause it to submit $_POST['submit_x'] and $_POST['submit_y'] (coordinates)...  )

Well ok thanks so much for helping with this one! Got there in the end.  This was the solution that fixed it :

 

<?php

session_start();


if (!isset($_SESSION['duration'])) {
    $_SESSION['duration'] = "Some Text";
}

if (!isset($_SESSION['banner'])) {
    $_SESSION['banner'] = $_POST['banner'];
}

if (!isset($_SESSION['price'])) {
    $_SESSION['price'] = $_POST['price'];
}

$duration = $_SESSION['duration'];
$banner = $_SESSION['banner'];
$price = $_SESSION['price'];
?>

 

Once I had set the session varaibles in this way at the top of the page a they were visable from other PHP scripts.

 

Sorry to be such a dam noob.  gotta start somewhere huh.  Hope this is vaguely helpful for others

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.