PHP- Ajax and Redirect

前端 未结 3 628
情书的邮戳
情书的邮戳 2020-12-11 12:33

i have a jquery Ajax request happening on a page. On php side i am checking if the session is active and doing something. If the session is not active i want to redirect th

3条回答
  •  情书的邮戳
    2020-12-11 13:06

    This is what you would want in your php IF THIS WERE A REGULAR REQUEST, NOT AN AJAX

    if (isset($_SESSION)) doSomething();  
    else header("Location: otherUrl");
    

    Since this is an Ajax call, you are not passing control to the php, but just trying to get a response that (likely) fills a particular section of your page. You do not mention what jQuery ajax function you use, but it matters. I would imagine you are using either $.get() or $(element).load() ??

    Without knowing the particulars, this is my best suggestion for you.

    Ajax call: $.get(url, callbackFunc);

    php:

    if(isset($_SESSION)) echoSomething()   else echo("redirect");
    

    callbackFunc:

    function(data)
    { if (data == "redirect") window.location = otherUrl;
       else 
       $("#desiredElement").html(data);
    }
    

提交回复
热议问题