0

Suppose my Form codes look like this

URL : localhost/my-url.php

<form action="hello.php">
...bla bla bla
</form>

I will process the data in hello.php and i want to redirect to user to same url after processing (according to above example)

localhost/my-url.php

I know we can use header but i don't know how to get that url from which form was submited :(

Googled but didn't found any use full.

Thanks.

asked Jun 24, 2014 at 12:36
2
  • pass the URL as a hidden input with the form or even better, try using ajax Commented Jun 24, 2014 at 12:38
  • You can give the previous page name in hidden value And can use that in header in next page Commented Jun 24, 2014 at 13:00

3 Answers 3

4

Add a hidden value in your form:

<input type="hidden" name="lastUrl" value="<?php echo $_SERVER['REQUEST_URI'] ?>" />

You now have the URL in $_POST['lastUrl'] data. You need to do it that complicated because $_SERVER["HTTP_REFERER"]; is send by the browser, and not all of them do this reliable.

answered Jun 24, 2014 at 12:39
Sign up to request clarification or add additional context in comments.

3 Comments

about security? will i face problem if user sets their data?
@user3703850 if the user modifies the form, you receive modified data.
Well you would redirect them to another url. But there is no harm in that, they could have just as easily inputted the address in their browser themselves.
3

You should put a hidden field in your form and set its value to current page url.

Then you submit the form and get the value of hidden field.

Then you can redirect user to hidden field (which is actually a URL of the page where you are submitting form) by using javascript or php.

answered Jun 24, 2014 at 12:42

Comments

0

You can use the

 $_SERVER["HTTP_REFERER"];

to get the original URL where the form was posted from.

Remember to escape it, if you use it however. ]

Alternatively, you can process the form using AJAX, send process things (redirection) client-side.

Note that form data can be changed and intercepted if you wish to send the URL of the page as form data.

answered Jun 24, 2014 at 12:38

10 Comments

HTTP_REFERER is transmitted by the browser. And not all of them do this!
@colburton whilst true, it's mostly reliable and somewhat secure.
I do not think, "mostly" and "somewhat" are going to cut it.
is is dependent on browser??
it's more secure than putting the data in the form itself. I say mostly because most browsers do send the data, but it is stripped out by intermediaries.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.