message in chosen page only

Discussion in 'PHP' started by ma3hd, Sep 8, 2010.

  1. #1
    Hello ,

    i have php script

    and i need to appear message to my visitors in all script pages ...but not in index page

    i used this code

    
    <?php
     if (basename($_SERVER["SCRIPT_NAME"]) != "index.php")
    
    {
    
    echo "<p>My wanted Message</p>";
    
    }
    
    ?>
    
    PHP:
    but still message appear in all pages
     
    ma3hd, Sep 8, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    To display the message on any page but not the index.php:

    <?php if (basename($_SERVER['SCRIPT_FILENAME']) != 'index.php'): ?>
    <p>My wanted Message</p>
    <?php endif; ?>
    PHP:
    To display a message only on the index.php:

    <?php if (basename($_SERVER['SCRIPT_FILENAME']) == 'index.php'): ?>
    <p>My wanted Message</p>
    <?php endif; ?>
    PHP:
    http://php.net/manual/en/language.operators.comparison.php


    Note: You can either use $_SERVER or the predefined constant (__FILE__) - in this case I used $_SERVER.
     
    danx10, Sep 8, 2010 IP
  3. ma3hd

    ma3hd Active Member

    Messages:
    116
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    thanks danx10 ;

    i write first code ....but the message not appear in all pages with index also !!! :(
     
    ma3hd, Sep 9, 2010 IP
  4. twitterdev

    twitterdev Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try to make a print_r of $_SERVER and post it here
     
    twitterdev, Sep 9, 2010 IP
  5. ma3hd

    ma3hd Active Member

    Messages:
    116
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    can please more explain ..i have little info in php !!1
     
    ma3hd, Sep 9, 2010 IP
  6. twitterdev

    twitterdev Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    type in your code

    echo $_SERVER['SCRIPT_FILENAME'];

    and post here what php print out
     
    twitterdev, Sep 9, 2010 IP
  7. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #7
    I use this to pull different titles when different php scripts are shown, but you could use it for anything I guess.

    example: at the top of a file say index.php I would put something like:
    
    $thispage = 'home';
    
    PHP:
    ..and then in the header section or in your case where you want the message, I would put.
    
    <?php if ( $thispage == 'home' ) {
    echo "<p>My wanted Message</p>";
    }else{ // or do something else }?>
    
    PHP:
    I know its basic, but it works just fine for me, you just have to name your php files, in your case the script page file.
     
    MyVodaFone, Sep 9, 2010 IP
  8. SEOaaron

    SEOaaron Peon

    Messages:
    107
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    By PHP script, do you mean you're using a CMS?
     
    SEOaaron, Sep 9, 2010 IP