1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP - if URL is Homepage, then do this...

Discussion in 'PHP' started by espmartin, Oct 2, 2007.

  1. #1
    Hello,

    I need to ad a menu link to the homepage's navigation ONLY - and not have
    that link sitewide.
    SEMrush
    How do I check via PHP if the page loaded is the homepage URL, and if so,
    generate that link? If the page loaded is a sub-page, then that link should
    NOT be there.

    EXAMPLE:
    • Homepage + this link
    • Sub-page w/o link
    I need this PHP code to be within an included sidebar.php file that sits in a
    sub folder:
    • /includes/sidebar.php
    Code examples would be very appreciated!
     
    espmartin, Oct 2, 2007 IP
    SEMrush
  2. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #2
    you can try using:

    
    <?
    $homepage = "/index.php";
    $currentpage = $_SERVER['REQUEST_URI'];
    if($homepage==$currentpage) {
    echo "links links links links";
    }
    ?>
    
    Code (markup):
     
    smatts9, Oct 2, 2007 IP
    bogart likes this.
  3. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if ($_SERVER['PHP_SELF'] == 'index.php') {
    echo 'link just for homepage';
    }
     
    matthewrobertbell, Oct 2, 2007 IP
  4. espmartin

    espmartin Well-Known Member

    Messages:
    1,137
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    110
    #4
    I'm using this, but it does NOT work:
            <!-- begin homepage ads -->
            <?php
                $homepage = "/index.php";
                $currentpage = $_SERVER['REQUEST_URI'];
                if($homepage==$currentpage) {
                echo '<!-- begin link-->';
                echo '<li><a href="http://www.domain.com/" rel="external">Homepage only link</a></li>';
                echo '<!-- end link -->';
                }
            ?>
            <!-- end homepage ads -->
    
    PHP:
    It doesn't put that link on the page (or any page). Did I do something wrong?
     
    espmartin, Oct 2, 2007 IP
  5. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #5
    is your homepage under index.php?
     
    smatts9, Oct 2, 2007 IP
  6. espmartin

    espmartin Well-Known Member

    Messages:
    1,137
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Yes it is...When I tried that, the link does not show
     
    espmartin, Oct 2, 2007 IP
  7. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #7
    try putting echo $currentpage; , put it right after the $currentpage =, line and goto your homepage to see what it displays.
     
    smatts9, Oct 2, 2007 IP
  8. espmartin

    espmartin Well-Known Member

    Messages:
    1,137
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    110
    #8
    Yes - that does echo the current location for whatever page I'm on. It shows
    / - /sub-folder/ - /etc/

    So it does "know" the URL location, but it just is NOT echoing the link tag
     
    espmartin, Oct 2, 2007 IP
  9. espmartin

    espmartin Well-Known Member

    Messages:
    1,137
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    110
    #9
    I just found something...

    On my local development PC (running Apache/PHP) - that bit of code DOES work!!!!

    Just NOT working on my host...
     
    espmartin, Oct 2, 2007 IP
  10. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #10
    does it echo index.php when you are on your homepage?
     
    smatts9, Oct 2, 2007 IP
    espmartin likes this.
  11. codyrockx

    codyrockx Peon

    Messages:
    33
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Check the value of $currentpage by adding a debug line: echo $currentpage. If it does have a value there for when you visit the homepage try making $homepage that value assuming it's correctly returning the homepage for you. If it's not returning anything, it may be that your PHP installation doesn't support REQUEST_URI and you'll have to check your php settings to see what SERVER vars you can use. Check those out using phpinfo().
     
    codyrockx, Oct 2, 2007 IP
  12. espmartin

    espmartin Well-Known Member

    Messages:
    1,137
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    110
    #12
    On my local PC, yes it does. On my hosting account, no...
    Got it:

    And yes, it shows it's enabled:

    REQUEST_URI 	/phpinfo.php
    Code (markup):
     
    espmartin, Oct 2, 2007 IP
  13. codyrockx

    codyrockx Peon

    Messages:
    33
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I'm assuming then that you're homepage is at your domain rather than a page such as somedomain.com/. You'll have to check then if $currentpage = '' or index.php
     
    codyrockx, Oct 2, 2007 IP
  14. espmartin

    espmartin Well-Known Member

    Messages:
    1,137
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    110
    #14
    I believe the syntax is correct, as it DOES work on my local apache/php server.
    Just not on my web host...

    So, when I use the code listed above, my local apache server serves the ad
    on the homepage only, and sub-pages do NOT get it. On the hosting account
    however, all I get is the echo of the current URI...
     
    espmartin, Oct 2, 2007 IP
  15. codyrockx

    codyrockx Peon

    Messages:
    33
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #15
    No, definitely syntax is correct. If you're navigating to the homepage via the main domain and not /index.php it's not going to match. If the $currentpage that is being echoed is different from what you have set to $homepage, just change it to that.
     
    codyrockx, Oct 2, 2007 IP
    espmartin likes this.
  16. espmartin

    espmartin Well-Known Member

    Messages:
    1,137
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    110
    #16
    Works!!!

    Here's the total code:
    	<!-- begin homepage ads -->
        <?php
    		$homepage = "/";
            $currentpage = $_SERVER['REQUEST_URI'];
            if($homepage==$currentpage) {
            include('homepage-ads.php');
            }
        ?>
    	<!-- end homepage ads -->
    
    PHP:
    Must have been that /index.php - that I changed to just /

    You guys rock!
     
    espmartin, Oct 2, 2007 IP
    jacobbannier likes this.
  17. espmartin

    espmartin Well-Known Member

    Messages:
    1,137
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    110
    #17
    Rep added to you guys!!!
     
    espmartin, Oct 2, 2007 IP
  18. Kooza

    Kooza Peon

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #18
    I would like to do something similar to this however I would like to display a different title tag depending on the directory. Basically so that I can use the same theme for two different programs.

        <?php
            $directory = "/directory/....";
            $currentpage = $_SERVER['REQUEST_URI'];
            if($directory==$currentpage) {
            echo "<title>{SITETITLE}</title>";
            }else{
            echo "<?php wp_title(); ?></title>";
        ?>
    Code (markup):
    It would need to display
    <title>{SITETITLE}</title>
    Code (markup):
    for any page which exists in the /directory sub directory.

    I hope that makes sense?

    Cheers,
    Mike.
     
    Kooza, Feb 27, 2009 IP
  19. Kooza

    Kooza Peon

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #19
    I am sure you would just have to load the current URL into an array and if the first 10 number of characters match /directory then display <title>{SITETITLE}</title>

    Am I on the right track?
     
    Kooza, Feb 27, 2009 IP
  20. young coder

    young coder Peon

    Messages:
    302
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #20
    <!-- begin homepage ads -->
        <?php
            $currentpage = $_SERVER['REQUEST_URI'];
            if($currentpage=="/" || $currentpage=="/index.php" || $currentpage=="" ) {
            include('homepage-ads.php');
            }
        ?>
        <!-- end homepage ads -->
    PHP:
    to make it work in any condition :)
    www.yourdomain.com & www.yourdomain.com/ & www.yourdomain.com/index.php
     
    young coder, Feb 28, 2009 IP
    eCollector likes this.