tags:

views:

143

answers:

6

I have a php page that takes in a bunch of url parameters and sends out an email. I am trying to have this page call another web page using the same url parameters, after the mail is sent. If I do the following, will my email be sent reliably? Is a redirect what I really want to do?


Update: Thanks for the tips. As you can see by my use of the +, I don't know any php. After reading all the answers so far I have come up with this:

Random code to send email...

file_get_contents('http://www.othersite.com/' . $_SERVER["REQUEST_URI"]. "?". $_SERVER["QUERY_STRING"]);

I believe this should initiate a GET on the other site with all the current parameters, which is exactly what I want. This way I don't have to deal with redirects. Any problems to this solution?


Update 2: Since my url was https, file_get_contents caused me some problems. There are ways to get around this but I just used header for a redirect and all worked well. Thanks everyone!

+1  A: 
  • if its the same application, why dont you call the same functions ?
  • if you want you could do file_get_contents .. instead of a redirect for the same effect.
solomongaby
different application, but thanks for the file_get_contents.
UmYeah
+1  A: 

If you just want to hit that page why not use file_get_contents

$data = file_get_contents('http://www.othersite.com/' . $_SERVER["REQUEST_URI"]); 
echo $data; 

The benefit with this is you don't have to physically go to the other site if you don't want to, equally if you control the script on the other site you could return a true or false in the HTML which could be checked upon return.

ILMV
This won't pass through the GET parameters to the called script, though.
Pekka
+2  A: 

The header location call will be only called after the mail code so it won't affect your email.

Don't forget to call exit() after your header location call.

Also the string concat operator is not + it's . (dot).

AlexV
+3  A: 

The question raised in the other answers whether your basic approach is really what you want is valid - check that first. Anyway, if it really is what you want to do (Is your target URL really identical to the one you're on?) you can indeed use

header('Location: http://www.othersite.com/' . $_SERVER["REQUEST_URI"]);

Just note the use of . to concatenate the string instead of +, you can't do that in PHP.

To do it really properly, you could use http_build_url to build a full valid URL from the current GET array. Code from the manual, modified a bit:

<?php
echo http_build_url("http://[email protected]/pub/index.php",
    $_GET,
    HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY 
);
?>
Pekka
I actually had to use header instead of file_get_contents because my url was https. So your answer is the one I used.
UmYeah
`$_SERVER["REQUEST_URI"]` already contains the URI path and query.
Gumbo
@Gumbo cheers, of course. Corrected.
Pekka
A: 

For full compliance (sometimes Chrome will not work with just a Location: header)

header( "HTTP/1.0 302 Found" );
header( "Status: 302" ); # this is for chrome compliance
header( "Location: http://www.othersite.com/' . $_SERVER["REQUEST_URI"] );
Andy
A: 

Another option is to echo the HTML tag:

<meta http-equiv="Refresh" content="1;url=http://www.othersite.com/&lt;?php echo $_SERVER['REQUEST_URI']; ?>">

This allows you to set a delay time for redirecting (usually 1s), which is good in some situations so that the user doesn't become confused by a flash of content. You can put a 'Stand by while we redirect you' message or similar.

Lotus Notes