Insert script only in the index page

Discussion in 'PHP' started by Andres_BBB, Sep 6, 2012.

  1. #1
    Hi.

    I am looking for the way to insert a Jquery script just in the index:

    I have tried this two possibilities so far:

    <?php
    $homepage = "/";
    $currentpage = $_SERVER['REQUEST_URI'];
    if($homepage==$currentpage) {
    echo '<script type="text/javascript" src="/templates/zone_2/javascript/jquery-file.js"></script>';
    }
    ?>

    and

    <?php
    $homepage = "/index.php";
    $currentpage = $_SERVER['REQUEST_URI'];
    if($homepage==$currentpage) {
    echo '<script type="text/javascript" src="/templates/zone_2/javascript/jquery-file.js"></script>';
    }
    ?>


    The fact is that the index ends up in .com. Neither .com/ nor .com/index.php

    So these codes do not work.

    The idea is that the script is meant to be just in the index page, but not in any of the internal sections.

    Any idea?

    Thanks.
     
    Andres_BBB, Sep 6, 2012 IP
  2. Syndication

    Syndication Active Member

    Messages:
    351
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    90
    #2
    if (strstr($_SERVER['PHP_SELF'],"index.php")) {
    echo "This is index.php";
    }
     
    Syndication, Sep 6, 2012 IP
  3. Andres_BBB

    Andres_BBB Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Sorry Syndication, but it does not work.

    I was very specific: there is no index.php in my site.

    The index page is like this: www.thewebpage.com

    Nothing more.
     
    Andres_BBB, Sep 7, 2012 IP
  4. matt_62

    matt_62 Prominent Member

    Messages:
    1,827
    Likes Received:
    515
    Best Answers:
    14
    Trophy Points:
    350
    #4
    are you using a framework like code igniter or something?

    i think your way could work, however set up an index.html file and redirect it to index.php
    here is a site that uses this style of redirect. and it forces the site to become yourdomain/index.php and then your script might work,
     
    matt_62, Sep 7, 2012 IP
  5. Andres_BBB

    Andres_BBB Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    My page is a Joomla" with SEO-friendly urls.

    How could I set that redirection?

    Thanks very much.
     
    Andres_BBB, Sep 7, 2012 IP
  6. matt_62

    matt_62 Prominent Member

    Messages:
    1,827
    Likes Received:
    515
    Best Answers:
    14
    Trophy Points:
    350
    #6
    This is the source code for the page i mentioned before:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>yoursite.com</title> <meta http-equiv="REFRESH" content="0;url=http://www.yoursite.com/index.php"></HEAD> <BODY> </BODY> </HTML>
    The code only works as most servers look for index.html before index.php, if your server has it in a different order (unlikely) then this wont work for you.

    I have no experience with joomla, and i cant guarantee that it will work for you. Please let me know if or how you get it to work.
     
    matt_62, Sep 7, 2012 IP
  7. Andres_BBB

    Andres_BBB Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    Hi matt_62.

    Yes; this time it did work.

    Many thanks.
     
    Andres_BBB, Sep 7, 2012 IP