Quick PHP Question

nam6641

Supreme Member
Joined
Nov 15, 2008
Messages
1,470
Reaction score
926
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.
 
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.
 
Last edited:
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.
 
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:
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.
 
This really would be a tough task to do. You may like to do some other work. I think that you should be.
 
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
 
As a workaround if it fits your needs you can do it with javascript and jsonp to load your data off a different domain.
 
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

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.
 
Back
Top