0

This is really hard to explain, I'm trying to remove a query string from my url after I've displayed the data from the string.

in file1.php I have a redirection to file2.php after some checks

header('Location: file2.php?var=somemessage');

in file2.php I echo $_GET['var']; to display the message then use javascript to remove the query string from url

if (location.href.match(/\?.*/) && document.referrer) {
   location.href = location.href.replace(/\?.*/, '');
}

The problem here is that I get a notice of undifined index: var in file2.php. How do I get around this to remove the query string and still display the message?

I don't want to pass the message from a session.

Deon Dazy
  • 153
  • 1
  • 10
  • 1
    If you set `location.href`, you will also navigate to the new url you set (the one without `var`. Because of that, the PHP script will be executed again, but without the string, hence the error. Have a look at the history API, and especially [history.pushState](https://developer.mozilla.org/en-US/docs/Web/API/History_API#Adding_and_modifying_history_entries). Another possibility is to set a cookie first, and read back the cookie after the redirect. – GolezTrol Oct 01 '15 at 22:54
  • Wow! I didn't think about that. Thanks :) – Deon Dazy Oct 01 '15 at 23:06
  • You can find more informations reading this question [javascript - Modify the URL without reloading the page - Stack Overflow](https://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page#3354511) . – Chocolord Aug 21 '18 at 08:01

0 Answers0