简体   繁体   English

如何检查网址是否为首页

[英]How to check if url is the homepage

Found this answer If url is the homepage (/ or index.php) do this which was really helpful however doesn't fully answer my question. 找到了这个答案如果url是主页(/或index.php),这确实很有帮助,但是并不能完全回答我的问题。

I have an index of site for school that shows all my folders to different assignments. 我有一个学校网站索引,其中显示了我所有文件夹的不同任务。 so my homepage is actually domain/folder/index.html 所以我的主页实际上是domain / folder / index.html

so when I ask if $currentpage == /midterm/index.php || 所以当我问$ currentpage == /midterm/index.php ||时 /midterm/ it always triggers as true even if I am on /midterm/add.php / midterm /即使我在/midterm/add.php上,它也总是触发为true

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

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

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 . 您的问题if ($currentpage == $homeurl || $homepage)条件: if ($currentpage == $homeurl || $homepage)将始终返回true因为您声明$currentpage必须等于$homeurl或者只是$homepage Adding brackets helps showcase this: 添加括号可以帮助展示这一点:

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

Because $homepage is set, it's truthy , and evaluates to true . 因为$homepage已设置,所以它是truthy ,并且评估为true Because only one part of the conditional needs to be true due to your OR ( || ), the condition returns true as a whole. 由于您的OR|| ),因为只有一部分条件需要为true ,因此该条件整体返回true

To resolve this, you're looking to check whether $currentpage is equal to $homeurl OR $currentpage is equal to $homepage : 为了解决这个问题,你打算检查是否$currentpage等于$homeurl $currentpage等于$homepage

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

Which, with added brackets, evaluates to: 使用括号将其评估为:

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

Hope this helps! 希望这可以帮助! :) :)

It's because your if statement is checking to see if $homepage is set, not if $homepage is equal to $currentpage . 这是因为您的if语句正在检查是否设置了$homepage ,而不是如果$homepage等于$currentpage

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM