简体   繁体   English

jQuery / PHP Ajax登录系统

[英]Jquery/PHP ajax login system

I'm setting up a blog type page for my business. 我正在为我的业务设置博客类型页面。 Brand new to MySQL and PHP. 全新的MySQL和PHP。 Set up this login system. 设置此登录系统。 For some reason have no idea why the login is dropping. 由于某种原因,不知道为什么登录被删除。 Suppose to check for errors then return 'good' through php if the email and password is right. 假设检查错误,然后在电子邮件和密码正确的情况下通过php返回“ good”。 If php returns good then it's suppose to redirect to the blog page. 如果php返回良好,则应该重定向到博客页面。

Been dealing with this for a few months need desperate help please. 已经处理了几个月,请急切的帮助。 Thank you. 谢谢。 Here is the php code that goes along with the jquery. 这是与jquery一起使用的php代码。

Link to test site is here. 链接到测试站点在这里。 test.toddprod.com/login Would really appreciated the help. test.toddprod.com/login非常感谢您的帮助。 Thanks 谢谢

<?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);
?>

Umm there's a number of things I see wrong here... 嗯,我在这里看到很多错误。

First of all your query should be sanitized... 首先,您的查询应该被清理...

$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' ";

Next you're executing the query wrong, the mysql_query function takes two arguments, the query and the database connection. 接下来,您错误地执行查询, mysql_query函数接受两个参数,查询和数据库连接。 You're passing the wrong arguments, you're passing the query and the result of the mysql_select_db function which is just a boolean value. 您传递了错误的参数,传递了查询以及mysql_select_db函数的结果,该函数只是一个布尔值。 So you have to $dbc not $db into that query, and even then you're passing the arguments in the wrong order. 因此,您必须在该查询中使用$dbc而不是$db ,即使这样,您还是以错误的顺序传递了参数。 The query goes first, than the connection. 查询先于连接。 So it should be... 所以应该是...

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

Next you're trying to set the return value from the mysql_query function as your cookie but that value is a resource, not the userid that you need. 接下来,您尝试将mysql_query函数的返回值设置为cookie,但是该值是资源,而不是所需的userid。 You have to actually read the value from the resource like this. 您必须像这样从资源中实际读取值。

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

Moving on... when you're setting the email cookie, you have the variable in single quotes, so the cookie will actually contain $e and not the actual email because single quotes store strings litterly (without parsing the variables). 继续...当您设置电子邮件cookie时,您在单引号中包含了变量,因此cookie实际上将包含$e而不是实际的电子邮件,因为单引号会litterly存储字符串(不解析变量)。 So you should either use double quotes, or no quotes at all. 因此,您应该使用双引号或根本不使用引号。 So either one of the following is fine... 因此,以下任何一项都可以...

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

Last but not least, you should not have the semicolon at the end of your if-statement, and you again you need to pass the connection not the database-selection result into the mysql_close function, so it should be 最后但并非最不重要的一点是,您不应在if语句的末尾使用分号,并且再次需要将连接而不是数据库选择结果传递给mysql_close函数,因此应该

mysql_close($dbc);

There, hope this gets you somewhere, try out these changes and if the problem persists i'd be happy to help you further. 在那里,希望可以带您到某个地方,尝试这些更改,如果问题仍然存在,我很乐意为您提供进一步的帮助。

Here are links that will help you out: 以下链接将为您提供帮助:

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

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

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

Edit: 编辑:

Here, I have fixed the code according to the problems I found. 在这里,我根据发现的问题修复了代码。 Try it out, I could not test it so it might have some small syntax errors here and there, but it should give you something to compare with. 尝试一下,我无法对其进行测试,因此它可能在某些地方有一些小的语法错误,但是它应该给您一些可以比较的东西。 Also for the future, I would suggest that you name your variables semantically/properly so it's easier for others to pickup and it will also keep you from getting confused like you were passing $db instead of $dbc into a few of your functions. 同样在将来,我建议您以语义/适当的方式命名变量,这样其他变量就更容易使用,并且还可以避免像将$ 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);
?>

First things first, you MUST sanitise user input with mysql_real_escape_string() : 首先,您必须使用mysql_real_escape_string()清除用户输入:

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

Read up a bit on SQL injection , you'll be very glad you did. 阅读有关SQL注入的内容 ,您会感到很高兴。

As for the main problem, could you provide a bit more context? 至于主要问题,您能否提供更多背景信息? How are you checking to see if the user is logged in? 您如何检查用户是否已登录?

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

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