如何检查网址是否为主页

时间:2017-12-03 23:53:05

标签: php request-uri

找到了这个答案If url is the homepage (/ or index.php) do this,但它并没有完全回答我的问题。

我有一个学校网站索引,显示我的所有文件夹到不同的作业。所以我的主页实际上是domain / folder / index.html

所以当我问$ currentpage == /midterm/index.php ||时/ midterm /它总是触发为真,即使我在/midterm/add.php

<?php
$homeurl = '/midterm/index.php';
$homepage = '/midterm';
$currentpage = $_SERVER['REQUEST_URI'];

if ($currentpage == $homeurl || $homepage) {
    echo '<div class="hero"></div>';
}

2 个答案:

答案 0 :(得分:1)

Your problem is in your conditional: if ($currentpage == $homeurl || $homepage) will always return true because you are stating that $currentpage must equal $homeurl, OR just simply $homepage. Adding brackets helps showcase this:

if ( ($currentpage == $homeurl) || ($homepage) )

Because $homepage is set, it's truthy ,并评估为{{1} }。由于truetrue),条件的一部分只需要OR,因此条件会返回||整体。

要解决此问题,您需要检查true是否等于$currentpage $homeurl是否等于$currentpage:< / p>

$homepage

其中,添加括号,评估为:

if ($currentpage == $homeurl || $currentpage == $homepage)

希望这有帮助! :)

答案 1 :(得分:0)

It's because your if statement is checking to see if $homepage is set, not if $homepage is equal to $currentpage.

if ($currentpage == $homeurl || $currentpage == $homepage) {
    echo '<div class="hero"></div>';
}