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!
在查看此处提供的示例之前,建议先阅读文章 “Hugo 中的 PHP 入门”
在 single.php 模板中调用 PHP 文件的示例
可执行的 PHP 代码位于以下文件中:
php_test_01.php
该程序使用 PHP 检测您当前的浏览器语言、设备类型(移动/平板电脑/台式机)和平台(Windows/Android/iOS)。
创建static/php/php_test_01.php:
<?php
echo '检测 PHP 中的浏览器语言 - ';
echo'<em><strong>'. substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) . '</strong></em>';
echo'<br>';
// 检查 User-Agent 中是否存在“mobile”一词
// $useragent = $_SERVER [ 'HTTP_USER_AGENT' ];
$useragent = $_SERVER["HTTP_USER_AGENT"];
$isMob = is_numeric(strpos(strtolower($useragent), "移动"));
// 检查 User-Agent 中是否存在“tablet”一词
$isTab = is_numeric(strpos(strtolower($useragent), "平板电脑"));
//平台检查
$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;
如果($isMob){
如果($isTab){
echo '使用<em><strong>平板电脑</strong></em>设备';
}else{
echo '使用<em><strong>移动</strong></em>设备';
}
}else{
echo '使用<em><strong>桌面</strong></em>';
}
echo '<br>检测平台-';
如果($isIOS){
echo '<em><strong>iOS</strong></em>';
}elseif($isAndroid){
echo '<em><strong>ANDROID</strong></em>';
}elseif($isWin){
echo '<em><strong>WINDOWS</strong></em>';
}
?>
用法:
{{ define "main" }}
<section class="container">
<h1>{{ .Title }}</h1>
{{ readFile "static/php/php_test_01.php" | safeHTML }}
...
{{ .Content }}
</section>
{{end}}
php_test_02.php
该程序使用 PHP 确定客户端的 IP 地址
$_SERVER[‘REMOTE_ADDR’]定义连接方的真实IP地址。这是您能找到的最可靠的值。$_SERVER[‘HTTP_CLIENT_IP’]确定用户使用公共 Internet 服务时的 IP 地址,但该值很容易被欺骗。$_SERVER[‘HTTP_X_FORWARDED_FOR’]确定用户位于代理服务器后面时的 IP 地址(该值很容易被欺骗)。 创建static/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>';
?>
用法:
{{ define "main" }}
<section class="container">
<h1>{{ .Title }}</h1>
...
{{ readFile "static/php/php_test_02.php" | safeHTML }}
{{ .Content }}
</section>
{{end}}
在短代码中访问站点变量的示例
创建 shortcode layouts/shortcodes/siteurl.html:
{{ .Page.Site.BaseURL }}
用法:
---
#front-matter
---
1. 转到您网站的网址 {{< siteurl >}}
2....
结果:
1. 转到您网站的网址 https://www.webpractica.ru/
2....
将 front Matter 参数传递给短代码的示例
创建 shortcode layouts/shortcodes/param_page.html:
{{- with index .Page.Params (.Get 0 ) -}}
{{- . -}}
{{- end -}}
用法:
---
#front-matter
---
1. Title: {{% param_page "title" %}}
2. Authors: ({{% param_page "author" %}})
结果:
1. Title: 将 PHP 与 Hugo 结合使用的示例
2. Authors: [tim]
将站点参数传递给短代码的示例
创建 shortcode layouts/shortcodes/param_site.html:
{{- with index .Page.Site.Params (.Get 0 ) -}}
{{- . -}}
{{- end -}}
用法:
---
# front-matter
---
1. copyright: ({{% param_site "copyright" %}})
2. description: {{% param_site "description" %}}
结果:
1. copyright: © WebPractica, 2024
2. description: 我们将帮助您使用 Hugo 创建一个快速、安全的网站,Hugo 是一个用 Go 编写的静态网站生成器。
在 shortcode 中调用 PHP 的示例
创建 shortcode layouts/shortcodes/php.html:
<div {{ if ( .Get "class" ) }} class='{{ .Get "class" }}' {{ end }} {{ if ( .Get "title" ) }} title='{{ .Get "title" }}' {{ end }}>
{{ .Inner | safeHTML }}
</div>
用法:
{{< php >}}
<?php
echo '您的浏览器:<br>' 。 $_SERVER['HTTP_USER_AGENT'];
?>
{{< /php >}}
结果:
Your browser:Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
从 shortcode 中的 PHP 代码访问 Hugo 变量和参数的示例
在短代码php中,为了获取Hugo变量和参数的值,我们使用短代码:param_page,param_site和siteurl(短代码代码在上面给出)。
用法:
{{% 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 %}}
结果:
Authors: [tim] on the site https://www.webpractica.ru/
Copyright: © WebPractica, 2024
创建令牌的示例:
论坛上讨论了一个例子 - [How can I include PHP code in Hugo?](https://discourse.gohugo.io/t/how-can-i-include-php-code-in-hugo/28589 “如何包含用于生成令牌的 PHP 代码”)
我们在 baseof.php 中的 HTML 代码之前添加并将该行保存在会话变量 ($_SESSION[’token’]) 中:
<?php
session_start(); if (empty($_SESSION['token'])) {$_SESSION['token'] = bin2hex(random_bytes(32));}
?>
执行 php 短代码:
{{< 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 >}}
结果示例:
Token: e7d88d1e9f3146d7dc8a061f56a39fefeb4e013c6b25c3134a26e1b5b6bbc359
Array ( [token] => e7d88d1e9f3146d7dc8a061f56a39fefeb4e013c6b25c3134a26e1b5b6bbc359 [author] => [tim] )
在 Hugo 模板中使用 PHP 的示例
将 PHP 插入 Single.php 模板中
<?php
echo 'Your browser: <br>' . $_SERVER['HTTP_USER_AGENT'];
echo '<br>Author: ' . '{{ .Params.author }}';
?>
结果:
Your browser:Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Author: [tim]