草庐IT

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

coder 2023-08-07 原文

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

最佳答案

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

header("Location:login.html");
exit;

关于php - 验证注册表中的数据后,重定向到另一个页面以登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19763174/

有关php - 验证注册表中的数据后,重定向到另一个页面以登录的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  4. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  5. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  6. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  7. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  8. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  9. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  10. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

随机推荐