If this Page Show This Else Show This

前端 未结 5 698
-上瘾入骨i
-上瘾入骨i 2021-01-18 06:16

On all pages apart from the contact page, I want it to show the following in the inc-header.php include.

Contact

相关标签:
5条回答
  • 2021-01-18 06:46
    <?php
    if (stripos($_SERVER['REQUEST_URI'], 'contact.php')){
         echo '<p><a href="index.php">Home</a></p>';
    }
    else{
         echo '<p><a href="contact.php">Contact</a></p>';
    }
    
    0 讨论(0)
  • 2021-01-18 06:47

    There is a global variable named $_SERVER['PHP_SELF'] that contains the name of your page currently requested. Combined with basename() this should work:

    if( basename($_SERVER['PHP_SELF'], '.php') == 'contact' ) {
        // Contact page
    } else {
        // Some other page
    }
    
    0 讨论(0)
  • 2021-01-18 06:47

    You can do it with a simple if statement, but this will only work for your contact page. You could also use a simple function in your inc-header file that would work like so:

    function LinkToPageOrHome( $script, $title ){
       if ( strtolower( $_SERVER[ 'SCRIPT_NAME' ] ) == strtolower( $script) ){
           $script = 'home.php';
           $title = 'Home';
       }
       echo '<p><a href="' . $script. '">' . htmlentities( $title ) . '</a></p>';
    }
    

    It's sort of a blunt approach from a design standpoint, but you could use LinkToPageOrHome( 'page.php', 'My Page' ); in multiple templates and never worry about having a page link to itself.

    0 讨论(0)
  • 2021-01-18 06:53

    the quick and dirty solution is:

    <?php
    $current_page = 'contact';
    include('inc_header.php');
    ....
    ?>
    

    In inc_header.php:

    <?php
    if($current_page == 'contact') {
        // show home link
    } else {
        // show contact link
    }
    ?>
    
    0 讨论(0)
  • 2021-01-18 07:08
    if ($_SERVER["SCRIPT_NAME"] == '/contact.php') {
        echo '<p><a href="index.php">Home</a></p>';
    } else {
        echo '<p><a href="contact.php">Contact</a></p>';
    }
    
    0 讨论(0)
提交回复
热议问题