View Full Version : PHP javascript before header (re-direct);

02-04-2010, 12:03 PM
Hi. I've inherited a piece of PHP code that I need to modify by inserting some java-script that registers clicks so affiliates get paid.

The PHP does some logic but essentially its only there to re-direct to different URLs depending on the outcome of the preceding logic.

From what I understand, you cannot have any HTML output before the before the header statement...

$redirect = "www.somewebsite.com";
header( 'Location: '.$redirect );

And as I need to put the java script in echo statements before the redirect I cannot see how I can do this.

What I really need is the equivalent of a VB Subroutine that I call from within my PHP, executes some javascript and returns.

Other constraints I have are:

* I don't want to break the procedural flow of the page by making it call another page beforehand. The page is used pseudo recursively in that once it redirects to another URL, That URL will redirect back to this page once user interaction is complete. Making all those other pages call a different PHP page will take a hell of a lot of work! Also, the PHP page seems badly written although I'm new to PHP so what do I know? Theres a lot of logic going on so I'm uncomfortable interfering with it too much.

* I need to pass a parameter to the javascript.

* I'm very new to PHP so please go easy if theres an obvious answer!


02-04-2010, 12:21 PM
You can not output JavaScript to the browser before the redirect. This is because any HTTP document consists of 2 parts: first headers and only then body.

The function header() is used to send an HTTP header. After the redirect header is sent, the browser will most probably ignore any JavaScript you try to output in the body of the HTTP document and not execute your JavaScript.

On the other hand, if you try to output your JavaScript before the sending the HTTP header, you'll get the error "headers already sent". Because your HTTP server to be able to send the body of the HTTP document containing your JavaScript to the browser will be forced to send some default HTTP headers first. And only then it will send the body of the HTTP document containing your JavaScript.

Probably you could use the JavaScript redirect instead of PHP redirect. But it will not work if JavaScript is turned off in the user browser.

Or maybe you could simply add a parameter to the URL to which you are redirecting like

$redirect = "http://www.somewebsite.com/mypage.php?p=1";
header( 'Location: '.$redirect );

Then at mypage.php you could check if

if(isset($_GET['p']) && 1==$_GET['p'])
Place your JavaScript here

In this case the JavaScript would be included at the page where you are redirecting. But it will be included only if a certain parameter is sent by GET.

02-04-2010, 01:02 PM
Thank you for replying.

Is there no other way around this? I really want this piece of javascript to be called in one place only as I'd have to rely on other developers ensuring the javascript is run after the redirect which I therefore can't guarantee will happen.

I just looked up ob_start(); and am about to try using the pseudocode code below within my PHP page.

\\Some preceding logic that determines the value of $redirect\\


echo "<html>";
echo "<head>";
echo "</head>";
echo "<body>";
echo "<script language=\"javascript\" src=\"https://scripts.somewebsite.com/jsfunctions.js"></script>";
echo "<script language=\"javascript\">";
echo " <<my java script>>";
echo "</script>";
echo "</body>";
echo "</html>";


header( 'Location: '.$redirect );

Do you think this will work?


02-04-2010, 01:15 PM
Output buffering works in the following way: when you turn it on, it makes the PHP parser to temporarily buffer any content sent to the browser and send only HTTP headers. Then when the headers are sent, it will send the buffered content to the browser.

It could not be otherwise. This is the structure of HTTP document according to the standard. Headers go first, then an empty line, then body.

So with output buffering or not, the browser will first receive the HTTP headers, including your Location header. And only then it will receive the body of the document containing your JavaScript.

And personally I would never rely on a browser to execute any JavaScript after the Location header is received. Most probably any browser would ignore the JavaScript.

02-04-2010, 02:14 PM
Thanks. Still getting used to what I can and cannot do in web apps. I miss VB and executables! I'll make the other dev's call the javascript. Cheers