简体   繁体   English

php或jquery会重定向我吗? Ajax登录

[英]Will php or jquery redirect me? Ajax login

I made an ajax call to the php script, which would indicate whether the information input by the user were valid and he would be authorized to sign in. The thing is I'm not really sure about the session variable. 我对php脚本进行了ajax调用,该调用将指示用户输入的信息是否有效,并且将授权他登录。问题是我不确定会话变量。 Can that php script save session id variable and pass it onto the next php page, where jquery would redirect me upon successful ajax call? 该php脚本可以保存会话ID变量并将其传递到下一个php页面吗,在此页面上,jQuery将在成功调用ajax时重定向我? Or would php itself redirect me using header location? 还是PHP本身会使用标头位置重定向我? I hope I made myself clear. 我希望我能说清楚。 It is a theoretical question. 这是一个理论问题。

You would set the userid in the session and return a "success" condition back to ajax. 您将在会话中设置用户标识,并将“成功”条件返回给ajax。 the ajax success callback would inspect the returned data to determine if the login was successful and act accordingly (for example, redirect to a logged-in homepage) ajax成功回调将检查返回的数据以确定登录是否成功并采取相应措施(例如,重定向到登录的主页)

You cant not redirect with PHP in this case because the headers are not returned in the http response. 在这种情况下,您无法使用PHP进行重定向,因为标头未在http响应中返回。 you can set a variable in the response that returns the state of your php proccess. 您可以在响应中设置一个变量,以返回您的php处理状态。 something like 就像是

data = array(
 "connect"=>true
);

or 要么

data = array(
 "connect"=>false,
 "errors"=>"There is an error in your request"
);

then in the success: function you can check if the variable is true or false and redirect if you need to. 然后在success:函数中,您可以检查变量是true还是false,并根据需要重定向。

success: function(response){
  if(response.connect){
//succeded
}else{
//not succeded
}

hope that helps. 希望能有所帮助。 cheers! 干杯!

I disagree from @Mijail when he says 我不同意@Mijail的话

You cant not redirect with PHP in this case because the headers are not returned in the http response . 在这种情况下,您无法使用PHP进行重定向,因为标头未在http响应中返回

With jQuery (or plain XHR), you can verify if you got a redirection with the response status (one of 3xx) and then use getResponseHeader method from xhr object to get the 'location' header to see where you should be redirected : 使用jQuery(或普通XHR),您可以验证是否具有响应状态(为3xx之一)的重定向,然后使用xhr对象中的getResponseHeader方法获取'location'标头,以查看应该重定向到'location'

success: function(data, textStatus, xhr) {
    if(xhr.status > 300 && xhr.status < 400) {
        document.location.href = xhr.getResponseHeader('location') || '/';
    }
}

This is the right way of doing it when you don't want to replicate your redirection rules on the client side too. 当您也不想在客户端上复制重定向规则时,这是正确的方法。

In theory you could do it either way, but without seeing your actual code, it's hard to give a definitive answer. 从理论上讲,您可以使用任何一种方法来完成此操作,但是如果看不到您的实际代码,很难给出明确的答案。

If you are using jQuery to call your auth PHP script and setting your session in that PHP script, you would likely then just use your JS (Ajax) to redirect to the "protected" area on success (returned from your PHP script with session started). 如果您使用jQuery调用auth PHP脚本并在该PHP脚本中设置会话,则可能会成功使用JS(Ajax)重定向到“受保护”区域(会话启动时从PHP脚本返回) )。

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

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