LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to supply extra commands along with PHP header("Location: http://www.abc.com")? (http://www.linuxquestions.org/questions/programming-9/how-to-supply-extra-commands-along-with-php-header-location-http-www-abc-com-448661/)

alphaque 05-26-2006 01:52 AM

How to supply extra commands along with PHP header("Location: http://www.abc.com")?
 
Hi
I have an index.php file that use the PHP header(location) command to redirect to an external web site when user submit the "p" value as "123" in their source URL (i.e : www.xyz.com/index.php?p=123). Below is the example when user supply p=123, it will be redirected to www.abc.com :

//source PHP file - index.php
<?php
$a = $_REQUEST["p"];
switch ($a) {
case '123': header( "Location: http://www.abc.com"); exit; break;
}
?>

My problem is besides redirect the user to the destination URL (http://www.abc.com), i also want to pass along a line of java script to the destination URL which is like below :

<script>document.write('You are coming from www.xyz.com')</script>

The above script need to be executed at the detination web site (www.abc.com)before the HTML code of the www.abc.com is read. This means i want to pass the above script command to the destination URL and it is executed before the destination URL <HTML> tag,basically i want the destination web site source code to look something like below :

<script>document.write('You are coming from www.xyz.co')</script> //this script command is passed from the source PHP file

<html>
<head>abcde </head>
<body>scnsdjcsc</body?
</html>

Can anyone share with me how do i pass that script command in my PHP header (location) command in my source PHP file ?

thanks in advance..

zeitounator 05-26-2006 04:06 AM

2 quick n' dirty solutions.

Solution 1: pass a parameter in the url.
Code:

#Index.php on xyz.com
<?php
$a = $_REQUEST["p"];
switch ($a) {
case '123': header( "Location: http://www.abc.com?fromsite=www.xyz.com"); exit; break;
}
?>

#Index.php on abc.com
<?php
#some blabla for the head
$onload = '';
if (!empty($_GET['fromsite'])) {
    $onload = " onload=\"document.write('You are coming from " . $_GET['fromsite'] . "')\"";
}
echo '<body' . $onload . '>';
# remaining code for the page....
?>

Solution 2: use the http referer (I let you figure out how to retreive the hostname only from that string if your really need to).
Code:

#index.php on abc.com
<?php
#some blabla for the head
$onload = '';
if (!empty($_SERVER['HTTP_REFERER'])) {
  $onload = " onload=\"document.write('You were redirected from " . $_SERVER['HTTP_REFERER'] . "')\"";
}
echo '<body' . $onload . '>';
# remaining code for the page....
?>


alphaque 05-26-2006 04:14 AM

Hi zeitounator
Thanks, your solution works if i own the destination web site, the problem is i don't own the destination web site so i can't modify the code there, anymore help ? :-)

thanks ~~

spooon 05-26-2006 04:28 AM

Quote:

Originally Posted by alphaque
My problem is besides redirect the user to the destination URL (http://www.abc.com), i also want to pass along a line of java script to the destination URL which is like below :

<script>document.write('You are coming from www.xyz.com')</script>

The above script need to be executed at the detination web site (www.abc.com)before the HTML code of the www.abc.com is read. This means i want to pass the above script command to the destination URL and it is executed before the destination URL <HTML> tag,basically i want the destination web site source code to look something like below :

<script>document.write('You are coming from www.xyz.co')</script> //this script command is passed from the source PHP file

<html>
<head>abcde </head>
<body>scnsdjcsc</body?
</html>

You can't do that (for obvious security reasons). Why do you need to do this? Why not instead do something like 1) use frames to display a message in one frame and the site in the other, or 2) print the content of the other site with your script?


All times are GMT -5. The time now is 05:33 AM.