Forum Moderators: coopster

Message Too Old, No Replies

Dynamically inserting part of a domain name

Removing http:// and anything after and including the trailing slash

         

Lucas

7:25 pm on Mar 1, 2008 (gmt 0)

10+ Year Member



My website has multiple subdomains. I want all the subdomains to reference the same file for part of their head:
http://example.com/index.php
,
http://subdomain1.example.com/index.php
, and
http://subdomain2.example.com/index.php
will use
<?php include("/home/content/html/head.php");?>
to reference a file at
http://example.com/head.php
.

http://example.com/head.php
will, in turn, include the code required for Google Analytics, part of which is:

<script type="text/javascript">
_uacct="UA-123456-7";
_udn="example.com"; // Or, depending... _udn="subdomain1.example.com";
urchinTracker();
</script>

It is the "

example.com
" part of the line that begins "
_udn
" that I want to dynamically change, automatically inserting the domain name of the file referencing this. Is PHP capable?

Thanks,

Lucas

PHP_Chimp

10:53 pm on Mar 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are a few ways that you could do this.

Assuming all sites are hosted on the same server you could include using a absolute path.

include '/var/vpopwww/domains/example.com/htdocs/head.php';

As this will give you the same location for all includes.

You could read the contents of that file as a string then echo that.


$urchin = [url=http://uk3.php.net/manual/en/function.file-get-contents.php]file_get_contents[/url]('http://example.com/head.php');
echo $urchin;

Or you could use cURL to do the same thing.

What you decide to do may well depend on what you are allowed to do on the server, as file_get_contents may well be limited as will the include. If I get a choice then I use the include with an absolute path, however cURL seems to be the most portable approach, but the most code to complete.

g1smd

10:59 pm on Mar 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can also set the PHP include paths in the .htaccess file.

Lucas

12:35 am on Mar 2, 2008 (gmt 0)

10+ Year Member



Thanks for getting back, I appreciate it.

PHP_Chimp: "Assuming all sites are hosted on the same server you could include using a absolute path"
Everything is on the same server, but I thought I was using an absolute path to include with
<?php include("/home/content/html/head.php");?>

PHP_Chimp: "You could read the contents of that file as a string then echo that"
If the file at
http://example.com/head.php
refers to
example.com
as the domain name, won't using
file_get_contents
from a subdomain result in a domain mismatch? I.e., even files on
subdomain1.example.com
would end up including
example.com
instead of
subdomain1.example.com
. I expected you to say I needed something like
<?php echo $_SERVER["PHP_INSERT_REFERRING_DOMAIN_NAME_EXCEPT_FOR_THE_HTTP://_PART"]?>
;)

PHP_Chimp: "cURL seems to be ... the most code to complete"
I do not know anything about cURL, so let's come back to that as a second choice only if we must.

g1smd: "You can also set the PHP include paths in the .htaccess file"
What would I add to the
.htaccess
file?

L

encyclo

2:49 am on Mar 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I understand your question correctly, can't you just use this?

<?php echo $_SERVER['HTTP_HOST']; ?>

or

<?php echo $_SERVER['SERVER_NAME']; ?>

PHP_Chimp

1:54 pm on Mar 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Durr...missed the fact that you are already using an absolute path in your include statement.

will use <?php include("/home/content/html/head.php");?> to reference a file at http://example.com/head.php.

I am assuming that you want to include the contents of http://example.com/head.php in all of your documents. So if you want that file then file_get_contents('http://example.com/head.php') will return that as a string. Assuming you want the same file across all of your subdomains then you want to use the example.com/head.php location of that file...so file_get_contents('http://subdomain.example.com/head.php'); will not exist, so there is no point trying to ask for it...or maybe I am not understanding what you are after (wouldn't be the first time ;)

file_get_contents needs fopen wrappers to be enabled for you to pass url's to it. So this may or may not be an option, however if fopen wrappers are enabled then you can pass any url to the function, your domain or not.

It is likely that using this type of file function has been disallowed if the domains are not the same. That is quite normal, as this can create a huge security hole, although is annoying if you are trying to use one file across many of your own domains.

You could try -


// .htaccess
# on Apache
php_value include_path '.:/home/content/html'
# on windows
php_value include_path '.;C:\home\content\html'
// [url=http://uk2.php.net/set_include_path]set_include_path[/url]
set_include_path('/home/content/html');
// [url=http://www.php.net/manual/en/function.ini-set.php]ini_set[/url]
ini_set('include_path', '/home/content/html');

There are notes on both the ini_set and set_include_path pages of the manual about using htaccess.

How are your subdomains organized? Are they set as completley different domains, or are you using files within your main site as subdomains?

As most people are hosted on a shared server there is generally some directory that holds all of the sites. So you get a structure that looks a bit like -

/var/sites/example.com/all_your_stuff
/var/sites/example1.com/all_there_stuff
/var/sites/example2.com/..

You look as though you have either a dedicated server, or dont actually have the root of the file system as /. This may well be how your server is set up so that you cant get access to other people sites. There may well have an alias set up so that your / is actually /var/sites/example.com/. If this is true then absolute uri's wont help, although a relative one may, if you can find out the actual structure of the file system.

It may just be easier to ask the host to put that include path into the paths for all of your subdomains.

<edit>
Have a look at
[php.net...]
as that should give you everything you need to know. There are lots of user notes that should sort out the problem :)

[edited by: PHP_Chimp at 2:05 pm (utc) on Mar. 2, 2008]

Lucas

10:20 pm on Mar 2, 2008 (gmt 0)

10+ Year Member



encyclo: can't you just use ...
<?php echo $_SERVER['SERVER_NAME'];?>
?

Yes! This is exactly what I want. Here are the details of the solution:

  1. http://example.com/head.php
    contains the string
    <?php echo $_SERVER["SERVER_NAME"];?>
    (double quotes for visual consistency -- or for pedantry)
  2. All files are on the same virtual server. Some files are in sub-directories, which also serve as subdomains. E.g.:
    • http://subdomain1.example.com/index.php
      calls a file at
      /home/content/html/subdomain1/index.php
    • http://example.com/foo.php
      is actually at
      /home/content/html/foo.php
  3. All
    text/html
    files, no matter their location, reference
    http://example.com/head.php
    like this:
    <?php include("/home/content/html/head.php");?>
  4. The code, when parsed and served, then includes only the same domain as that in the URI making the request. In other words, each referring file will now accurately include its own server name.

Thank you for your help, guys.

L