大家好我想用PHP创建一个动态网页,我认为$_SERVER['PATH_INFO']
有问题。每次我按回家或关于它将index.php.home或index.php.about附加到网址。这是我的代码:
<body>
<div style="width:800px; height:auto;">
<nav>
<a href="index.php/home">home</a>
<a href="index.php/about">about</a>
</nav>
<?php
$path = substr($_SERVER['PATH_INFO'],1);
echo $path;
if($path==""){
$path = "home";
}
if($path == "home"){
?>
<h1> Home Page </h1>
<?php
}
elseif($path == "about"){
?>
<h1> About Page</h1>
<?php
}else{
?>
<h1> Page Not Found </h1>
<?php
}
?>
</div>
</body>
我可以帮忙解决这个问题吗?
答案 0 :(得分:2)
你应该使用$ _GET变量。如果你有一个像
这样的网址index.php?p=categories&sp=specific_category
然后你会:
$_GET['p'] will be categories
$_GET['sp'] will be specific_category
所以
<body>
<div style="width:800px; height:auto;">
<nav>
<a href="index.php?p=home">home</a>
<a href="index.php?p=about">about</a>
</nav>
<?php
if(!isset($_GET['p']) || $_GET['p'] == 'home'){
?>
<h1> Home Page </h1>
<?php
}
elseif($_GET['p'] == "about"){
?>
<h1> About Page</h1>
<?php
} else {
?>
<h1> Page Not Found </h1>
<?php
}
?>
</div>
</body>
答案 1 :(得分:0)
像这样放一条完整的路径
<?php
$protocall="http://www.";
$host= $_SERVER['SERVER_NAME'];
$ext=".com";
$site=$protocall.$host.$ext;
?>
<nav>
<a href="<?php echo $site; ?>/index.php/home">home</a>
<a href="<?php echo $site; ?>/index.php/about">about</a>
</nav>
在全局文件中定义$ site