Navi Gamage
Navi Gamage

Reputation: 2783

Entering HTML form data in PHP code?

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?

Upvotes: 1

Views: 156

Answers (4)

Ry-
Ry-

Reputation: 225281

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>

Upvotes: 1

alex
alex

Reputation: 490657

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

Upvotes: 2

doubter
doubter

Reputation: 1

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?

Upvotes: 0

Trey
Trey

Reputation: 5520

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

Upvotes: 0

Related Questions