1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Quick PHP Question

Discussion in 'PHP & Perl' started by nam6641, Sep 27, 2012.

  1. nam6641

    nam6641 Supreme Member

    Joined:
    Nov 15, 2008
    Messages:
    1,476
    Likes Received:
    914
    Location:
    East Coast
    My coding knowledge is small so this may be a simple question... appreciate any replies.

    I have a php code to rotate 3 banners based on IP address (if US show this, if UK so that, else show something else).

    I want to use this code across 10+ sites.

    I would like to save the code on one site and then use something like an include code on all other sites, this way if I want to change the banners I just need to edit one file and everything else updates automatically.

    That's my question, can I do this, or will I have trouble using something like include when it references a file located on a different domain/server.
     
  2. cgimaster

    cgimaster Power Member

    Joined:
    Jun 30, 2012
    Messages:
    525
    Likes Received:
    311
    Gender:
    Male
    You can't include a php file that is NOT located in the same server as it will not be able to render the source code into the php you are calling it from it will give you just the HTML or response it produces. While you may be able to use allow_url_include it relies on being able to edit the php.ini as well as if your host allows you to.

    However you could use a different approach, for example, make a include php file that will read a XML or TEXT file or even connect to a MySQL and read data.

    This way you can centralize the information it needs to display in 1 place and change it from one place.
     
    • Thanks Thanks x 1
    Last edited: Sep 27, 2012
  3. fatboy

    fatboy Elite Member

    Joined:
    Aug 13, 2008
    Messages:
    1,618
    Likes Received:
    3,227
    Occupation:
    Retired
    Location:
    Old Peoples Home
    You can do it, but from a security point of view its a big no no.
    To do it, you must be able to edit the php.ini file and set allow_url_include to on.
     
    • Thanks Thanks x 1
  4. tripper_john_md

    tripper_john_md Newbie

    Joined:
    Feb 21, 2011
    Messages:
    40
    Likes Received:
    35
    Location:
    Southern Germany
    Just use a php file as image.

    banner.php on example1
    PHP:
    <?php
    header
    ('Content-Type:image/jpeg'); //change to correct image type
    $loc=get_location_of_ip($_SERVER['REMOTE_ADDR']); //some function to get the location of the visitor's IP
    if ($loc=='US') {
     echo 
    file_get_contents('banner_us.jpg');
    }
    elseif (
    $loc=='UK') {
     echo 
    file_get_contents('banner_uk.jpg');
    }
    else {
     
    //do something else
    }?>
    Now use it like this everywhere you want:
    Code:
    <img src="http/example1/banner.php" />
    You could even use URL-Rewriting to use a normal image-extension.


    Note: shortened the URI because I can't use links
     
    Last edited: Sep 28, 2012
  5. bufferoverflow

    bufferoverflow Newbie

    Joined:
    Sep 29, 2012
    Messages:
    24
    Likes Received:
    0
    Well using this kind of a script has a drawback. Because there's any glitch on the main server where your PHP file is hosted. All your 10 domains will suffer. So Try not to do this kind of a thing.
     
  6. queenmery

    queenmery Power Member

    Joined:
    Jan 18, 2011
    Messages:
    501
    Likes Received:
    30
    Occupation:
    Student
    Location:
    BANGLADESH
    This really would be a tough task to do. You may like to do some other work. I think that you should be.
     
  7. misulicus

    misulicus Regular Member

    Joined:
    Jan 31, 2010
    Messages:
    251
    Likes Received:
    118
    Home Page:
    Websites to use it on:


    Code:
    $banner = file_get_contents('http://www.mainsite.com/banner.php?ip=$_SERVER['REMOTE_ADDR']');
    echo $banner;
    

    banner.php file
    Code:
    $ip = $_GET['ip'];
    do some stuff to find location of ip
    
    
    print on the file the banner code like:
    print '<img src="bannerus.jpg">';
    

    then on the main site it will print the contents of the banner file wich is html code
    Untested but it should give u an idea
     
  8. xtr3m

    xtr3m Newbie

    Joined:
    Jan 7, 2011
    Messages:
    18
    Likes Received:
    6
    Occupation:
    Web developer
    Home Page:
    misulicus is right: make a small API out of it.
     
  9. sirgold

    sirgold Supreme Member

    Joined:
    Jun 25, 2010
    Messages:
    1,260
    Likes Received:
    645
    Occupation:
    Busy proving the Pareto principle right
    Location:
    A hot one
    As a workaround if it fits your needs you can do it with javascript and jsonp to load your data off a different domain.
     
  10. upl8t

    upl8t Regular Member

    Joined:
    Apr 9, 2008
    Messages:
    475
    Likes Received:
    84
    Location:
    New Scotland
    This concept works well. It keeps everything internal and leaves no footprints. The image solution can tie all your sites together which isn't a good idea.