7

我是 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>
4

7 回答 7

1

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

确保在 header() 之前没有打印任何内容。
确保页面开头没有 BOM 元素。

于 2013-11-04T10:19:32.453 回答
1

只需删除 main if 语句的 else 部分和放入 main 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 语句中

于 2013-12-24T13:26:42.187 回答
1

尝试使用 ob_start(); 在文件的顶部和 o​​b_end_flush(); 在文件的底部。

于 2013-11-04T10:10:48.400 回答
1

一旦控件转移到另一个页面,您必须退出代码段。否则将执行以下代码。所以exit;在 header() 之后使用。

header("Location:login.html");
exit;
于 2013-11-04T07:18:37.630 回答
1

我可能是错的,但看看你的 if/else 语句的结构,看起来你只会在请求方法不是 POST 时发送标头。(即。看起来它在其他部分。)我认为它在错误的地方。

这是我认为需要删除的其他内容。(虽然我不确定您的验证如何使用您当前的代码停止重定向)

    {$phone = test_input($_POST["phone"]);}
    }   
    别的
    {
    如果($密码!= $pass_conf){

于 2013-11-04T07:29:07.597 回答
1

用这个替换你的代码......

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");
    }
}
?>

错误是-仅当REQUESTED 方法不等于 POST时,您才执行重定向条件 。

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

于 2016-05-29T17:46:14.153 回答
0

把你的标题(“位置:login.html”);在 if ($_SERVER["REQUEST_METHOD"] == "POST") 而不是这个 if 语句的 else 部分。

于 2021-05-30T12:17:53.457 回答