开发者

How do I write a basic if statement to include code on the index page?

I'm so used to writing for Wordpress that I don't know how to do a simple PHP logic statement by itself!

For a very basic project, I'm trying to include an unordered list on the index.php page. Here's an example of code that I am trying to use:

        <?php 
        $index = $_SERVER['REQUEST_URI'];
        if ($index == "/") {
        ?><ul id="links">
            <li><a href="http://facebook.com" class="facebook">Facebook</a></li>
            <li><a href="example.php">Example</a></li>
            <li><a href="test.php">Test</a></li>
            <li><a href="page.php">Page</a></li>
        </ul>
        <?php } ?>

I know this needs to work for index pages ending with the actual URI of index.php as well as a blank suffix 开发者_开发技巧without index.php. How can I write this code to work correctly?


You need to use basename() function to get actual page name like this:

    <?php 
    $page = basename($_SERVER['REQUEST_URI']);
    if ($page == 'index.php' || $page == "/" || $page == '') {
    ?><ul id="links">
        <li><a href="http://facebook.com" class="facebook">Facebook</a></li>
        <li><a href="example.php">Example</a></li>
        <li><a href="test.php">Test</a></li>
        <li><a href="page.php">Page</a></li>
    </ul>
    <?php } ?>


It does work however if you use SCRIPT_NAME with the slashes following.

<?php 
    $page = basename($_SERVER['SCRIPT_NAME']);
    if ($page == 'index.php' || $page == "/" || $page == '') 
    { ?> 
    Hi This should show up.
    <?php }?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜