1

e.g. i have page with url http://mysite.com?page=3&var=10 also there is form on page.

When form submitted there some actions in php but i need to remove this ?page=3&var=10 after form was submitted somehow is there way compatible with all browsers trough PHP without mod_rewrite?

3
  • Using POST instead of GET will hide the parameters from your submitted form. Commented Sep 28, 2011 at 19:12
  • why not convert the form to use POST then there will be no parameters in the url in the first place. or just redirect afterwards. Commented Sep 28, 2011 at 19:13
  • This parameters come from page pagination not form. Commented Sep 28, 2011 at 20:07

6 Answers 6

5

This is an old topic, but just in case anyone else is searching for this in the future, you can use the javascript replaceState to change the history and browser bar label. A simple php function to do this:

function set_url( $url )
{
    echo("<script>history.replaceState({},'','$url');</script>");
}

Then would simply call this function with the desired url (presumably dropping the post variables):

set_url("http://example.com");

A page reload or a back after calling another page will now have the new url location in the history.

I think that using POST may be a more elegant solution, but if you must use GET this is a work around.

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

Comments

2

If you're using action=index.php, then all values will be posted to index php, ?page=3&var=10 will be automatically removed.

If you want to post to the same page you can either use 'action=index.php?page=3&var=10' or action=<?php echo $_SERVER['PHP_SELF'] ?>

You can check at the beginning of the page if something submitted and then redirect to whatever you want with header('Location: http://www.example.com/'); More about header function http://php.net/manual/en/function.header.php

1 Comment

PHP_SELF is the name of the php script as opposed to the path of the actual uri as found in $_SERVER['REQUEST_URI'] they are completely different in nature and purpose. Thus PHP_SELF is not useful information in changing the original url when a single php script can represents thousands of different url paths... down voting.
1

Yeah, the solution is quite simple (even if not really SEO friendly):

<?php
 header("Location: http://mysite.com")
?>

just for information...why do you need it?

3 Comments

If i use header i have to kill the script in the middle by using exit; or die;
Even if i dont its still just redirects to empty page and restarts it from scratch without $_POST from form
Why you cant simply process the $_POST data BEFORE redirecting the browser?
1

use parse_str to get the query string as an associative array that is easy to modify. Then use http_build_query to convert the associative array into a query string.

$queryString = $s['QUERY_STRING'];
$params = array();
parse_str($queryString, $params);
//change $params as needed
$queryString = http_build_query($params);
if ($queryString) {
  $queryString = '?'.$queryString;
}
return preg_replace("/\\?.*/s","",$s['REQUEST_URI']).$queryString;

preg_replace("/\\?.*/s","",$s['REQUEST_URI']) removes the original query string allowing you to replace it.

Comments

0

Does this work for you?

header('Location:/');

Comments

-1

mod_rewrite cannot affect what's displayed in the user's browser address bar, UNLESS the rewrite does an externally visible redirect. Otherwise it only rewriting things within the webserver, and that's invisible to the user.

If you want to affect the user's address bar, you'll have to do a redirect via header('Location: ...') after the form's finished processing.

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.