2011-09-07 175 views
0

我正在为我的业务设置一个博客类型页面。全新的MySQL和PHP。设置这个登录系统。出于某种原因,不知道为什么登录正在下降。假设检查错误,然后通过php返回'好',如果电子邮件和密码是正确的。如果PHP返回好,那么它就会重定向到博客页面。Jquery/PHP ajax登录系统

处理这个几个月需要绝望的帮助。谢谢。 这里是跟随jQuery的php代码。

链接到测试网站在这里。 test.toddprod.com/login 真的会很感激帮助。 感谢

<?php 
#fake mysql connection first 
DEFINE ('DB_USER','usernamegoeshere'); 
DEFINE ('DB_PASSWORD','passwordhere'); 
DEFINE ('DB_HOST','hostnamehere'); 
DEFINE ('DB_NAME','andtheotherthinghere'); 

$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL'); 

$db = mysql_select_db(DB_NAME, $dbc) or die('Could not select database.'.mysql_error()); 


$e = $_POST['email']; 
$pass = $_POST['pass']; 
$q = 'SELECT user_id from toddprod where email="'.$e.'" and pass= SHA1("'.$pass.'")'; 
$r = mysql_query($db, $q); 
if(mysql_num_rows($r)==1){ 
    setcookie ('user_id', $r); 
    setcookie ('email', '$e'); 
    setcookie ('logged-in', 'true'); 
    echo 'good'; 
    } 
else if (mysql_num_rows($r)==0) { 
    echo 'Your '.$e.' with password '.$pass; 
}; 
mysql_close ($db); 
?> 
+0

因为我是一个很好的人,我不会毁掉你的数据库,我真的希望它不包含任何重要的东西。你*必须*学会清理你的查询。阅读您可以在SQL注入中找到的所有内容。 – meagar

回答

0

首先第一件事情,你MUST清理用户与mysql_real_escape_string()输入:

$e = mysql_real_escape_string ($_POST['email']); 
$pass = mysql_real_escape_string ($_POST['pass']); 

阅读上SQL injection一点,你会很高兴你没有。

至于主要问题,您能否提供更多的上下文?你如何检查用户是否登录?

1

乌姆有一些事情我错在这里看到...

首先查询应消毒的...

$email = mysql_real_escape_string ($_POST['email']); // escape the email 
$pass = SHA1(mysql_real_escape_string ($_POST['pass'])); // escape and encrypt the pass 

// now you can put it into the query safely 
$query = "SELECT user_id from toddprod where email = '$email' and pass = '$pass' "; 

接下来你执行查询错了,mysql_query函数接受两个参数,即查询和数据库连接。你传递错误的参数,你传递的查询和mysql_select_db函数的结果只是一个布尔值。因此,您必须将$dbc而不是$db纳入该查询中,并且即使这样您仍然以错误的顺序传递参数。查询首先进行,而不是连接。因此,它应该是...

$result = mysql_query($query, $dbc); 

接下来你想设置为从mysql_query功能作为Cookie返回值,但该值是一种资源,而不是你需要的用户ID。你必须从这个资源中实际读取值。

$row = mysql_fetch_array($result); 
$userid = $row["user_id"]; 
setcookie('user_id', $userid); 

继续前进......当你设置电子邮件的cookie,你必须在单引号中的变量,所以cookie将实际上包含$e,而不是实际的电子邮件,因为单引号存储字符串litterly(不解析变量)。所以你应该使用双引号,或者根本不使用引号。所以下面的任何一个是好的...

setcookie('email', "$e"); 
setcookie('email', $e); 

最后但并非最不重要的,你不应该在你的if语句的结束分号,再次,你需要通过不连接的数据库-selection导致进入mysql_close功能,所以它应该是

mysql_close($dbc); 

在那里,希望这可以让你的地方,尝试这些变化,如果问题仍然存在,我很乐意进一步提供帮助。

这里有链接,这将帮助你:

http://www.php.net/manual/en/function.mysql-query.php

http://www.php.net/manual/en/function.mysql-fetch-array.php

http://www.php.net/manual/en/function.mysql-real-escape-string.php

编辑:

在这里,我根据已定的代码我发现的问题。试一试,我无法测试它,所以它可能会有一些小的语法错误在这里和那里,但它应该给你一些比较。同样对于将来,我建议你在语义上/正确地命名变量,以便其他人更容易拾取,并且还可以避免让你感到困惑,例如将$ db而不是$ dbc传递给几个函数。

<?php 
    // keep the function names in lowercase, no reason, just looks better to me 
define('DB_USER', 'usernamegoeshere'); 
define('DB_PASSWORD', 'passwordhere'); 
define('DB_HOST', 'hostnamehere'); 
define('DB_NAME', 'andtheotherthinghere'); 

    // connect to the mysql server 
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL'); 
    // select the database, you don't need to store the result, it just returns true or false 
mysql_select_db(DB_NAME, $conn) or die('Could not select database.' .mysql_error()); 

    // escape the input 
$email = mysql_real_escape_string($_POST['email']); 
$pass = sha1(mysql_real_escape_string($_POST['pass'])); 

    // create the query 
$query = "SELECT user_id FROM toddprod WHERE email = '$email' AND pass = '$pass'"; 

    // execute the query 
$result = mysql_query($query, $conn); 
$usercount = mysql_num_rows($result); 

if($usercount == 1){ 
    // read the results and get the user_id 
    $row = mysql_fetch_array($result); 
    $userid = $row['user_id']; 

    // set the cookies 
    setcookie('user_id', $userid); 
    setcookie('email', $email); 
    setcookie('logged-in', 'true'); 

    // echo success message 
    echo 'good'; 
}elseif($usercount == 0) { 
    echo "You're $email with password $pass"; 
} 
mysql_close($conn); 
?>