1

I have a situation like this.

<php>
<redirect the page >
<exit>

<javascript>
<redirect the page again>

I want to have javascript that basicall disables the PHP redirect. So if Javascript is enabled on the browser, the javascript redirect will work, if it disable, the PHP redirect will work. Should I just enclose the PHP code in span and make it invisible? Any ideas?

Addition ok this is not a simple redirect. the form authentication is rather odd. Register.php -> register_submit.php -> Was there an error -> yes go back to register.php (everything is javascript at this point). What I have added is PHP authentication as well so if I see javascript is not enabled, I take the user to register.php *after it does the regular checking of fields *.

3
  • 1
    Why not redirecting always from php? Commented Sep 13, 2011 at 17:34
  • I don't see how this can make sense. JavaScript can't turn "off" some PHP block, as JS is client-side and PHP is server-side. What kind of redirect are you trying to handle? Commented Sep 13, 2011 at 17:36
  • This question makes no sense. A real PHP redirect is done via header("Location: URL"). With this, the redirect happens before the page is loaded - thus JavaScript has no chance to load, let alone execute. Commented Sep 13, 2011 at 17:36

4 Answers 4

1

PHP is a server-side technology. By the time Javascript even sees what's happened, it's too late.

Short answer, JS can't intercept/block PHP (as long as PHP is being called first).

Order of events:

  1. Client requests page
  2. PHP executes and generates output of page
  3. Browser receives output
  4. Browser begins parsing what was sent by what PHP already spit out.
Sign up to request clarification or add additional context in comments.

6 Comments

Interesting note ("as long as PHP is being called first"). How would one manage to call JS before PHP is processed?
I'm saying as long as it's a PHP-generated page. An HTML page with an AJAX call to a PHP script would complicate that statement. ;p
Just thinking that... AJAX means that the javascript is executing before PHP. But, Brad was right to leave that out of this answer... there's obviously a fundamental concept that is not properly grasped by the OP.
But then you are cheating, aren't you ;)?
@NullUserException: Nope; you're developing. ;-)
|
1

Remove your PHP redirection and add this in your <head>:

<noscript>
    <meta http-equiv="refresh" content="0; http://www.example.com/1" />
</noscript>
<script>
    window.location = 'http://www.example.com/2';
</script>

This will redirect to http://www.example.com/1 when javascript is disabled, and to http://www.example.com/2 when it's enabled.

4 Comments

Unless I'm mis-reading, hasn't the PHP already been rendered and pushed out a header('Location: ...') by the time this has been rendered? (Or am I mis-interpreting the execution order?)
@Brad, yes, obviously OP should replace the location header by this
ok this is not a simple redirect. the form authentication is rather odd. Register.php -> register_submit.php -> Was there an error -> yes go back to register.php (using javascript). What I have added is PHP authentication now as well so if I see javascript is not enabled, I take the user to register.php using PHP rather Javascript. Previously everything was Javascript.
Aren't noscript elements not allowed in the head element?
0

PHP code is executed on the server-side, while JS is client-side. So with that structure the PHP will kick in before the JS is executed. If you want JS to control PHP you need to make use of AJAX to control it.

Also enclosing PHP code in a "span" won't have any effect.

Comments

0

Javascript and PHP do not directly interact (exceptions apply, don't worry about them now :D). The best way to implement this type of interaction between these two disparate languages is to use the query string or cookies.

I think there may be some confusion here about when and how PHP is executed as opposed to when and how javascript is executed. Think of PHP as the factory - the goods are physically produced there. Think of your server as the loading dock, the internet as the shipping company. Your browser is the store, HTML is the shelves; Javascript is the window decorations on the store that sells the merchandise. The window decorations have no affect on the production, the factory can make some window decorations, but it doesn't use them, it just ships them right along with the merchandise for the store to use. PHP is the factory, javascript is the decoration. There are some problems with taking this analogy too literally, but there it is in a nutshell.

You can make the PHP redirect conditional on the presence or absence of a specific query string variable:

<?php
  // redirect if $_GET['no_redirect'] is NOT set. Reverse the true/false to invert this rule
  $do_redirect = (isset($_GET['no_redirect']) === false ? true : false);

  // perform the redirect, if required
  if ($do_redirect === false)
     header('Location:http://mydomain.com');
?>

Javascript:

window.location = 'http://mydomain.com/?no_redirect=1';

EDIT If you're trying to detect if javascript is enabled, then the best way is for javascript to set a cookie if it is enabled. PHP can then check for this cookie, and if it isn't found then you'll know that javascript didn't get a chance to set it, so it must be disabled (or the user edited their cookies).

Take a look at some code snippets for dealing with cookies in javascript, and check out the documentation for dealing with cookies in PHP.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.