-7

I have been searching to find out if it would be better to redirect a user loggin into a website on the client or server side. From my research i see that it is better to rely on to the server side.

I have a JS script that will use Jquery to post data to a php script to check the users login details, if they're find it tells the JS to redirect. This worked okay but now i want to redirect using the PHP script but the script won't redirect when i us $.post() method.

So the question is, how do i redirect the use this way? I'm trying to build this system so it's independent on the user having JS turned on in their browser so any help and tips would be very welcome.

Thanks.

ragebunny
  • 1,582
  • 10
  • 33
  • 53
  • $.post makes a request in the background, so the answer arrives "in" your script - so you can't do a server-side redirect of the page displayed in the browser that way. And anyway, if solution X is "better" than Y depends on multiple factors - just "having read" that a server-side redirect would be better without actually having any criteria to base that decision on is quite nonsense. – CBroe Oct 15 '13 at 09:11
  • Redirect where you want.. Usually PHP does the redirection, but you can use and javascript base redirection after receiving success from your PHP login – Svetoslav Oct 15 '13 at 09:13

3 Answers3

4

Because you're using AJAX, only the JavaScript can redirect the browser. If the server attempts to do so, it will only redirect the AJAX request.

That being said, your statement that you are "trying to build this system so it's independent on the user having JS turned on" is contradictory with your use of $.post().

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

you may get the "Location" header (redirect URL) from here:

jQuery - get AJAX response headers

success: function(res, status, xhr) { 
  alert(xhr.getResponseHeader("Location"));
}
Community
  • 1
  • 1
Moshe L
  • 1,797
  • 14
  • 19
0

Use a standard form to post credentials to the auth method. Using JavaScript as the primary method of authentication is like having no authentication at all.

If you have already worked around that adequately and are just looking for a non-js fallback, make it all non-js. PHP redirects are very easy, but you have to output a header, which can't be done asynchronously, so you will have to post the data anyway.

Example PHP Redirect

header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);
Chidge
  • 184
  • 7