Forum Moderators: coopster
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
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.
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
will use <?php include("/home/content/html/head.php");?> to reference a file at http://example.com/head.php.
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');
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]
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:
http://example.com/head.php contains the string <?php echo $_SERVER["SERVER_NAME"];?> (double quotes for visual consistency -- or for pedantry)http://subdomain1.example.com/index.php calls a file at /home/content/html/subdomain1/index.phphttp://example.com/foo.php is actually at /home/content/html/foo.phptext/html files, no matter their location, reference http://example.com/head.php like this: <?php include("/home/content/html/head.php");?>Thank you for your help, guys.
L