我提前为这个问题的措辞道歉,如果管理员能说出来的话,我也可以放心。我可能会觉得这很难解释。
我想创建一个页面,它使用URL的一部分来创建页面的"自定义"部分。
例如www.example.com/hello=Derek
标题应该是"你好,德里克"或者你在"?="后面加的任何字。我知道有几个网站使用这个,我想知道我该怎么做。
您想到的是查询参数,它们的形式为key=value
,多个参数由&
分隔,普通页面URL由?
分隔。因此,在您的情况下,它将是www.example.com/hello?name=Derek
。
至于如何在PHP中显示它,应该由以下人员完成:
<?php
echo 'Hello ' . htmlspecialchars($_GET["name"]);
?>
如果www.example.com?hello=Derek
对您是可接受的,您可以在index.php中使用以下代码:
<?php
// Initialize the name variable, so it can be used easily.
$name = '';
// Check if a name was given. If so, assign it to the variable.
// The leading space is there to have a space between 'Hello' and the name.
// If you don't pass a name, the text will just say Hello! without space.
if (isset($_GET['hello'])) {
$name = ' ' . $_GET['hello'];
}
// Personal opinion: Don't echo big chunks of HTML. Instead, close the PHP tag, and
// output the plain HTML using < ?= ... ? > to insert the variables you've initialized
// in the code above.
?>
<h2>Hello<?=$name?>!</h2>