Php 验证注册表中的数据后,重定向到另一个页面以登录

Php 验证注册表中的数据后,重定向到另一个页面以登录,php,html,mysql,forms,post,Php,Html,Mysql,Forms,Post,我不熟悉php和mysql。我正在尝试创建一个用户注册表单,验证数据,然后重定向到一个登录页面,在那里他们可以输入用户名和密码进行登录。我已经编写了代码,但由于某些原因,我无法正确使用header()。 以下是我目前的代码: $userErr= $passErr= $passErrc= $firstErr= $lastErr= $middle= $addErr= $cityErr= $stateErr=$zipErr= $emailErr= $phoneErr= $passMatchE

我不熟悉php和mysql。我正在尝试创建一个用户注册表单,验证数据,然后重定向到一个登录页面,在那里他们可以输入用户名和密码进行登录。我已经编写了代码,但由于某些原因,我无法正确使用header()。 以下是我目前的代码:
$userErr= $passErr= $passErrc= $firstErr= $lastErr= $middle= $addErr= $cityErr=      $stateErr=$zipErr= $emailErr= $phoneErr= $passMatchErr="";

$userID= $password= $pass_conf= $firstName= $lastName= $middle= $address= $city= $state= $zip= $email= $phone="";


// Validate the form
// use trim() function to remove unnecessary characters such as extra space, tab, newline,
// use stripslashes(() to remove backslashes 
// use htmlspecialchars() for security
function test_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}


if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    // validating the form to see all required fields are entered
    if (empty($_POST["userID"]))
    {
        $userErr = "User ID is required";
    }
    else
    {
        $userID = test_input($_POST["userID"]);
    }

    if (empty($_POST["password"]))
    {
        $passErr = "Password is required";
    }
    else
    {
        $password = test_input($_POST["password"]);
    }
    if (empty($_POST["pass_conf"]))
    {
        $passErrc = "Confirm your password";
    }
    else
    {
        $pass_conf = test_input($_POST["pass_conf"]);
    }

    if (empty($_POST["firstName"]))
    {
        $firstErr = "First Name is required";
    }
    else
    {
        $firstName = test_input($_POST["firstName"]);
    }

    if (empty($_POST["lastName"]))
    {
        $lastErr = "Last Name is required";
    }
    else
    {
        $lastName = test_input($_POST["lastName"]);
    }
    if (empty($_POST["middle"]))
    {
        $middle = "";
    }
    else
    {
        $middle= test_input($_POST["middle"]);
    }
    if (empty($_POST["address"]))
    {
        $addErr = "Address is required";
    }
    else
    {
        $address = test_input($_POST["address"]);
    }
    if (empty($_POST["city"]))
    {
        $cityErr = "City is required";
    }
    else
    {
        $city = test_input($_POST["city"]);
    }
    if (empty($_POST["state"]))
    {
        $stateErr = "State is required";
    }
    else
    {
        $state = test_input($_POST["state"]);
    }
    if (empty($_POST["zip"]))
    {
        $zipErr = "Zip is required";
    }
    else
    {
        $zip = test_input($_POST["zip"]);
    }
    if (empty($_POST["email"]))
    {
        $emailErr = "Email is required";
    }
    else
    {
        $email = test_input($_POST["email"]);
    }
    if (empty($_POST["phone"]))
    {
        $phoneErr = "";
    }
    else
    {
        $phone = test_input($_POST["phone"]);
    }
}   
else
{
    if($password != $pass_conf){
        $passMathErr= "Passwords do not match. Please, go back and re-enter the passwords!";
        // die($passMathErr);
    } else{
        // perform sql query to insert the data
        $sql="insert into users values('$userID', '$password', '$firstName', '$lastName', '$middle', '$address', '$city', '$state', '$zip', '$email', '$phone')";
        $result=mysql_query($sql, $connection);
        header("Location:login.html");
    }
}
?>  

<html lang="em">
<head>
    <title> Registration </title>
    <style type="text/css">
        h1{
            text-align: left;
            font-weight:bold;
            font-size: 2em;
            color:#FFFF99;
            word-spacing: 0.3em;
            letter-spacing:0.1em;
            text-decoration:underline;
        }
        body{
            background-color: #421818;
        }
        .txtinput{
            margin-left:150px;
        }
        table{
            font-color:#99FF00;
        }
        .error {
            color: #FF0000;
        }
    </style>
</head>
<body>
    <h1> Registration Form </h1><br>
    <form name="reg" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
        <table style="color:#99FF00" border=0 cellspacing=0 cellpadding=2>
            <tr>
                <td>User ID * <td><input type="text" name="userID"/>
                <span class="error"><?php echo $userErr;?></span>
            </tr>
            <tr>
                <td>Password *<td><input type="password" name="password" />
                <span class="error"><?php echo $passErr;?></span>
            </tr>
            <tr>
                <td>Confirm Password *<td><input type="password" name="pass_conf" />
                <span class="error"><?php echo $passErrc;?></span>
            </tr>
            <tr>
                <td>First Name *<td><input type="text" name="firstName" />
                <span class="error"><?php echo $firstErr;?></span>
            </tr>
            <tr>
                <td>Last Name *<td><input type="text" name="lastName" />
                <span class="error"><?php echo $lastErr;?></span>
            </tr>
            <tr>
                <td>Middle<td><input type="text" name="middle" />
                <span class="error"><?php echo $middle;?></span>
            </tr>
            <tr>
                <td>Address *<td><input type="text" name="address" />
                <span class="error"><?php echo $addErr;?></span>
            </tr>
            <tr>
                <td>City *<td><input type="text" name="city" />
                <span class="error"><?php echo $cityErr;?></span>
            </tr>
            <tr>
                <td>State *<td><input type="text" name="state" />
                <span class="error"><?php echo $cityErr;?></span>
            </tr>
            <tr>
                <td>Zip *<td><input type="text" name="zip" />
                <span class="error"><?php echo $zipErr;?></span>
            </tr>
            <tr>
                <td>Email *<td><input type="text" name="email"/> 
                <span class="error"><?php echo $emailErr;?></span>
            </tr>
            <tr>
                <td>Phone<td><input type="text" name="phone" />
                <span class="error"><?php echo $phone;?></span>
            </tr>
        </table>
        <div class="txtinput">
            <input type="submit" name="submit" value="Register"/>   
        </div>
    </form>
</body>
</html>
$userErr=$passer=$passErrc=$firstErr=$lastErr=$middle=$addErr=$cityErr=$stateErr=$zipErr=$emailErr=$phoneErr=$passMatchErr=”“;
$userID=$password=$pass\u conf=$firstName=$lastName=$middle=$address=$city=$state=$zip=$email=$phone=“”;
//验证表单
//使用trim()函数删除不必要的字符,例如额外的空格、制表符、换行符、,
//使用斜杠(()删除反斜杠
//使用htmlspecialchars()实现安全性
功能测试输入($data)
{
$data=修剪($data);
$data=条带斜杠($data);
$data=htmlspecialchars($data);
返回$data;
}
如果($\服务器[“请求\方法”]=“发布”)
{
//正在验证表单以查看是否输入了所有必填字段
if(空($\u POST[“userID”]))
{
$userErr=“需要用户ID”;
}
其他的
{
$userID=test_输入($_POST[“userID]”);
}
如果(空($_POST[“password”]))
{
$passer=“需要密码”;
}
其他的
{
$password=test_输入($_POST[“password”]);
}
if(空($\u POST[“pass\u conf”]))
{
$passErrc=“确认您的密码”;
}
其他的
{
$pass\u conf=测试输入($\u POST[“pass\u conf”]);
}
如果(空($_POST[“firstName”]))
{
$firstErr=“需要名字”;
}
其他的
{
$firstName=test_输入($_POST[“firstName”]);
}
if(空($_POST[“lastName”]))
{
$lastErr=“姓氏是必需的”;
}
其他的
{
$lastName=test_输入($_POST[“lastName”]);
}
如果(空($_POST[“middle”]))
{
$middle=“”;
}
其他的
{
$middle=测试输入($\u POST[“middle”]);
}
如果(空($_POST[“address”]))
{
$addErr=“地址是必需的”;
}
其他的
{
$address=测试输入($\ POST[“地址]);
}
如果(空($_POST[“city”]))
{
$cityErr=“需要城市”;
}
其他的
{
$city=测试输入($\u POST[“城市]);
}
如果(空($_POST[“state”]))
{
$stateErr=“状态是必需的”;
}
其他的
{
$state=test_输入($_POST[“state”]);
}
如果(空($_POST[“zip”]))
{
$zipErr=“Zip是必需的”;
}
其他的
{
$zip=test_输入($_POST[“zip”]);
}
如果(空($_POST[“email”]))
{
$emailErr=“需要电子邮件”;
}
其他的
{
$email=test_输入($_POST[“email”]);
}
如果(空($_POST[“phone”]))
{
$phoneErr=“”;
}
其他的
{
$phone=test_输入($_POST[“phone”]);
}
}   
其他的
{
如果($password!=$pass\u conf){
$passMathErr=“密码不匹配。请返回并重新输入密码!”;
//死($passMathErr);
}否则{
//执行sql查询以插入数据
$sql=“在用户值中插入(“$userID”、“$password”、“$firstName”、“$lastName”、“$middle”、“$address”、“$city”、“$state”、“$zip”、“$email”、“$phone”)”;
$result=mysql\u查询($sql,$connection);
标题(“Location:login.html”);
}
}
?>  
登记处
h1{
文本对齐:左对齐;
字体大小:粗体;
字号:2em;
颜色:#FFFF99;
字距:0.3em;
字母间距:0.1米;
文字装饰:下划线;
}
身体{
背景色:#421818;
}
.txtinput{
左边距:150像素;
}
桌子{
字体颜色:#99FF00;
}
.错误{
颜色:#FF0000;
}
登记表

控件转移到另一页后,必须退出代码段。否则将执行以下代码。因此,请使用
exit;
after header()


我可能是错的,但从if/else语句的结构来看,似乎只有在请求方法不是POST时才会发送头(即,看起来像是在else部分)。我认为它位于错误的位置

我认为需要删除的就是这个(尽管我不确定您的验证如何使用当前代码停止重定向)

{$phone=test_输入($_POST[“phone”]);} } 其他的 { 如果($password!=$pass\u conf){
尝试在文件顶部使用ob_start();在文件底部使用ob_end_flush()。

好的,您说过数据正常进入数据库,但标头()无法重定向。 发生这种情况的唯一方法是在header()发生之前回显一些字符

确保在页眉()之前未打印任何内容。

确保页面开头没有BOM元素。

只需删除主if语句的else部分和主if语句中的密码确认部分:

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
  some coce
}
//""""
else
{
    if($password != $pass_conf){
        $passMathErr= "Passwords do not match. Please, go back and re-enter the passwords!";
        // die($passMathErr);
    } else{
        $sql="insert into users values('$userID', '$password', '$firstName', '$lastName', '$middle', '$address', '$city', '$state', '$zip', '$email', '$phone')";
        $result=mysql_query($sql, $connection);
        header("Location:login.html");
    }
} //"""
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
  some coce
}
//""""
else
{
    if($password != $pass_conf){
        $passMathErr= "Passwords do not match. Please, go back and re-enter the passwords!";
        // die($passMathErr);
    } else{
        // perform sql query to insert the data
        $sql="insert into users values('$userID', '$password', '$firstName', '$lastName', '$middle', '$address', '$city', '$state', '$zip', '$email', '$phone')";
        $result=mysql_query($sql, $connection);
        header("Location:login.html");
    }
} //"""
        // perform sql query to insert the data

删除上面的“”部分并将其添加到main if语句中

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    // validating the form to see all required fields are entered
    if (empty($_POST["userID"]))
    {
        $userErr = "User ID is required";
    }
    else
    {
        $userID = test_input($_POST["userID"]);
    }

    if (empty($_POST["password"]))
    {
        $passErr = "Password is required";
    }
    else
    {
        $password = test_input($_POST["password"]);
    }
    if (empty($_POST["pass_conf"]))
    {
        $passErrc = "Confirm your password";
    }
    else
    {
        $pass_conf = test_input($_POST["pass_conf"]);
    }

    if (empty($_POST["firstName"]))
    {
        $firstErr = "First Name is required";
    }
    else
    {
        $firstName = test_input($_POST["firstName"]);
    }

    if (empty($_POST["lastName"]))
    {
        $lastErr = "Last Name is required";
    }
    else
    {
        $lastName = test_input($_POST["lastName"]);
    }
    if (empty($_POST["middle"]))
    {
        $middle = "";
    }
    else
    {
        $middle= test_input($_POST["middle"]);
    }
    if (empty($_POST["address"]))
    {
        $addErr = "Address is required";
    }
    else
    {
        $address = test_input($_POST["address"]);
    }
    if (empty($_POST["city"]))
    {
        $cityErr = "City is required";
    }
    else
    {
        $city = test_input($_POST["city"]);
    }
    if (empty($_POST["state"]))
    {
        $stateErr = "State is required";
    }
    else
    {
        $state = test_input($_POST["state"]);
    }
    if (empty($_POST["zip"]))
    {
        $zipErr = "Zip is required";
    }
    else
    {
        $zip = test_input($_POST["zip"]);
    }
    if (empty($_POST["email"]))
    {
        $emailErr = "Email is required";
    }
    else
    {
        $email = test_input($_POST["email"]);
    }
    if (empty($_POST["phone"]))
    {
        $phoneErr = "";
    }
    else
    {
        $phone = test_input($_POST["phone"]);
    }
    if($password != $pass_conf){
        $passMathErr= "Passwords do not match. Please, go back and re-enter the passwords!";
        // die($passMathErr);
    } else{
        // perform sql query to insert the data
        $sql="insert into users values('$userID', '$password', '$firstName', '$lastName', '$middle', '$address', '$city', '$state', '$zip', '$email', '$phone')";
        $result=mysql_query($sql, $connection);
        header("Location: login.html");
    }
}
?>
错误是-只有当 请求的方法不等于POST


因此,如果请求方法是POST,则执行上面的代码&输入的密码与确认密码匹配,然后将用户重定向到登录页面。现在就可以了!

将标题(“Location:login.html”);放入if($\u SERVER[“request\u method”]=“POST”)而不是此if语句的其他部分。

位置后空格:可能?我尝试过,数据被插入到数据库中,但它仍然没有指向我的登录页。它会一直显示注册文件号。但是在头命令之前的任何输出都会显示。请检查是否没有空格
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
  some coce
}
//""""
else
{
    if($password != $pass_conf){
        $passMathErr= "Passwords do not match. Please, go back and re-enter the passwords!";
        // die($passMathErr);
    } else{
        $sql="insert into users values('$userID', '$password', '$firstName', '$lastName', '$middle', '$address', '$city', '$state', '$zip', '$email', '$phone')";
        $result=mysql_query($sql, $connection);
        header("Location:login.html");
    }
} //"""
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
  some coce
}
//""""
else
{
    if($password != $pass_conf){
        $passMathErr= "Passwords do not match. Please, go back and re-enter the passwords!";
        // die($passMathErr);
    } else{
        // perform sql query to insert the data
        $sql="insert into users values('$userID', '$password', '$firstName', '$lastName', '$middle', '$address', '$city', '$state', '$zip', '$email', '$phone')";
        $result=mysql_query($sql, $connection);
        header("Location:login.html");
    }
} //"""
        // perform sql query to insert the data
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    // validating the form to see all required fields are entered
    if (empty($_POST["userID"]))
    {
        $userErr = "User ID is required";
    }
    else
    {
        $userID = test_input($_POST["userID"]);
    }

    if (empty($_POST["password"]))
    {
        $passErr = "Password is required";
    }
    else
    {
        $password = test_input($_POST["password"]);
    }
    if (empty($_POST["pass_conf"]))
    {
        $passErrc = "Confirm your password";
    }
    else
    {
        $pass_conf = test_input($_POST["pass_conf"]);
    }

    if (empty($_POST["firstName"]))
    {
        $firstErr = "First Name is required";
    }
    else
    {
        $firstName = test_input($_POST["firstName"]);
    }

    if (empty($_POST["lastName"]))
    {
        $lastErr = "Last Name is required";
    }
    else
    {
        $lastName = test_input($_POST["lastName"]);
    }
    if (empty($_POST["middle"]))
    {
        $middle = "";
    }
    else
    {
        $middle= test_input($_POST["middle"]);
    }
    if (empty($_POST["address"]))
    {
        $addErr = "Address is required";
    }
    else
    {
        $address = test_input($_POST["address"]);
    }
    if (empty($_POST["city"]))
    {
        $cityErr = "City is required";
    }
    else
    {
        $city = test_input($_POST["city"]);
    }
    if (empty($_POST["state"]))
    {
        $stateErr = "State is required";
    }
    else
    {
        $state = test_input($_POST["state"]);
    }
    if (empty($_POST["zip"]))
    {
        $zipErr = "Zip is required";
    }
    else
    {
        $zip = test_input($_POST["zip"]);
    }
    if (empty($_POST["email"]))
    {
        $emailErr = "Email is required";
    }
    else
    {
        $email = test_input($_POST["email"]);
    }
    if (empty($_POST["phone"]))
    {
        $phoneErr = "";
    }
    else
    {
        $phone = test_input($_POST["phone"]);
    }
    if($password != $pass_conf){
        $passMathErr= "Passwords do not match. Please, go back and re-enter the passwords!";
        // die($passMathErr);
    } else{
        // perform sql query to insert the data
        $sql="insert into users values('$userID', '$password', '$firstName', '$lastName', '$middle', '$address', '$city', '$state', '$zip', '$email', '$phone')";
        $result=mysql_query($sql, $connection);
        header("Location: login.html");
    }
}
?>