post / redirect / get - 如何取消设置会话变量

时间:2017-08-20 06:47:47

标签: php session registration

Bellow是register.php

代码的一部分
if($_SERVER['REQUEST_METHOD'] == "POST") {

  /* running  some checks of an input*/

 if(sizeof($errorArray)==0) {
    // redirecting to avoid form resubmission
    $_SESSION['registered'] = true;
    header('location: success.php',true,303);
    }

else{
    $_SESSION['post']['email'] = $_POST['email'];
    $_SESSION['post']['name'] = $_POST['name'];
    $_SESSION['errorArray'] = $errorArray;
    header('location: register.php',true,303);
    }
}

逻辑很简单 - 如果errorArray为空,则重定向到成功页面,否则重定向到register.php本身。为了避免重新提交表单,我尝试将post变量放入会话变量中,因此用户在出现错误时不必再次填写表单。

<?php
            if (isset($_SESSION['errorArray'])) {
                if (sizeof($_SESSION['errorArray']) != 0) {
                    echo '<div class="alert alert-danger" role="alert">
                    <h4>There were errors in Your input:</h4>';
                    foreach ($_SESSION['errorArray'] as $item) {
                        echo $item . '<br>';
                    }
                }
                $_SESSION['post'] = null;
                $_SESSION['errorArray'] = null;
            }
 ?>

这段代码稍后会在register.php中执行,但是它会让我得到我想要的结果。不知何故,变量在执行上面的循环之前设置为null(??!)。 I have found a solution with get method包含传递给标题的url中的microtime,但它告诉我有更优雅的解决方案,并不是每次都会向会话变量添加新值。

有什么方法吗?

编辑:

    <?php
session_start();
if ($_SERVER['REQUEST_METHOD'] == "POST") {

    // array to hold all the errors of input
    $errorArray = [];
    $emailRegex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
    $nameRegex = '/^[a-z0-9][a-z0-9_]*[a-z0-9]$/';
    $passwordRegex = '/^[a-z0-9][-a-z0-9_!@#$?]*[a-z0-9]$/';

    $email = $_POST['email'];
    if (empty($email)) {
        array_push($errorArray, "E-mail field required");
    } else {
        if (!preg_match($emailRegex, $email)) array_push($errorArray, 'Invalid email');
    }
    $name = $_POST['name'];
    if (empty($name)) {
        array_push($errorArray, "Name field required");
    } else {
        if (!preg_match($nameRegex, $name)) array_push($errorArray, 'Invalid name');
    }

    $password = $_POST['password'];
    $passwordR = $_POST['passwordR'];


    if (empty($passwordR) || empty($password)) {
        array_push($errorArray, 'Password fields required');
    } else if (!preg_match($passwordRegex, $password)) {
        array_push($errorArray, 'Invalid password');
    } else {
        if ($password !== $passwordR) {
            array_push($errorArray, 'Password inputs are not the same');
        }
    }

    if (sizeof($errorArray) == 0) {
        // redirecting to avoid form resubmission
        $_SESSION['registered'] = true;
        header('location: success.php', true, 303);
    } else {
        $_SESSION['post']['email'] = $_POST['email'];
        $_SESSION['post']['name'] = $_POST['name'];
        $_SESSION['errorArray'] = $errorArray;
        header('location: register.php', true, 303);
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Log</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css">
    <link rel="stylesheet" href="css/maincss.css" type="text/css">
    <meta name="viewport" content="width = device-width, initial-scale = 1, user - scalable = no">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
</head>
<body>

<nav class="navbar navbar-default navbar-fixed">
    <div class="container">
        <a href="index.php" class="navbar-brand col-xs-5">Home</a>
        <ul class="navbar-brand col-xs-5"><?= (isset($username)) ? 'Welcome ' . $username : ''; ?></ul>
        <a href="login.php" class="navbar-brand col-xs-1">Login</a>
        <a href="register.php" class="navbar-brand col-xs-1">Register</a>
    </div>
</nav>


<div class="container">

    <form class="form-signin" action="register.php" method="post">
        <h2 class="form-signin-heading text-center text-capitalize">Registration form</h2>
        <!-- I have purposely excluded required attribute from inputs and set type="text"
        for an email so all the checks could be done on server side -->
        <div class="row center-block">
            <div class="col-xs-4"></div>
            <div class="col-xs-4">
                <label for="email" class="sr-only">Email address</label>
                <input type="text" id="email" name="email" class="form-control" placeholder="Email address"
                       value="<?= (isset($_SESSION['post']['email'])) ? $_SESSION['post']['email'] : ''; ?>" autofocus>
            </div>
            <div class="col-xs-4"></div>
        </div>
        <br>
        <div class="row center-block">
            <div class="col-xs-4"></div>
            <div class="col-xs-4">
                <label for="name" class="sr-only">Name</label>
                <input type="text" id="name" name="name" class="form-control" placeholder="Name"
                       value="<?= (isset($_SESSION['post']['name'])) ? $_SESSION['post']['name'] : ''; ?>" autofocus>
            </div>
            <div class="col-xs-4"></div>
        </div>
        <br>
        <div class="row center-block">
            <div class="col-xs-4"></div>
            <div class="col-xs-4">
                <label for="password" class="sr-only">Password</label>
                <input type="password" id="password" name="password" class="form-control" placeholder="Password">
            </div>
            <div class="col-xs-4"></div>
        </div>
        <br>
        <div class="row center-block">
            <div class="col-xs-4"></div>
            <div class="col-xs-4">
                <label for="passwordR" class="sr-only">Repeat Password</label>
                <input type="password" id="passwordR" name="passwordR" class="form-control"
                       placeholder="Repeat Password">
            </div>
            <div class="col-xs-4"></div>
        </div>
        <br>
        <div class="row">
            <div class="col-xs-4"></div>
            <div class="col-xs-4 center-block">
                <button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
            </div>
            <div class="col-xs-4"></div>
        </div>
        <br>
        <div>
            <?php
            if (isset($_SESSION['errorArray'])) {
                if (sizeof($_SESSION['errorArray']) != 0) {
                    echo '<div class="alert alert-danger" role="alert">
                    <h4>There were errors in Your input:</h4>';
                    foreach ($_SESSION['errorArray'] as $item) {
                        echo $item . '<br>';
                    }
                }
                // $_SESSION['post'] = null;
                // $_SESSION['errorArray'] = null;
            }
            ?>
        </div>

    </form>
</body>
</html>

0 个答案:

没有答案