1

There is a textbox (name="url") in a form which contains a page URL. When the form is submitted, data will be sent to a PHP page. I want to know how to redirect that PHP page to the URL from the form? I have the code for redirection:

header( 'Location: www.google.com' );

Now I want to know how use that URL from the form, like so:

header( 'Location: << here should be the url we got from form >>' );

How could I do this?

4 Answers 4

2

Just use string concatenation...

header('Location: ' . $_POST['url']);
exit;

I am pretty sure header() doesn't allow \n or anything that would allow additional headers to be set. If it did, it would be HTTP splitting, and that is bad.

Also, make sure you exit. User agents can ignore your Location header if they want. The WayBackMachine bot also has a habit of ignoring Location headers.

This does violate some recommendation (can't remember which right now) though that says user input should not be placed in a Location header. But so long as you don't allow \n, you should be OK. :)

Sign up to request clarification or add additional context in comments.

2 Comments

wow thanks it works...but can we set it to run after some seconds ? redirect after 20 seconds like that ?
@Navi You'd need to use some HTML in your head element I think. I'm not sure if it works as a header (despite the HTML saying http-equiv).
1

Can't you just do this:

header("Location: {$_POST['url']}\r\n");

? Edit: Sorry for the really late response, should have checked.

Edit 2: If you want to redirect after twenty seconds, add this in the <head> section:

<meta http-equiv="refresh" content="20; <?php echo $_POST['url']; ?>" />

And make sure to include a link back to the page somewhere in your body for accessibility:

<a href="<?php echo $_POST['url']; ?>">Go back</a>

1 Comment

thanks it works minitech.....but can we set it to run after some seconds ? redirect after 20 seconds like that ?
0
header('Location: '.$_POST['url']);

Comments

0

Can't you simply concatenate the value passed by the post with the location string to redirect? Or am I not understanding the problem correctly?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.