Q:

How to get current page URL in PHP

0

How to get current page URL in PHP

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the PHP $_SERVER Superglobal Variable

You can use the $_SERVER built-in variable to get the current page URL in PHP. The $_SERVER is a superglobal variable, which means it is always available in all scopes.

Also if you want full URL of the page you'll need to check the scheme name (or protocol), whether it is http or https. Let's take a look at an example to understand how it works:

<?php
$uri = $_SERVER['REQUEST_URI'];
echo $uri; // Outputs: URI
 
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
 
$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $url; // Outputs: Full URL
 
$query = $_SERVER['QUERY_STRING'];
echo $query; // Outputs: Query String
?>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP  and MySQL Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to create a string by joining the values of an... >>
<< How to compare two strings in PHP...