php recording the error session but it doesn’t work

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.

New contributor

Craig Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật