Have you ever wanted to retrieve a page URL during web development and check if it contains a specific string to branch out the display content or processing? For instance, you may want to load different scripts on specific pages or toggle displayed content. This blog explains how to retrieve the page URL using PHP and determine if it contains a specified string to process accordingly.
When Is URL Determination Necessary?
Examples include loading different JavaScript files on multiple pages or displaying a unique title for certain pages for SEO purposes. Dynamically branching out processing based on the URL in such cases can be highly effective. The PHP method introduced here achieves this with simple code, so feel free to use it as a reference.
Retrieve the Page URL and Check for a Specified String Using PHP
First, here is the PHP code to retrieve the URL of the currently accessed page. This code uses the $_SERVER variable to get the current URL and displays it in the browser.
<h2>The current URL is <br>「<b>
<?php echo (empty($_SERVER['HTTPS']) ? 'http://' : 'https://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
</b>」<br></h2>
This code determines whether the protocol is HTTP or HTTPS, concatenates the host name and request URI, and retrieves the URL. It is helpful for completely retrieving the current page URL.
CSS Example
This section is not critical and can be modified as needed.
<style type="text/css">
<!--
body {
font-family: Verdana, "Hiragino Kaku Gothic Pro", "ヒラギノ角ゴ Pro W6", Osaka, "MS Pゴシック", Arial, sans-serif;
padding: 0;
margin: 0;
}
h1 {
font-size: 18px;
font-weight: bold;
line-height: 1.2em;
padding: 15px 0 30px 0;
}
h2 {
font-size: 16px;
font-weight: normal;
padding-bottom: 40px;
line-height: 1.6em;
}
#idWrap {
width: 900px;
margin: 0 auto;
text-align: center;
}
.red {
font-size: 16px;
color: #FF0000;
line-height: 1.8em;
}
.bk {
font-size: 16px;
color: #FF0000;
line-height: 1.8em;
}
#idWrap b {
font-size: 22px;
}
-->
</style>
Check If the URL Contains a Specific String
Next, here is a sample PHP code to check if the URL contains a specific string and change the displayed content accordingly. This example checks whether the string ?moziretsu
is included in the URL.
<?php
if(strstr($_SERVER["REQUEST_URI"], "?moziretsu")):
?>
<div class="red">The search string<br>「<b>?moziretsu</b>」<br>is <b>present</b> in the URL.</div>
<br>
<br>
<a href="/demo/php/20210618-strstr-request-uri">If the search string「?moziretsu」is absent>></a>
<?php else: ?>
<div class="bk">The string<br>「<b>?moziretsu</b>」<br>is <b>not present</b> in the URL.</div>
<br>
<br>
<a href="/demo/php/20210618-strstr-request-uri/?moziretsu">If the search string「?moziretsu」is present>></a>
<?php endif; ?>
This code uses the strstr
function to check if the string ?moziretsu
is present in the current page URL. If it is present, a specific message is displayed; otherwise, a different message is shown. This method is often used for campaign pages or filter search pages.
Flow of PHP Processing
This PHP processing operates in the following steps:
- Retrieve the current page URL.
- Search the retrieved URL for the specified string (in this example,
?moziretsu
) using thestrstr
function. - Branch the displayed content based on whether the string is present or not.
Using this technique, you can dynamically display content or load different scripts for specific pages. It is highly versatile and can be applied in various projects.
Display Results of Determining the URL String
By using this code, you can confirm how it is displayed. Below are examples of the results:
If the search string「?moziretsu」is absent>>
If the search string「?moziretsu」is present>>
Clicking these links allows you to verify the behavior when ?moziretsu
is included or not included in the URL. This helps you understand how content toggles or dynamic processing works on different pages.
Summary
Using the method introduced here, you can easily retrieve a page URL and determine if it contains a specific string. This approach is highly effective for displaying different content based on website hierarchy or loading dynamic scripts. While the method uses basic PHP syntax, its applications are extensive and can be utilized in various projects. Try incorporating this into your website.
※ Please use it at your own discretion when referencing this method.