470,848 Members | 1,249 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

How to get url of current url from included script on different server

I have a banner script that is included in php on a website but the
actual script is hosted on a different site allowing me to only edit
one file to update the script.

I need to store in a variable the hostname that the included script is
being run on NOT the master url where it is hosted.

Everything I try gives the url of where the script is hosted and NOT
the location of the page where it is being called from which is what I
want as I need be able to tell the banner script not to show the banner
for the site which is it on.

tia

Jan 10 '06 #1
7 3173
Venturer said the following on 10/01/2006 09:52:
I have a banner script that is included in php on a website but the
actual script is hosted on a different site allowing me to only edit
one file to update the script.

I need to store in a variable the hostname that the included script is
being run on NOT the master url where it is hosted.


I'm assuming you're include()-ing the script as something like:

include("http://remote.server.example.com/script.php");

Is the remote server set to execute PHP or not?

* If it is, then script.php is being run on the remote server, not the
local server, so you're actually include()-ing the *output* of
script.php, not the PHP code itself. In which case, you will need to
either pass the hostname in the request for script.php, e.g.:

include("http://remote.server.example.com/script.php?host=http://local.example.com/mainscript.php");
* If the remote server is not set to execute PHP, then script.php will
be executed on the local server, so there should be no problem. However,
this configuration is a potential security risk, as anyone can request
script.php and see the code.
--
Oli
Jan 10 '06 #2
Oli Filth wrote:
Venturer said the following on 10/01/2006 09:52:
I have a banner script that is included in php on a website but the
actual script is hosted on a different site allowing me to only edit
one file to update the script.

I need to store in a variable the hostname that the included script is
being run on NOT the master url where it is hosted.

I'm assuming you're include()-ing the script as something like:

include("http://remote.server.example.com/script.php");

Is the remote server set to execute PHP or not?

* If it is, then script.php is being run on the remote server, not the
local server, so you're actually include()-ing the *output* of
script.php, not the PHP code itself. In which case, you will need to
either pass the hostname in the request for script.php, e.g.:

include("http://remote.server.example.com/script.php?host=http://local.example.com/mainscript.php");


This is something that I have never done (include from another
server/domain)...

Out of curiosity, does the $_GET array actually get populated with
values sent in this way for the remote script? When I want that
behavior, I usually use something like curl to send the request and
receive the output.

I know that if you do something like:
include 'myfile.php?var=val1';

That php looks for a file with that exact name (which will not exist in
most cases). Therefore in order to make that work (locally anyway),
you'd need to set the variable before the file was included.

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Jan 10 '06 #3
Justin Koivisto said the following on 10/01/2006 14:57:
Oli Filth wrote:
* If it is, then script.php is being run on the remote server, not the
local server, so you're actually include()-ing the *output* of
script.php, not the PHP code itself. In which case, you will need to
either pass the hostname in the request for script.php, e.g.:

include("http://remote.server.example.com/script.php?host=http://local.example.com/mainscript.php");
Out of curiosity, does the $_GET array actually get populated with
values sent in this way for the remote script?


Yes. The receiving remote server doesn't care (or know) where the HTTP
request for script.php came from, so it acts exactly as if someone had
requested that URL from their browser.

I know that if you do something like:
include 'myfile.php?var=val1';

That php looks for a file with that exact name (which will not exist in
most cases). Therefore in order to make that work (locally anyway),
you'd need to set the variable before the file was included.


Well yes, because "?var=var1" is meaningless in a local filesystem.
Setting the local variable works because both scripts are being run
locally in the same PHP instance.

If you do something like include("http://example.com/file.php?foo=bar"),
then PHP goes away and performs an HTTP request, where "?foo=bar" has
meaning, i.e. a GET sub-string.

--
Oli
Jan 10 '06 #4
Oli Filth wrote:
Justin Koivisto said the following on 10/01/2006 14:57:
Oli Filth wrote:
include("http://remote.server.example.com/script.php?host=http://local.example.com/mainscript.php");


Out of curiosity, does the $_GET array actually get populated with
values sent in this way for the remote script?


Yes. The receiving remote server doesn't care (or know) where the HTTP
request for script.php came from, so it acts exactly as if someone had
requested that URL from their browser.


I'll have to keep that in mind next time I consider using curl for a
simple get request...
I know that if you do something like:
include 'myfile.php?var=val1';

That php looks for a file with that exact name (which will not exist in
most cases). Therefore in order to make that work (locally anyway),
you'd need to set the variable before the file was included.


Well yes, because "?var=var1" is meaningless in a local filesystem.
Setting the local variable works because both scripts are being run
locally in the same PHP instance.


That's what I was trying to explain. When I first started php (many
years ago), I learned that while experimenting with different things.
One of these days I'll get my site together that I plan to use for
articles and such like that. ;)

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Jan 10 '06 #5
ok i'll explain more...

a script on www.master.com/script.php

contains a list of items:

1.com
2.com
3.com

the script.php file is included in a page:

www.2.com/index.php

as

include("http://www.master.com/script.php");

the script.php checks for the domain it is on (which should tell it its
on www.2.com/index.php) and then makes sure it WONT display its own
domain in the script.php list... eg it will show 1.com and 3.com but
not 2.com

Of course what happens is that the domain test comes back as saying its
on http://www.master.com

How else can I do this and not have to update 50 websites when I want
to make a common change to one file?

Jan 10 '06 #6
Venturer said the following on 10/01/2006 22:01:
ok i'll explain more...

a script on www.master.com/script.php

contains a list of items:

1.com
2.com
3.com

the script.php file is included in a page:

www.2.com/index.php

as

include("http://www.master.com/script.php");

the script.php checks for the domain it is on (which should tell it its
on www.2.com/index.php) and then makes sure it WONT display its own
domain in the script.php list... eg it will show 1.com and 3.com but
not 2.com

Of course what happens is that the domain test comes back as saying its
on http://www.master.com

How else can I do this and not have to update 50 websites when I want
to make a common change to one file?


Well, the easiest way is to pass a GET variable which contains the
"local" URL when include()-ing http://www.master.com/script.php, as I
suggested in response to your original post.

However, we might be able to help more if you explain *why* you want to
access a remote script in this way.
P.S. "www.master.com" is a domain, whereas "http://www.master.com" and
"http://www.master.com/script.php" are URLs.

--
Oli
Jan 10 '06 #7

Venturer wrote:
include("http://www.master.com/script.php");

the script.php checks for the domain it is on (which should tell it its
on www.2.com/index.php) and then makes sure it WONT display its own
domain in the script.php list... eg it will show 1.com and 3.com but
not 2.com

Of course what happens is that the domain test comes back as saying its
on http://www.master.com

How else can I do this and not have to update 50 websites when I want
to make a common change to one file?


Ah, you might be looking for the "Non-Standard File Extension" Try

include 'http://www.remotedomain.com/script.xyz';
this script should have open/close php tags.

You can use the $_SERVER array in script.xyz as if it were on the
includer domain. I use this all the time to put hit counter code, etc
in my sites.

Do be aware, anyone could browse to www.remotedomain.com/script.xyz and
see the raw php code, so you shouldnt use this for anything secret.

--
juglesh

Jan 11 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Sandman | last post: by
11 posts views Thread by juglesh | last post: by
11 posts views Thread by kk | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.