2010-08-22 45 views
1

如何确保我的登录脚本安全并使其更好,这是我的第一个代码:如何改进登录脚本?

帮助是非常感谢。

<?php 

include ('../includes/db_connect.php'); 

$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 
$email = $_POST['email']; 
$mobile = $_POST['mobile']; 
$username = $_POST['username']; 
$password = md5($_POST['password']); 

// lets check to see if the username already exists 

$checkuser = mysql_query("SELECT username FROM users WHERE username='$username'"); 

$username_exist = mysql_num_rows($checkuser); 

if($username_exist > 0){ 
    echo "I'm sorry but the username you specified has already been taken. Please pick another one."; 
    unset($username); 
    header("Location: /registration?registration=false"); 
    exit(); 
} 

// lf no errors present with the username 
// use a query to insert the data into the database. 

$query = "INSERT INTO users (firstname, lastname, email, mobile, username, password) 
VALUES('$firstname', '$lastname','$email', '$mobile','$username', '$password')"; 
mysql_query($query) or die(mysql_error()); 
mysql_close(); 

echo "You have successfully Registered"; 
header("Location: /registration?registration=true"); 
// mail user their information 

//$yoursite = ‘www.blahblah.com’; 
//$webmaster = ‘yourname’; 
//$youremail = ‘youremail’; 
//  
//$subject = "You have successfully registered at $yoursite..."; 
//$message = "Dear $firstname, you are now registered at our web site. 
// To login, simply go to our web page and enter in the following details in the login form: 
// Username: $username 
// Password: $password 
//  
// Please print this information out and store it for future reference. 
//  
// Thanks, 
// $webmaster"; 
//  
//mail($email, $subject, $message, "From: $yoursite <$youremail>\nX-Mailer:PHP/" . phpversion()); 
//  
//echo "Your information has been mailed to your email address."; 

?> 
+0

询问关于PHP 37点后的问题。你说这是你的第一个代码。很奇怪。反正好,不断尝试,祝你好运。 – 2010-08-22 20:27:41

+0

但是想说些什么一旦我的哥哥告诉我,当我还是个孩子时: - 实践并不能使人完美,完美的实践使人完美.. :-) – 2010-08-22 20:29:07

回答

2

关注Artefacto关于数据库中SQL注入和哈希密码的建议。其他东西...

echo "I'm sorry but the username you specified has already been taken. Please pick another one."; 
unset($username); 
header("Location: /registration?registration=false"); 

不会工作,因为你不能回声然后发送一个头。必须在输出之前发送标题。

而且,没有一点这样做:

header("Location: /registration?registration=false"); 
echo "I'm sorry but the username you specified has already been taken. Please pick another one."; 
unset($username); 

web浏览器会马上重新定向,用户将无法看到您打印的得心应手消息。

此外,它通常要求对登记表2个密码字段柜面用户输入有误,并没有注意到,因为所有的文字是*的。你比较2,如果他们不同,你会认为是错字,然后再次提问。

+0

此外,不要通过电子邮件发送密码......它们以纯文本形式发送,并且可以被其他人阅读。 玩得开心用PHP! – James 2010-08-22 20:32:55

1

这不是一个登录脚本。这是一个注册脚本。

请参阅PHP手册中的SQL injection。你的程序容易受到这种攻击。

此外,不只是or die(mysql_error())。这将公开您可能不想公开的数据库信息(表名等)。使用适当的错误处理。例如,你可以抛出异常并定义一个未捕获的异常处理程序,它显示一个“oops”页并记录错误。

最后,使用比MD5强的散列,比如sha1

+0

'die(mysql_error())'进行调试。 – Ben 2010-08-22 20:16:15

0

正如@Artefacto所说,这不是一个登录脚本。 但是,如果你打算做一个登录脚本,我想给你一个建议。我前一阵子已经这样做了。

而不是做这样的事情的:

$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'"; 

我这样做:

$sql = "SELECT * FROM users WHERE username = '$username'"; 
$user = //use the php-sql (query, fetch_row) commands to fetch the user row. 
if (strcmp($user['password'], $password) == 0) { 
    //log in success 
} 

通过这样做,您避免在一个简单而优雅的方式SQL注入。你们有什么想法?

0

重申一下其他人提及。保护自己(以及切断)SQL注入非常重要。例如:

$checkuser = mysql_query("SELECT username FROM users WHERE username='$username'"); 

你只是简单的从$_POST['username']取值,并把它放在变量$username

有些人不是很漂亮,并会尝试打破你的程序:(所以它总是建议逃脱是从用户所采取的任何数据,将其放置到一个SQL查询之前。

例如。 ..
此:

$checkuser = mysql_query("SELECT username FROM users WHERE username='$username'"); 

变为:

$checkuser = mysql_query("SELECT username FROM users WHERE username='" .mysql_real_escape_string($username). "'");