我正在尝试使用AJAX验证表单。这就是我到目前为止所做的:
$('#login-form').submit(function(e) {
e.preventDefault();
var user = username.value;
var pass = password.value;
if (user != '' && pass != '') {
$('#login').html('Proccessing...');
$.ajax({
url: 'login.php',
type: 'POST',
data: {
username: user,
password: pass
},
processData: false,
contentType: false,
success: function(response) {
if (response == 'success') {
window.location.href = 'admin.php';
} else {
$('.login_message').html('Incorrect Credentails');
$('#login').html('Login');
}
}
});
} else {
$('.login_message').html('Fill All Fields');
$('#login').html('Login');
}
})
似乎response
没有返回success
。以下是login.php文件
<?php
session_start();
$password = $username = '';
$_SESSION['user'] = $_SESSION['error'] = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['login'])) {
include_once('db.php');
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$password = md5($password);
echo 'username: ' . $username . ' and ' . ' password: ' . $password;
$sql = "select * from users where username = '" . $username . "' limit 1";
$query = mysql_query($sql);
if ($query) {
$row = mysql_fetch_assoc($query);
$dbpass = $row['password'];
if ($password == $dbpass) {
;
$_SESSION['user'] = $username;
header('Location: admin.php');
} else {
$_SESSION['error'] = 'Wrong username or password!';
}
} else {
echo mysql_error();
}
}
}
?>
如果碰巧找到解决方案,请向我解释如何找到解决方案以及我做错了什么。 先感谢您。
答案 0 :(得分:0)
使用AJAX时,我相信如果你在PHP目标文件中public String toString() {
char buf[] = {value};
return String.valueOf(buf);
}
(echo
),它将作为login.php
。因此,return
之后的代码将无法按预期运行。
您的代码中还有:echo
使用echo $_SESSION['error'] = '';
比较两个对象,==
是assignment operator。
在代码中使用jQuery中的=
方法对我来说是正确的。因此,当进行调用时,信息将异步发送到服务器。更准确地说,它会将参数发送到您在ajax对象属性中指定的PHP文件:ajax()
。
在login.php
中,您可以访问login.php
数组中传递的参数。
您将拥有以下内容:
$_POST
在您的jQuery代码中,您可以使用$username = $_POST['username'];
$password = $_POST['password'];
// process information...
$state = 'success'
// now you can return a JSON object back to your page
// I strongly recommend using a PHP array and converting it to JSON
// this way it's very easy to access it back with JS
$response = array(state=$state)
echo json_encode($response);
response.state
现在,您通常在此PHP文件的代码中遇到问题。调试它并不容易。所以我的方法是:我在if(response.state == 'success') {
alert('It is a succcess!');
}
中设置参数:
login.php
然后我会在浏览器中打开PHP文件并运行它,看看它是否回显了对象以及是否有任何错误。
然后你可以放回$username = 'usernameTest'; // $username = $_POST['username'];
$password = 'passwordTest'; // $password = $_POST['password'];
和$username = $_POST['username'];
。
$password = $_POST['password'];
<?php
session_start();
if (isset($_POST['username'], $_POST['password']) {
include_once('db.php');
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$password = md5($password);
$sql = "select * from users where username = '" . $username . "' limit 1";
$query = mysql_query($sql);
if ($query) {
$row = mysql_fetch_assoc($query);
$dbpass = $row['password'];
if ($password == $dbpass) {
$state = 'success';
} else {
$state = 'failed';
}
} else {
echo mysql_error();
}
}
,mysql()
和SQL注入
请勿使用已弃用且不安全的
md5()
函数。它们自PHP 5.5(2013年)以来已被弃用,并在PHP 7(2015年)中被完全删除。请改用mysql_*
或MySQLi
。您对SQL Injections持开放态度,应该使用Prepared Statements而不是连接查询。使用
PDO
远非逃避数据的安全方法。请勿使用
strip_tags()
进行密码散列。这是非常不安全的。请改用PHP的md5()
和password_hash()
。如果你运行的PHP版本低于5.5(我真的希望你不是这样),你可以使用password_verify()
库来获得相同的功能。
答案 1 :(得分:0)
由于这是import numpy as np
import urllib.request
import cv2
from matplotlib import pyplot as plt
from keras import backend as K
%matplotlib inline
file = "http://farm2.static.flickr.com/1353/1230897342_2bd7c7569f.jpg"
# Read web file
images_or = np.ndarray((1,128, 128,3), dtype=np.uint8)
req = urllib.request.urlopen(file)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr,-1) # 'load it as it is'
images_or[0] = cv2.resize(img,(128,128))
# Display image
plt.imshow(images_or[0])
plt.show()
# Format image
images_or = images_or.astype(K.floatx())
images_or *= 0.96/255
images_or += 0.02
# Display image
plt.imshow(images_or[0])
plt.show()
# Reshape image
images_or = images_or.reshape(images_or.shape[0], 3, 128, 128)
# Copy image in another np.array
A_train_test = np.ndarray((1, 3, 128, 128), dtype=np.uint8)
A_train_test[0] = images_or[0]
# Format image
A_train_test = A_train_test.astype(K.floatx())
A_train_test *= 0.96/255
A_train_test += 0.02
# Reshape image
A_train_test = A_train_test.reshape(A_train_test.shape[0], 128, 128, 3)
image_xxx = A_train_test[0]
plt.imshow(image_xxx)
plt.show()
请求,我们需要从服务器发送一些响应。正如您在问题中所做的那样,检查ajax
。为此,您需要将if(response=='success')
发送给您的客户。如果一切正常(数据发送到服务器和查询),则在success
编辑此行
login.php
我在这里放了一行代码if($password == $dbpass) {
$_SESSION['user'] = $username;
//comment this line
//header('Location: admin.php');
echo "success";
} else {
$_SESSION['error'] = 'Wrong username or password!';
}
,我相信这会解决您的问题。