SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    localhost detection

    I have a google adsense javascript in my html pages for the ads, I only want my ads to show on my live online site, not my local virtualhost. So instead of having to manually paste the js script into all my html pages each time I upload, I want to have the javascript as a file on my server and a php function on my page that says if local server, do nothing(or load false image), if online server, load javascript and show ads. Hopefully loading the javascript ad with php will not cause problem with adsense tracking either.

    The main bit of code I need is for detecting wether it is on local server or real online server, but if anyone has the full script for importing external scripts combined with the if and else statements for local vs live then that would be brilliant, otherwise I can research the rest myself.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Over-simplified, but it should be enough to get you started.
    HTML Code:
    <html><head><title>get host</title>
    <script type='text/javascript'>
    var hst = location.host;
    alert(hst);
    </script>
    <body></body></html>

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something along the lines of
    PHP Code:
    $is_dev_server file_exists('C:\local.flag'); 

  4. #4
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I presume that would be using the javascript for host detection instead of php?
    I have not thought of that. Seems simpler but I am wary of editing the javascript that adsense sent me, I don't want to screw it up.

    Thanks though.

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    How are you currently using PHP to get the adsense code into your pages? Maybe you can do something like
    PHP Code:
    if ($_SERVER['HTTP_HOST'] != 'localhost')
    {
    include 
    'get_adsense_script_tag.php';

    or
    PHP Code:
    if ($_SERVER['HTTP_HOST'] != 'localhost')
    {
    ?>
    <script src="....
    <?php
    }else{
    ?>
    <other tag?>
    <?php
    }
    Last edited by Mittineague; Sep 11, 2008 at 13:58. Reason: syntax error

  6. #6
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am currently manually copying and pasting the adsense js script into my pages. I have just started learning js and php.

    Thanks for the php code, I will try it out.

    On thinking though, having just javascript would probably be simpler... if I can do it without editing the adsense code.

    Not sure on the syntax but I want something like (in javascript code):

    if host =local
    {load another image}

    if host =online
    process {adsense javascript}


    -you mentioned var hst = location.host;
    -would I replace "location.host" simply with the url of my local/live host

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by cyjetsu View Post
    -you mentioned var hst = location.host;
    -would I replace "location.host" simply with the url of my local/live host

    No. location.host is the name of the field that will contain 'localhost' when you are viewing your site locally and will contain the domain name of your site when you are viewing it online.

    Code:
    if (location.host == 'localhost') {
    // code for local version goes here
    } else {
    // code for online version goes here
    }
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Maybe you could give a script tag an id with an empty src value. Then use javascript to assign the adsense URL value?
    HTML Code:
    <script id='adsns' src=''></script>
    <script type='text/javascript'>
    if (location.host == 'localhost'){
    // local version code
    document.getElementById('adsns').src = "[path to other js file?]";
    }else{
    document.getElementById('adsns').src = "[path to adsense file]";
    }
    </script>
    Not sure how it would work with page loading sequence, but you could try it. It should work better than document.write()ing the adsense script tag.

  9. #9
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks that looks pretty good and makes sense.

    Would this also work?:

    if (location.host == 'localhost'){
    *** display my other image ***
    }

    else{
    *** adsense script here ***
    }
    </script>

  10. #10
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    AFAIK, a src value to an image file wouldn't work in a script tag. But if the javascript file displayed another image (IMHO text would be better as only those using your computer would see it anyway), it should work OK.

  11. #11
    Non-Member
    Join Date
    Jul 2005
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you can use java you can detect localhost name and local ip address

    import java.net.*;

    class Information
    {
    public static void main(String [] aa)

    {
    try{

    System.out.println(InetAddress.getLocalHost().getH ostName());
    System.out.println(InetAddress.getLocalHost().getH ostAddress());


    }catch(Exception e){}

    }

    }


    compile it with save and compile it

    compile as follows:
    javac Information.java

    Run as follows:

    java Information

    note: If you face any problem please let me know...

  12. #12
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by pachecus View Post
    if you can use java you can detect localhost name and local ip address.

    But why would you bother to do that when the JavaScript version is so much shorter and doesn't require a plugin?
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •