php 标头(位置:)不会重定向

时间:2021-04-13 09:04:10

标签: php mysql

我正在写一个注册页面。检查凭据并将记录插入数据库后,注册页面将被重定向到注册成功页面,该页面将列出您的 user_id 并允许您使用该 user_id 登录登录页面。当我第一次编写代码时,下面的代码运行良好。但是,最近几天,当我再次执行它时,它不会重定向到注册成功页面而是登录页面。这很奇怪,因为我没有写header(位置:login.php)。我已经尝试了 stackoverflow 中的大部分建议,但仍然无法奏效。

我的 register.php 代码

session_start();
    include("connection.php");
    include("functions.php");
    
    $message = "";
    
    if ($_SERVER['REQUEST_METHOD'] == "POST") 
    {
        //something was posted
        $user_name = mysqli_real_escape_string($connection, strip_tags($_POST['user_name']));
        $email = mysqli_real_escape_string($connection, strip_tags($_POST['email']));
        $password = mysqli_real_escape_string($connection, strip_tags($_POST['password']));
        $password2 = mysqli_real_escape_string($connection, strip_tags($_POST['password2']));
        $h_password = "";
        $message = "";
        
        
            if (valid_email($email)) 
            {
                if ($password != $password2) 
                {
                    $message = "The passwords you entered do not match - please go back and try again.";
                    
                } else 
                    {
                        if ((strlen($password) < 8) ||(strlen($password) > 16))
                        {
                            $message = "Your password must between 8 to 16 characters Please go back and try again.";
                        } else 
                            {
                                
                                //save to database
                                $user_id = random_num(10);
                                $user_image = 'assets/images/profile.jpg';
                                $h_password = md5($password);
                                
                                $query = "INSERT INTO users (user_id, user_name, password, email, user_image) VALUES ('$user_id', '$user_name', '$h_password', '$email', '$user_image')";
                                        
                                $res = mysqli_query($connection, $query);
                                
                                header("Location: success.php?user_id=$user_id");
                                die;
                                
                            }
                    }   
            } else 
                {
                    $message= "That is not a valid email address.  Please go back and try again.";
                }   
    }

我的success.php代码

<?php 
session_start();
include("connection.php");
include("functions.php");

    $user_data = check_login($connection);
    
    $user_id = $_GET['user_id'];
    $q = "SELECT user_id from users where user_id = '$user_id'";
    $e = mysqli_query($connection, $q);
    $f = mysqli_fetch_assoc($e);


?>


<HTML lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="assets/stylesheets/loginNsignup.css" type="text/css">
    <title>Registration Successful!</title>
</head>

<body>
    <h1>Congrats! Your registration is successful! Now you can login!</h1>
    <p>Your user_id is <?php echo ' ' . $f['user_id'] ?></p>
    <div><a href="login.php" class="btn" style="border-radius:50%;">Login</a></div>
</body>

</HTML>

0 个答案:

没有答案
相关问题