简体   繁体   中英

PHP session variable won't save

I've been having an issue with saving a PHP session variable. It's a registration form, when the user presses submit the user gets sent to registrationprocess.php. The registrationprocess.php site will either accept the input (and will later process it in a database) or it will send the user back to the registration form if the input isn't accepted as valid. It saves the registration form input, so users don't have to retype everything. Now when users enter something wrongfully, they should get an error message back that tells them what they did wrong. And that error message variable won't save.

I've created 2 new pages to narrow down the issue. They're called 'sessiontest1.php' that handles the input and 'sessiontest2.php' that handles the processing.

sessiontest1.php:

<?php
session_start();
$sessionid = session_id();
echo "Sessie ID: $sessionid";
?>

<!DOCTYPE html>
<html>
<body>

<div id="text">

<p style="font-size:150%"><b>Sessiontest1</b></p>

<?php 
$firstname = "";
$firstnameErr = "";

$firstname = input($_POST["firstname"]);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$_SESSION["firstname"] = input($_POST["firstname"]);
if (!preg_match("/^[a-zA-Z]+$/",$firstname)) {
$_SESSION["firstnameErr"] = "*Alleen letters toegestaan";
 }
}

 function input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

$test1 = "Test1";
$_SESSION["test"] = $test1;
$_SESSION["test2"] = "Test2";

?>

sessiontest2.php:

 <?php
 session_start();
 $sessionid = session_id();
 echo "Sessie ID: $sessionid";
 ?>

<!DOCTYPE html>
<html>
<body>

<h2>Sessiontest 2</h2>

<?php
$firstname = "";
$firstnameErr = "";

#$_SESSION["firstname"] = $firstname = input($_POST["firstname"]);
#$firstnameErr = $_SESSION["firstnameErr"];

function input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

if (!preg_match("/^[a-zA-Z]+$/",$_POST["firstname"])) {
#header('Location: registreer.php');
echo $_SESSION["firstnameErr"];
echo $firstnameErr;
echo "Pregmatch werkt";
#debug
}
else {
unset($_SESSION["firstname"]);
#unset($_SESSION["firstnameErr"]);
 }
?>

<br>

<?php echo "Voornaam variable" . $firstname;?><br>
<?php echo "Voornaam session variable" . $_SESSION["firstname"];?><br>
<?php echo "Voornaam post variable" . $_POST["firstname"];?><br>
<?php echo "FirstnameErr variable" . $firstnameErr;?><br>
<?php echo "FirstnameErr session variable" . $_SESSION["firstnameErr"];?>    <br>
<?php echo $_SESSION["test"];?><br>
<?php echo $_SESSION["test2"];?><br>
<?php print_r($_SESSION);?>

</body>
</html>

Now if I enter 'King' in the input form, 'sessiontest2.php' will return this:

Sessie ID: j998drlj5449e4d969rft8il72

Sessiontest 2

Voornaam variable

Voornaam session variable

Voornaam post variableKing

FirstnameErr variable

FirstnameErr session variable

Test1

Test2

Array ( [firstnameErr] => [test] => Test1 [test2] => Test2 )

And if I enter '123' in the input form, 'sessiontest2.php' will return this:

Sessie ID: j998drlj5449e4d969rft8il72

Sessiontest 2

Pregmatch werkt

Voornaam variable

Voornaam session variable

Voornaam post variable123

FirstnameErr variable

FirstnameErr session variable

Test1

Test2

Array ( [firstnameErr] => [test] => Test1 [test2] => Test2 )

So it doesn't seem to save any of the necessary session variables, but at the same time it does save the test1 and test2 session variables.

Why won't it save the necessary session variables (firstname and firstnameErr)? Any help is appreciated!

Here's the phpmyinfo(): http://i.stack.imgur.com/W5pZo.png

Use session_start() before use session

<?php
session_start();
$test = testing;
$_SESSION["firstnameErr"] = $test;
?>

session_start() once in every file you access the $_SESSION variable.

I've finally figured out the issue. The moment I press submit on the form, it'll go to sessiontest2.php and just bring along the _POST variables. The whole code I wrote in sessiontest1.php wasn't used on form input as the moment the form input was submitted it moved towards the second page. That's why the session test variables did work, they were written before the form submission and that's why the necessary session variables didn't work as the form input was never written into those variables. So, make sure that the code is structured in such a way that you don't try to process/write down stuff when the code already moved on to the next page.

您可以使用ajax查询来验证表单,只需添加表单提交处理程序即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM