语言选择按钮 URL 根据当前 URL 改编
我们的网站有两种语言版本:捷克语和英语。我们在每个页面的右上角(在 header.php 内)有一个简单的标志,它指向所有页面的捷克语索引的 index_cz.php 和英语索引的 index.php,无论当前 URL 是什么。
<a href="index_cz.php" target="_self"><img src="img/Czech-icon.png" width="64" height="64" alt="Czech language version"></a>
<a href="index.php" target="_self"><img src="img/United-Kingdom-icon.png" width="64" height="64" alt="English language version">
我想要使用 PHP 进行更复杂的操作,根据当前页面的 URL 来更改按钮的 URL。 例如:如果当前页面是“new.php”,我希望捷克语按钮现在指向“new_cz.php”,英语按钮指向“new.php”。
包含 ?id= 结尾的页面(以及与该页面关联的任何 id)使用单独的 header.php 并引用数据库中的适当 id,例如:
<a href="project_cz.php?id=<?php echo $det[0];?>" target="_self"><img src="img/Czech-icon.png" width="64" height="64" alt="Czech language version"></a>
<a href="project.php?id=<?php echo $det[0];?>" target="_self"><img src="img/United-Kingdom-icon.png" width="64" height="64" alt="English language version"></a>
你能想到适当的代码来处理标准的 php 页面?非常感谢!
Our website has two language versions, Czech and English. We have a simple flag in the top right of each page (within header.php) that directs to either index_cz.php for the Czech index and index.php for the English index for all pages regardless of the current URL.
<a href="index_cz.php" target="_self"><img src="img/Czech-icon.png" width="64" height="64" alt="Czech language version"></a>
<a href="index.php" target="_self"><img src="img/United-Kingdom-icon.png" width="64" height="64" alt="English language version">
I would like something more sophisticated using PHP that changes the URL of the buttons depending on what the current page URL is. For example: if the current page is 'new.php' I would like the Czech button to now point to 'new_cz.php' and the English button to be 'new.php'.
The pages that include ?id= endings (with whatever id is associated with that page) use a separate header.php and reference the appropriate id from the database for example:
<a href="project_cz.php?id=<?php echo $det[0];?>" target="_self"><img src="img/Czech-icon.png" width="64" height="64" alt="Czech language version"></a>
<a href="project.php?id=<?php echo $det[0];?>" target="_self"><img src="img/United-Kingdom-icon.png" width="64" height="64" alt="English language version"></a>
Can you think of the appropriate code to handle the standard php pages? Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
$_SERVER['PHP_SELF']
访问当前文件,但请确保在回显之前对其运行htmlspecialchars
以防止 XSS 攻击。也就是说,您可以 str_replace 当前文件来获取捷克语版本:
另一种方法也很简单:
You can access the current file with
$_SERVER['PHP_SELF']
, but make sure to runhtmlspecialchars
on it before echoing it to prevent XSS attacks.That said, you can then str_replace the current file to get the czech version:
the other way is easy, too: