Skip to content

Instantly share code, notes, and snippets.

@ScottKaye
Last active April 18, 2024 12:30
Show Gist options
  • Save ScottKaye/5158488 to your computer and use it in GitHub Desktop.
Save ScottKaye/5158488 to your computer and use it in GitHub Desktop.
Hide URL parameters using Javascript's history.replaceState methods. The server (PHP or whatever) will still see it of course, but this script will quickly 'hide' the parameters. Don't use this to hide anything sensitive - there shouldn't be anything sensitive in GET anyway, but I use this to hide various un-pretty things like ?error=invalidPass…
function getURLParameter(name) {
return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]);
}
function hideURLParams() {
//Parameters to hide (ie ?success=value, ?error=value, etc)
var hide = ['success','error'];
for(var h in hide) {
if(getURLParameter(h)) {
history.replaceState(null, document.getElementsByTagName("title")[0].innerHTML, window.location.pathname);
}
}
}
//Run onload, you can do this yourself if you want to do it a different way
window.onload = hideURLParams;
@Wicher100
Copy link

Wicher100 commented Feb 2, 2023

i made this, seems to do the trick:

<?php // testfilename: hidehoverparams.php
session_start();
?>
<!-- My piece -->
<script>
if (sessionStorage.params)
{
	let urlparams = sessionStorage.getItem("params");
	//clear the params
	sessionStorage.removeItem("params");
	// rename hidehoverparams.php to the file you want to call, this puts the params back in $_GET
        window.location.href = 'https://<?php echo $_SERVER['SERVER_NAME'];?>/hidehoverparams.php?'+urlparams;
}
</script>
<!DOCTYPE HTML>
<html>
<head>
	<title>hidehoverparams combined with code from @ScottKaye </title>

<!-- My piece -->
<script>
function hidehoverparams(p)
{
	sessionStorage.setItem("params", p);
}
</script>

<!-- @ScottKaye's piece -->
<script>
function persistParams() {
  const PARAMS_KEY = "__params";
  if (performance.navigation.type === performance.navigation.TYPE_RELOAD) {
    const persistedParams = sessionStorage.getItem(PARAMS_KEY);

    if (persistedParams) {
      return persistedParams;
    }
  }
  sessionStorage.setItem(PARAMS_KEY, location.search);
  return location.search;
}

function hideParams() {
  persistParams();
  history.replaceState(null, document.title, window.location.pathname);
  window.addEventListener("beforeunload", () => {
    history.replaceState(
      null,
      document.title,
      window.location.pathname + (sessionStorage.getItem(PARAMS_KEY) || "")
    );
  });
}
window.addEventListener("load", hideParams, false);
</script>

<!-- my piece -->
</head>

<body>
<a href="hidehoverparams.php" onclick="hidehoverparams('degrees=270&lib=Fotos&catid=2&imgid=30&t=1675333259#103')">to hidehoverparams.php</a>
<?php
	if(isset($_GET['degrees']))
        {
          print_r($_GET);
          $_GET = array();
        }
?>
</body>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment