You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP注册功能正常但登录功能失效问题求助

单页账户表单登录验证跳转异常问题

我制作了包含注册与登录功能的单页式账户表单,注册表单可正常与数据库交互完成用户注册,但登录功能无法完成验证跳转。以下是相关代码:

uaccount.php(表单页面)

<form action="uregistration.php" method="post">
    <!--SIGN UP FORM HERE-->
    <h1>Create Account</h1>
    <input type="text" id="firstname" name="firstname" placeholder="First Name" required>
    <input type="text" id="lastname" name="lastname" placeholder="Last Name" required>
    <input type="email" id="email" name ="email" placeholder="Email" required>
    <input type="password" id="password" name="password" placeholder="Password" required>
    <input type="date" id="birthdate" name="birthdate" placeholder="Birthdate" required>
    <input type="text" id="contact" name="contact" placeholder="Contact No." maxlength = "11" required>
    <select type= "text" id="gender" name="gender" placeholder="Gender" required>
        <option selected disabled> Select your gender </option>
        <option value="male">Male</option>
        <option value="female">Female</option>
        <option value="others">Others</option>
    </select>
<button type="submit" id="register" name= "register">Sign Up</button>
</form>
</div>

<a href="index.php"> <i class="fa fa-times-circle-o"></i> </a>
<form action= "uvalidation.php" method="post">
    <!--SIGN IN FORM HERE-->
    <h1> Sign in </h1>
    <input type="email" id="email" name ="email" placeholder="Email" required>
    <input type="password" id="password" name="password" placeholder="Password" required>
    <a href ="#">Forgot your Password?</a>
    <button type="submit" id="login" name= "login">Sign In</button>
</form>
</div>

</body>
</html>

uregistration.php(注册数据库逻辑)

<?php
session_start();
header('Location: uaccount.php');

$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'castro_rentals');

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$password = sha1($_POST['password']);
$birthdate = $_POST['birthdate'];
$contact = $_POST['contact'];
$gender = $_POST['gender'];

$s = " SELECT * FROM user WHERE email = '$email'";
$result = mysqli_query($con, $s);
$num = mysqli_num_rows($result);

if ($num == 1) {
    echo "Email already taken!";
}else{
    $reg = " insert into user
                            (firstname, lastname, email, password, birthdate, 
                            contact, gender) 
                    VALUES ('$firstname' , '$lastname' , '$email' , 
                            '$password' , '$birthdate' , '$contact' , 
                            '$gender')";
    mysqli_query($con, $reg);
    echo " Registration Successful!";
}

uvalidation.php(登录验证逻辑)

<?php
session_start();
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'castro_rentals');

$email = $_POST['email'];
$password = sha1($_POST['password']);

$s = " SELECT * FROM user WHERE email = '$email' AND password = '$password'";
$result = mysqli_query($con, $s);
$num = mysqli_num_rows($result);
if ($num == 1) {
    header('Location: index.php');
}else{
    header('Location: uaccount.php');
}

index.php(登录后跳转页面)

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
    
    <title> Home | Castro Rentals </title>
    
</head>
<body>
    Welcome!!
</body>
</html>

已尝试的操作

  • 将header中的location首字母大写
  • 去除header Location中的空格
  • 删除PHP闭合标签?>

内容的提问来源于stack exchange,提问作者definitelynotaprogrammer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 16:21:52