开发者

PHP: store input variable in session and then echo it on a page is not working

Ok im going to try to explain this:

I have a php page "index.php" where there is a form that posts to a form.php?form=1 that processes the data and then redirects in a split second to a "thank you" page called "paso2.php"

What im trying to do is store the email variable on a session and then echo it on paso2.php

SO i did this at the very top of form.php:

<?php error_reporting(-1);
ini_set('display_errors', 1);
session_start();
// Validation here...
$_SESSION['email_address'] = $_POST['email'];

"email" is the name of the email input on index.php

Then开发者_如何学JAVA on paso2.php i did this at the very top:

<?php
error_reporting(-1);
ini_set('display_errors', 1);
session_start();?>

and then inside the input on paso2.php i echo the value

<input type="text" name="email2" value="<?php echo htmlspecialchars($_SESSION['email_address']);?>" /> 

But its not working! the error i can read inside that value is:

inside the value

<br /> <b>Notice</b>: Undefined index: email_address in     <b>/home/gulp1986/public_html/cupon0km.com/paso2.php</b> on line <b>59</b><br />

Can anyone tell me what am i doing wrong to post this value?


Use Firebug and/or HTTPFox to view the HTTP headers as your browser goes from index -> form -> paso2, and see if the session cookie (and id) are being transmitted properly at each stage. Missing vars of this sort are invariably do the session cookie being misconfigured and lost after each stage, leading a new/empty session being created each time.


To start, try adding:

session_start();
echo session_id();

in your "form.php" file. Assuming your session was started, you should see the corresponding session id.

Also if possible, modify your error reporting to: error_reporting(E_ALL); to help see if there were any errors associated with the creation of the session.


if you make a hidden field in form.php.

    <input type='hidden' name='email' value='223218' />

then post this form to paso2.php.

<form id='form_223218' class='appnitro'  method='post' action='paso2.php'>

the session variable should not be empty.


Try putting session_start() before everything else. session_start() can get a little quirky.


A followup to Marc B comment. You need to use do this before each session_start

<?php
$cookie_params = session_get_cookie_params();

session_set_cookie_params( 
    $cookie_params["lifetime"], 
    $cookie_params["path"], 
    '.cupon0km.com', 
    $cookie_params["secure"], 
    $cookie_params["httponly"] 
);

session_start();

You could also put the code above into a file and include it at the top of each page that requires a session.

Some debugging:

In form.php:

comment out the header('Location: ...') redirect

In form.php and paso2.php add this at the bottom:

echo 'session name' . session_name() . '<br>';
echo 'session id' . session_id() . '<br>';
echo 'cookies <br>'; print_r($_COOKIES);
echo 'session <br>'; print_r($_SESSION);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜