I’m learning PHP on YouTube and I met an issue I don’t know what happened.
I’m creating a signup page and want to show the error message but the error session seems to be recorded. It stops in the signUp.inc.php as below.
signUp.inc.php
<?php
declare(strict_types=1);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$userName = $_POST["userName"];
$pwd = $_POST["pwd"];
$email = $_POST["email"];
try {
require_once "dbh.inc.php";
require_once "signUp_model.inc.php";
require_once "signUp_contr.inc.php";
// ERROR HANDLERS
$errors = [];
if (is_input_empty($userName, $pwd, $email)) {
$errors["empty_input"] = "Fill in all fields";
}
if (is_email_invaild($email)) {
$errors["invalid_email"] = "Invalid email used";
}
if (is_username_taken($pdo, $userName)) {
$errors["username_taken"] = "Username already taken";
}
if (is_email_taken($pdo, $email)) {
$errors["email_used"] = "Email already registered";
}
require_once "config_session.inc.php";
if ($errors) {
$_SESSION["errors_signUp"] = $errors;
header("Location: ../index.php");
exit();
}
} catch (PDOException $e) {
exit("Query failed: " . $e->getMessage());
}
}
else {
header("Location: ../index.php");
exit("Query failed: " . $e->getMessage());
}
I think the config_session.inc.php and the signUp_view.inc.php are working but they can’t get the message in the $_SESSION[“errors_signUp”] from signUp.inc.php so they show NULL in the index.php.
I only found this reason but couldn’t further find out why it happened and the solution. Could someone kindly tell me how to fix it, please?
config_session.inc.php
<?php
ini_set('session.use_only_cookie', 1); // https://www.php.net/manual/en/session.configuration.php
ini_set('session.use_strict_mode', 1);
session_set_cookie_params([
'lifetime' => 3600,
'domain' => 'localhost',
'path' => '/',
'secure' => true,
'httponly' => true
]);
session_start();
if(!isset($_SESSION['last_regeneration'])) {
regenerate_session_id();
} else {
$interval = 60 * 60;
if(time() - $_SESSION['last_regeneration'] >= $interval) {
regenerate_session_id();
}
}
function regenerate_session_id() {
session_regenerate_id(); //not sure it should be true
$_SESSION['last_regeneration'] = time();
}
signUp.inc.php
<?php
declare(strict_types=1);
function check_signUp_errors()
{
if(isset($_SESSION["errors_signUp"])){
$errors = $_SESSION["errors_signUp"];
echo "<br>";
foreach ($errors as $error) {
echo '<p class="form-error">' . $error . '</p>';
}
unset($_SESSION["errors_signUp"]);
}
}
index.php
<?php
require_once './includes/signUp_view.inc.php';
require_once './includes/config_session.inc.php';
// require_once './includes/signUp.inc.php';
?>
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes, minimum-scale=1.0, maximum-scale=3.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel='stylesheet' href='./style/main.css' />
<title>Document</title>
</head>
<body>
<h3>Login</h3>
<form action="includes/login.inc.php" method="post">
<input type="text" name="userName" placeholder="Username">
<input type="password" name="pwd" placeholder="Password">
<button>Login</button>
</form>
<h3>Signup</h3>
<form action="includes/signUp.inc.php" method="post">
<input type="text" name="userName" placeholder="Username">
<input type="password" name="pwd" placeholder="Password">
<input type="text" name="email" placeholder="E-Mail">
<button>Signup</button>
</form>
<?php
check_signUp_errors();
?>
</body>
</html>
Many thanks <(_ _)>
I removed header("Location: ../index.php")
in the signUp.inc.php and echo var_dump($_SESSION["errors_signUp"]);
then I can see the messages in the $_SESSION["errors_signUp"]
.
However I used <?php echo var_dump($_SESSION) ?>
in the index.php, which shows NULL.
I tried to use session_start();
to replace require_once "config_session.inc.php";
in the signUp.php but it didn’t work.
Craig Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.