简体   繁体   English

我的PHP注册代码有问题

[英]Something's wrong with my PHP register codes

I've been trying to create a register page using php and html and once I was finally done and wanted to check if it works, it just reloaded the page and when I checked the database nothing new was in there. 我一直在尝试使用php和html创建一个注册页面,一旦我最终完成并想检查它是否有效,它就重新加载了页面,当我检查数据库时,那里没有新内容。 Here are the codes, in register.php, 这是register.php中的代码,

?php

require_once('connect.php');

$errors = array();

if (isset($_GET['submit'])) {

if (empty($_POST['username'])) { array_push($errors, 'Choose a username.'); }
if (empty($_POST['email'])) { array_push($errors, 'Choose an email.'); }
if (empty($_POST['password'])) { array_push($errors, 'Choose a password.'); }

$old_usn = mysql_query("SELECT id FROM users WHERE name = '".$_POST['username']."' LIMIT 1;") or die(mysql_error());
if (mysql_num_rows($old_usn) > 0) { array_push($errors, 'This username is already taken.'); }

$old_email = mysql_query("SELECT id FROM users WHERE email = '".$_POST['email']."' LIMIT 1;") or die(mysql_error());
if (mysql_num_rows($old_email) > 0) { array_push($errors, 'There is an existing account with this email.'); }

if ($_POST['password1'] != $_POST['password2']) { array_push($errors, 'The password does not match'); }

if (sizeof($errors) == 0) {

//htmlentities($_POST['username'], ENT_QUOTES);
    $username = htmlentities($_POST['username'], ENT_QUOTES);
    $email = htmlentities($_POST['email'], ENT_QUOTES);
    $password1 = htmlentities(sha1($_POST['password1']), ENT_QUOTES);

    mysql_query("INSERT into users (name, hashed_psw, email, joined)
    VALUES ('{$username}', '{$password}', '{$email}', NOW());") or die(mysql_error());

    }

}



?>

and below that: 以及以下:

 <div class="container v-align-transform">
                <div class="row">
                    <div class="col-sm-6 col-sm-offset-3">
                        <div class="feature bordered text-center">
                            <h4 class="uppercase">Register Here</h4>
                            <?php

                            foreach($errors as $e) {

                                echo $e;
                                echo "<br />\n";

                                }
                            ?>
                            <form class="text-left" action="register.php" method="post">
                                <input type="text" name="username" value="" placeholder="Username" />
                                <input type="text" name="email" value="" placeholder="Email Address" />
                                <input type="password" name="password1" value="" placeholder="Password" />
                                <input type="password" name="password2" value="" placeholder="Confirm Password" />
                                <input type="submit" name="submit" value="Register" />
                            </form>
                            <p class="mb0">By signing up, you agree to our
                                <a href="/">Terms Of Use</a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>

I've tried finding what's wrong but couldn't find anything. 我试图找出问题所在,但找不到任何东西。

There's a few things wrong with this that you should consider buttoning up. 您应该考虑扣上一些错误。

  1. Don't use mysql_* functions. 不要使用mysql_*函数。 Use PDO , or at the very least, mysqli_* functions. 使用PDO或至少使用mysqli_*函数。
  2. Sanitize your SQL data. 清理您的SQL数据。 Or else. 要不然。
  3. No need to use array_push (you can use it if you want to), you can use the shorthand version: $errors[] = "New Error" 无需使用array_push (如果需要,可以使用它),可以使用简写形式: $errors[] = "New Error"
  4. Don't use sha1/md5/etc hashes for passwords. 不要使用sha1 / md5 / etc哈希作为密码。 Use salted hashes instead. 请改用盐渍的哈希值。
  5. Your form method is POST , but you're checking for $_GET ... Switch that to $_POST (including your posted variables) 您的表单方法是POST ,但是您正在检查$_GET ...将其切换到$_POST (包括您发布的变量)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM