开发者

PHP Sessions Var Not Storing... Possible php.ini issues?

I'm trying to store information on a form by using sessions in case the validation returns false, then the user doesn't have to refill the form. This how I'm doing this:

PAGE A:

<?
session_start();

$Fname = $_SESSION['FirstName'];
?>
<html>
<input type="text" id="First" value="<? if($Fname){echo $Fname;}?>">
</html>

PAGE B:

<?
$Fname = $_POST['First'];

session_start();

$_SESSION['FirstName'] = $Fname;

//validation is not good then
header('Location: PageA.php');
?>

The issue is that when sent back to page A, nothing is showing up in the inputs, but randomly it will show up or, I'll refresh and it might show up. For most part its just not working and what I don't understand it kind of just started happening when I made a modification of removing one of the session vars and replacing it with a cookie because of other reasons, but decided not to go that route. Still I don't see what I could have done to start causing this issue. Any ideas? I'm thinking its something with the php.ini file because i have a separate form with the same setup and I never touched it, and now its not working when it was the last time i checked.

UPDATE

I just tried the form, I submitted it incorrectly on purpose to trigger the validation and it sent me back with blank inputs as I already mentioned. I clicked on another page and then came back to the form and the inputs appeared. It seems as if its storing but just not being r开发者_如何学编程ead immediately? Don't know if this helps.


On Page B session_start() should be the first command on the page. I always keep this at the top just to be sure


You do not need SESSION at all ...

PAGE A:

<?
  $Fname = isset($_POST['First']) ? $_POST['FirstName'] : '';
?>
<html>
  <form method='post' action='PageB.php'>
    <input type="text" id="First" value="<? if($Fname){echo $Fname;}?>"/>
  </form>
</html>

PAGE B:

<?
//validation is not good then
include 'PageA.php';
exit;
?>


Not sure if it is a typo, but in your input tag you don't have the "name" attribute specified, which would mean an empty $_POST array when you go to page B.


It's difficult (for me at least) to help you without further details, but here are my thoughts:

  1. From the PHP manual: "Session ID is not passed with Location header even if session.use_trans_sid is enabled. It must by passed manually using SID constant. "
  2. One solution found in other forums: call session_write_close() before the header redirection: external link

Apparently, header() can sometimes make the redirection BEFORE the session cookie is written. That could explain why sometimes it works and other times it doesn't. But, as I said, not sure that this applies to your code.


I got it working by filling in the things you left out in your example; does it work for you?

PageA.php

note addition of <form action="PageB.php" method="POST"> and change of id="First" to name="First"

<?
session_start();

$Fname = $_SESSION['FirstName'];
?>
<html>
<form action="PageB.php" method="POST">
<input type="text" name="First" value="<? if($Fname){echo $Fname;}?>">
</form>
</html>

PageB.php

(unchanged)

<?
$Fname = $_POST['First'];

session_start();

$_SESSION['FirstName'] = $Fname;

//validation is not good then
header('Location: PageA.php');
?>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜