Detect Browser Language in PHP -
Using Desktop
Detect platform -
Demo 02
REMOTE_ADDR - 216.73.216.136
HTTP_CLIENT_IP - UNKNOWN
HTTP_X_FORWARDED_FOR - UNKNOWN
To demonstrate examples of using PHP with Hugo, your hosting server must have PHP support!
Before viewing the examples presented here, it is recommended to read the article - “Using PHP in Hugo”
Examples of calling PHP files in the single.php template
Executable PHP codes are located in the files:
php_test_01.php
The program detects the language of your current browser, device type (mobile/tablet/desktop) and platform (windows/android/iOS) using PHP.
Create static/php/php_test_01.php:
<?php
echo 'Detect Browser Language in PHP - ';
echo '<em><strong>' . substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) . '</strong></em>';
echo '<br>';
// Check if the "mobile" word exists in User-Agent
// $useragent = $_SERVER [ 'HTTP_USER_AGENT' ];
$useragent = $_SERVER["HTTP_USER_AGENT"];
$isMob = is_numeric(strpos(strtolower($useragent), "mobile"));
// Check if the "tablet" word exists in User-Agent
$isTab = is_numeric(strpos(strtolower($useragent), "tablet"));
//Platform check
$isWin = is_numeric(strpos(strtolower($useragent), "windows"));
$isAndroid = is_numeric(strpos(strtolower($useragent), "android"));
$isIPhone = is_numeric(strpos(strtolower($useragent), "iphone"));
$isIPad = is_numeric(strpos(strtolower($useragent), "ipad"));
$isIOS = $isIPhone || $isIPad;
if($isMob){
if($isTab){
echo 'Using <em><strong>Tablet</strong></em> Device';
}else{
echo 'Using <em><strong>Mobile</strong></em> Device';
}
}else{
echo 'Using <em><strong>Desktop</strong></em>';
}
echo '<br> Detect platform - ';
if($isIOS){
echo '<em><strong>iOS</strong></em>';
}elseif($isAndroid){
echo '<em><strong>ANDROID</strong></em>';
}elseif($isWin){
echo '<em><strong>WINDOWS</strong></em>';
}
?>
Usage:
{{ define "main" }}
<section class="container">
<h1>{{ .Title }}</h1>
{{ readFile "static/php/php_test_01.php" | safeHTML }}
...
{{ .Content }}
</section>
{{end}}
php_test_02.php
The program determines the client’s IP address using PHP
$_SERVER[‘REMOTE_ADDR’]determines the real IP address of the connecting party. This is the most reliable value you can find.$_SERVER[‘HTTP_CLIENT_IP’]determines the IP address when the user is on public Internet services, but this value is easy to fake.$_SERVER[‘HTTP_X_FORWARDED_FOR’]determines the IP address of the user when he/she is behind a proxy server (this value is easy to fake). Createstatic/php/php_test_02.php:
<?php
echo "REMOTE_ADDR - ";
if (!empty($_SERVER['REMOTE_ADDR'])) {
echo '<em><strong>' . $_SERVER['REMOTE_ADDR'] . '</strong></em>';}
else {
echo "<em><strong>UNKNOWN</strong></em>";}
echo '<br>';
echo "HTTP_CLIENT_IP - ";
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
echo '<em><strong>' . $_SERVER['HTTP_CLIENT_IP'] . '</strong></em>';}
else {
echo "<em><strong>UNKNOWN</strong></em>";}
echo '<br>';
echo "HTTP_X_FORWARDED_FOR - ";
if (!empty($_SERVER['HTTP_X_FORWARDED'])) {
echo '<em><strong>' . $_SERVER['HTTP_X_FORWARDED_FOR'] . '</strong></em>';}
else {
echo "<em><strong>UNKNOWN</strong></em>";}
echo '<br>';
?>
Usage:
{{ define "main" }}
<section class="container">
<h1>{{ .Title }}</h1>
...
{{ readFile "static/php/php_test_02.php" | safeHTML }}
{{ .Content }}
</section>
{{end}}
Example of accessing site variables in shortcode
Create a shortcode layouts/shortcodes/siteurl.html:
{{ .Page.Site.BaseURL }}
Usage:
---
# front-matter
---
1. Go to your site's url {{< siteurl >}}
2. ...
Result:
1. Go to your site's url https://www.webpractica.ru/
2. ...
Example of passing the front matter parameter to a shortcode
Create shortcode layouts/shortcodes/param_page.html:
{{- with index .Page.Params (.Get 0 ) -}}
{{- . -}}
{{- end -}}
Usage:
---
# front-matter
---
1. Title: {{% param_page "title" %}}
2. Authors: ({{% param_page "author" %}})
Result:
1. Title: Examples of using PHP with Hugo
2. Authors: [tim]
Example of passing a site parameter to a shortcode
Create shortcode layouts/shortcodes/param_site.html:
{{- with index .Page.Site.Params (.Get 0 ) -}}
{{- . -}}
{{- end -}}
Usage:
---
# front-matter
---
1. copyright: ({{% param_site "copyright" %}})
2. description: {{% param_site "description" %}}
Result:
1. copyright: © WebPractica, 2024
2. description: We'll help you build a fast and secure website using Hugo, a static site generator written in Go.
Example of calling PHP in shortcode
Create shortcode layouts/shortcodes/php.html:
<div {{ if ( .Get "class" ) }} class='{{ .Get "class" }}' {{ end }} {{ if ( .Get "title" ) }} title='{{ .Get "title" }}' {{ end }}>
{{ .Inner | safeHTML }}
</div>
Usage:
{{< php >}}
<?php
echo 'Your browser: <br>' . $_SERVER['HTTP_USER_AGENT'];
?>
{{< /php >}}
Result:
Your browser:Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Example of accessing Hugo variables and parameters from PHP code in a shortcode
In the shortcode php, to obtain the values of Hugo variables and parameters, we use the shortcodes: param_page, param_site and siteurl (the shortcode code is given above).
Usage:
{{% php class="php" title="Demo PHP" %}}
<?php
$mem_php_title='{{% param_page "title" %}}';
echo 'Title: ' . '{{% param_page "title" %}}';
echo '<br>Authors: ' . '{{% param_page "author" %}}' . 'on the site'. '{{< siteurl >}}';
echo '<br>Copyright: ' . '{{% param_site "copyright" %}}';
?>
<br><br>
{{% /php %}}
Result:
Authors: [tim]on the sitehttps://www.webpractica.ru/
Copyright: © WebPractica, 2024
Example of token creation:
An example is discussed on the forum - How can I include PHP code in Hugo?
Add before the HTML code in baseof.php and save the string in the session variable ($_SESSION[’token’]):
<?php
session_start(); if (empty($_SESSION['token'])) {$_SESSION['token'] = bin2hex(random_bytes(32));}
?>
Execute the php shortcode:
{{< php >}}
<?php
if (empty($_SESSION['token'])) {$_SESSION['token'] = bin2hex(random_bytes(32));}$token = $_SESSION['token']; $_SESSION["author"] = "[tim]"; echo '<small>Token: ' . $token. '<br>'; print_r($_SESSION) . '</small>';
?>
{{< /php >}}
Example result:
Token: e7d88d1e9f3146d7dc8a061f56a39fefeb4e013c6b25c3134a26e1b5b6bbc359
Array ( [token] => e7d88d1e9f3146d7dc8a061f56a39fefeb4e013c6b25c3134a26e1b5b6bbc359 [author] => [tim] )
Example of PHP usage in Hugo template
Put PHP in Single.php template
<?php
echo 'Your browser: <br>' . $_SERVER['HTTP_USER_AGENT'];
echo '<br>Author: ' . '{{ .Params.author }}';
?>
Result:
Your browser:Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Author: [tim]