0

我正在使用以下代码将 imag 文件保存在机器上,如果用户是真实的,则保存图像,否则应显示注册页面。但是在保存图像页面后刷新或身份验证失败未重定向到注册。问题是什么?

js ajax ::

$.ajax({
  type: "POST",
  url: "userLoginAction.php",
  data: {image: dataURL,uname:uname,pwd:pwd}
}).done(function( respond ) {
 alert("Downloading Completed"+respond);
 console.log('back on download js**');
});

PHP userLoginAction.php ::

<?php 
include("sessionHandler.php");
include("../connect.php");
$con = connectDb();
$db = SelectDB($con);

$username = $_REQUEST['uname'];
$password = $_REQUEST['pwd'];
$img= $_POST['image'];

// To protect MySQL injection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$query = "SELECT * FROM user WHERE user_email ='".$username."' AND user_password='".$password."'";
$result = mysql_query($query);

$count=mysql_num_rows($result);
if($count==1){
    // Register $myusername, $mypassword and redirect to file 
    createLoginSession($username,$password);
    include 'download.php'; 

}
else {
    echo "Wrong Username or Password".$img;
    header("location:signup.php");
}
?>
4

3 回答 3

1

因为在服务器端进行的重定向不适用于 ajax。你必须像这样在你的 ajax 成功时进行重定向。尝试这样的事情

阿贾克斯

$.ajax({
  type: "POST",
  url: "userLoginAction.php",
  data: {image: dataURL,uname:uname,pwd:pwd}
}).done(function( respond ) {
   if(respond == 'download'){
      alert("Downloading Completed"+respond);
      console.log('back on download js**');
   }
   else if(respond == 'login'){
      window.location = "url/to/loginpage";
   }
});

PHP

include("sessionHandler.php");
include("../connect.php");
$con = connectDb();
$db = SelectDB($con);


$username = $_REQUEST['uname'];
$password = $_REQUEST['pwd'];
$img= $_POST['image'];


// To protect MySQL injection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$query = "SELECT * FROM user WHERE user_email ='".$username."' AND user_password='".$password."'";
$result = mysql_query($query);

$count=mysql_num_rows($result);
if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    createLoginSession($username,$password);
    include 'download.php';
    echo 'download';

}
else {
    echo "login";
}
于 2013-10-09T08:37:35.807 回答
0

尝试这个,

AJAX

$.ajax({
    type: "POST",
    url: "userLoginAction.php",
    data: {
        image: dataURL,
        uname:uname,
        pwd:pwd
    },
    success:function(data) {
        if (data == '**DOWNLOAD**') {
            alert('Download Completed');
        } else if (data == '**LOGIN**') {
            window.location.href = 'signup.php';
        }
    }
});

PHP

include("sessionHandler.php");
include("../connect.php");
$con = connectDb();
$db = SelectDB($con);


$username = $_REQUEST['uname'];
$password = $_REQUEST['pwd'];
$img= $_POST['image'];


// To protect MySQL injection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$query = "SELECT * FROM user WHERE user_email ='".$username."' AND user_password='".$password."'";
$result = mysql_query($query);

$count=mysql_num_rows($result);
if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    createLoginSession($username,$password);
    include 'download.php';
    echo '**DOWNLOAD**';

}
else {
    echo "**LOGIN**";
}
于 2013-10-09T08:47:56.407 回答
-2

要防止在 Ajax 调用后刷新页面,请在 done 函数中返回 false。

于 2013-10-09T08:38:25.260 回答