How-to: Get Current Url in PHP with or without Query String
In each PHP website, we need to find out the current URL of the page, and here’s the PHP code to find out the current URL of the page a user is browsing with or without the query string.
function currentUrl( $trim_query_string = false ) {
$pageURL = (isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on') ? "//" : "//";
$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
if( ! $trim_query_string ) {
return $pageURL;
} else {
$url = explode( '?', $pageURL );
return $url[0];
}
}
I like to keep such functions in a helpers class so I can use them anywhere in the app. To use it in a PHP class you can use the following code:
public function currentUrl( $trim_query_string = false ) {
$pageURL = (isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on') ? "//" : "//";
$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
if( ! $trim_query_string ) {
return $pageURL;
} else {
$url = explode( '?', $pageURL );
return $url[0];
}
}

Winery Website Development Services
Jewellery Website Development Services
Automotive Website Design Services
Drupal Website Development
Joomla Website Development
Webflow Development Services
Framer Website Development Services
WIX Development Services
Squarespace Development Services
AngularJS Website Development
ReactJS Development Services
IOS Apps Developmnet
Cross Platform App Development
Flutter App Development
Xamarin Apps Developmnet
Progressive Apps Developmnet
Prestashop Development Service
Neto (Maropost) Commerce Development Service
Shopify Plus Agency
Shopify Cro Services
Search Engine Optimization
Social Media Marketing
Paid Media Advertising
Google Ads Services
SEO Services India
Content Marketing Services
Email Marketing Services
SEO Pricing
Website Design Services India
Website Design Services USA Canada
Website Design Services British Columbia
Graphics Design Services
Logo Design
Figma To HTML5
Hire Remote Developers
Wearable App Development
Leave a Reply