简体   繁体   English

PHP注册脚本不起作用

[英]PHP Registration Script Not Working

I've written a PHP registration/login system that works fine. 我已经编写了一个运行良好的PHP注册/登录系统。 However, I tried to add some server side error checking to prevent a user from signing up without entering a password, to check that the password fields match, etc. Here is my code: 但是,我尝试添加一些服务器端错误检查,以防止用户在不输入密码的情况下进行注册,检查密码字段是否匹配等。这是我的代码:

<?php

session_start();

// retrieve data via POST

$username = $_POST['username'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
$userLoc = $_POST['userLoc']; // user location is a field on the submitted form

include("config.php");

$username = mysqli_real_escape_string($conn, $username); // clean username input

// ensure that the two password fields match
if ($pass1 != $pass2) {
    header('Location: ../');
    die();
}

// ensure that the user didn't bypass  maxlength for username
if(strlen($username) > 30) {
    header('Location: ../');
    die();
}

// ensure that the user actually entered a password
if( strlen($pass1<3) || strlen($pass2<3) ) {
    header('Location: ../');
    die();
}

// check if username already taken
// I'm using a session variable that causes a div to be displayed on index.php to indicate username taken
// (I also have AJAX username check already implemented)

$query = "SELECT 1 FROM users WHERE username='$username'";
$result = mysqli_query($conn,$query);
if ( $result && mysqli_num_rows($result) > 0 ) {
    $_SESSION['usernameTaken'] = 1;
    header('Location: ../');
}

// create hash for password
$hash = hash('sha256', $pass1);

// create salt for password
function createSalt()
{
    $string = md5(uniqid(rand(), true));
    return substr($string, 0, 3);
}

$salt = createSalt();
$hash = hash('sha256', $salt . $hash);

$userLoc = mysqli_real_escape_string($conn, $userLoc);
$query = "INSERT INTO users ( username, password, salt, userLoc ) VALUES ( '$username' , '$hash' , '$salt' , '$userLoc' );";
mysqli_query($conn,$query);
mysqli_close();

header('Location: ../');

?>

Here's the problem that I can't figure out: with the die(); 这是我无法解决的问题:使用die(); statements in there, the script will in fact terminate itself if any of the conditions are met (passwords don't match, username already exists, etc.), it is properly redirected to index.php ( ../ ), and the username is not added to the database. 实际上,如果满足以下任何条件(密码不匹配,用户名已经存在等),脚本实际上将自行终止,将其正确重定向到index.php( ../ ),并且用户名未添加到数据库。 However, even if none of the error checking logic is triggered (in other words, the username is available, the passwords do match, etc.), the username will not be added to the database. 但是,即使没有触发任何错误检查逻辑(换句话说,用户名可用,密码确实匹配等),用户名也不会添加到数据库中。 The only way that I'm able to get anything to add to the database is by getting rid of every die() statement, but this makes it so none of the error checking works (for example, I can enter mismatched passwords, and the username will still be added to the database, along with the hashed pass1 ). 我能够将任何内容添加到数据库的唯一方法是摆脱每个die()语句,但这使它无法进行任何错误检查(例如,我可以输入不匹配的密码,并且用户名以及散列的pass1仍将添加到数据库中。

I think this is happening because the die() statements are being triggered even if a given if statement does not evaluate to true. 我认为发生这种情况是因为即使给定的if语句的求值结果都不为true,也会触发die()语句。 Any suggestions? 有什么建议么?

To see if you are experiencing a MySQL error, put this code after your INSERT query. 要查看您是否遇到MySQL错误,请将此代码放在INSERT查询之后。

printf("Errormessage: %s\n", $mysqli->error);

If that doesn't work, I would highly recommend putting messages inside your die() lines, because then you know exactly where the errors are coming up. 如果那行不通,我强烈建议您在die()行中放入消息,因为这样您就可以准确知道错误的出处。 Do it like this: 像这样做:

die('This error happened!');

If you have unique messages at each die() , you can tell what part of the code is causing it to stop - if any. 如果每个die()处都有唯一的消息,则可以告诉代码的哪一部分导致其停止-如果有的话。

FInally, put something like echo "FINISHED!"; 最后,输入类似echo "FINISHED!"; at the bottom of your script, so you can see if the script even gets executed all the way through. 在脚本的底部,因此您可以查看脚本是否一直被执行。

The combination of the above error-finding methods should point you in the right direction for figuring out what the problem is. 上面的错误查找方法的组合应该为您指出正确的方向,以找出问题所在。

Good luck! 祝好运!

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

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