Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryCookies and Browser Interaction

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Removing URL Parameters From Browser Address Bars

A URL parameter is information appended to a URL. The beginning of parameter information is marked by a "?" character,

As an example, with the https://example.com?name=Will URL, name=Will is the parameter information.

When the code in this article is implemented on a page, a browser arriving via a https://example.com?name=Will link URL will load the page with the https://example.com URL in the address bar.

Reasons for implementing that include:

  1. Removing affiliate codes or other information that might affect sales.

  2. Stripping out the information if it should not be included when the page is bookmarked or the link shared.

Whatever the reason might be, this article has a way to do it.

How It Works

When a page begins loading, PHP code checks to see if there are any parameters in the URL.

If yes, PHP reloads the page without the parameters.

If any database updates or cookie setting need to be done based on the parameter information, those should be done before the page is reloaded.

Here is the PHP code. No customization is required. Implementation notes follow the PHP code.

<?php
if( isset($_SERVER['QUERY_STRING']) and strlen($_SERVER['QUERY_STRING']) )
{
   if( headers_sent() ) { echo "<script>location.href='{$_SERVER['PHP_SELF']}'</script>"; }
   else { header("Location: {$_SERVER['PHP_SELF']}"); }
   exit;
}
?>

The higher in the web page source code that the PHP code is placed, the sooner during the page loading process it will kick in.

But do place the PHP code below any other PHP code that sets cookies or does anything else related to URL parameters. Note that the reload will be without parameters.

If the PHP code is placed below the point where the browser has already sent the header information to the browser, then the PHP code will publish a line of JavaScript to reload the page.

In other words, place the above PHP code as high as is feasible in the web page source code so long as it is below other PHP code that expects URL parameter information. If the placement is too low to use the PHP header() function, JavaScript will be published to affect the reload.

Like any other coding, test it before going live.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC