2

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
  • 1
    Re your update: Looks fine if the directory structure and page name on the remote server are indeed identical, down to the page name? That sounds hard to believe if it's an external service you're calling. Commented Mar 17, 2010 at 13:36

6 Answers 6

3

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 
);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I actually had to use header instead of file_get_contents because my url was https. So your answer is the one I used.
$_SERVER["REQUEST_URI"] already contains the URI path and query.
2

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).

Comments

1
  • 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.

Comments

1

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.

1 Comment

This won't pass through the GET parameters to the called script, though.
0

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"] );

Comments

0

Another option is to echo the HTML tag:

<meta http-equiv="Refresh" content="1;url=http://www.othersite.com/<?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.

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.