With PHP (Hypertext Preprocessor) scripting language, it’s possible to check if a URL (link location) matches a specified pattern, and then if matched or unmatched, perform some operation on the URL string. PHP has several built-in pre-defined variables, chief among them is REQUEST_URI from $_SERVER array, which contains information such as headers, paths, and script locations.

The ability to check and match pattern or string in the URL where the web page is served by PHP allows programmers to perform various actions or operations, such as redirect to another web page, show or hide some parts of web page on specific page, or add in additional elements such as link to homepage on non-homepage web pages.

The variable syntax to return the the URI (the permalinks after the domain name) which was given in order to access this page is “$_SERVER[‘REQUEST_URI’]”. Here’s an example code:

<?php
     $ispage = "/index.php";
     $currentpage = $_SERVER['REQUEST_URI'];

     if ($ispage==$currentpage) {
          // is the URL matches http://www.domain.com/index.php, perform the actions.
     }
?>

PHP preg_match() function can also be used to perform a regular expression match. For example:

<?php
     $is_url = preg_match ("/\bpattern\b/i", $_SERVER['REQUEST_URI']);
?>

Note: Replace pattern with actual string to match in URL. The “i” indicates ignore case, and “\b” is used to mark word boundary to find only the distinct word.

As $_SERVER[‘REQUEST_URI’] variable only return the end part (directory structure) of the URL without the http (or https) and domain name, use the following trick to reconstruct the complete page URL which is used to access the page:

<?php
$pageURL = 'http';

if ($_SERVER["HTTPS"] == "on") {
     $pageURL .= "s";
}

$pageURL .= "://";

if ($_SERVER["SERVER_PORT"] != "80") 
{
     $pageURL .= 
          $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].
          $_SERVER["REQUEST_URI"];
} else {
     $pageURL .= 
          $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
?>