Forum Moderators: phranque

Message Too Old, No Replies

using php include(), along with URL rewriting

an apache/php question

         

httpwebwitch

6:38 pm on Nov 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll describe my idea, and maybe someone can suggest if it's possible.

I have many websites on my server, each in a directory like
/site1/
/site2/
/site3/

inside each are lots of pages that use common scripts. these are reused classes that I employ on every site. hence I have many copies of them on my server.

/site1/library/tool1.php
/site1/library/tool2.php
/site1/library/tool3.php

/site2/library/tool1.php
/site2/library/tool2.php
/site2/library/tool3.php

/site3/library/tool1.php
/site3/library/tool2.php
/site3/library/tool3.php

typically in my PHP script, I include() them as needed, like so:

<?php include("library/tool1.php");

The obvious problem is that I'm maintaining 70+ copies of each script, one copied to each site. When I make a change to tool1.php, I have to copy that script all over the place. Human error creeps in

What I'd rather have is one /_commonscripts/ folder on my server root (e.g. sibling to "public_html" etc), which all of the sites can share:

/_commonscripts/library/tool1.php
/_commonscripts/library/tool2.php
/_commonscripts/library/tool3.php

then on my page, could I include it from a place beyond the boundaries of the current site?

<?php include("/root/myserver/_commonscripts/library/tool3.php
")?>

but alas, the include() function doesn't dig deeper than the site root. The code above looks for this:

/site1/root/myserver/_commonscripts/library/tool3.php
which is actually
/root/myserver/site1/root/myserver/_commonscripts/library/tool3.php
... and that doesn't exist.

This is where the Apache part comes in
So I wondered if I could use Apache .htaccess rewriting to point local site URLs to a different one? then instead of including from the local site, it would include the file from somewhere else. brilliant!

<?php include("/_commonscripts/library/tool3.php
")?>


RewriteRule ^_commonscripts/library/(.*)$ http://www.example.com/_commonscripts/library/$1

well that didn't work.

surely there's a simple strategy for this situation

httpwebwitch

7:02 pm on Nov 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



similar problem described here

[webmasterworld.com...]

jdMorgan

8:47 pm on Nov 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The reason I'm recommending symbolic links --which should work, BTW-- is that .htaccess only affects HTTP accesses -- That is, requests coming in from the Web. It does not affect internal server reads or writes, FTP transactions, or anything else. So a PHP include, being an internal access, won't be affected by anything in .htaccess.

You might want to ask over in the Linux, Unix, and *nix forum if the symlinks don't work for you.

Jim

httpwebwitch

3:21 pm on Nov 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how do I make a symbolic link like that?
I put an Alias rule in my .htaccess but it made my server very upset

jdMorgan

3:50 pm on Nov 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Login to your server using SSH, and use the *nix "ln" or "symlink" commands as desired. Seem the "man pages" for these commands for more information before trying to use them.

Jim

coopster

10:29 pm on Nov 10, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Unless I am missing something here, a much simpler approach here for PHP is to set the include path in the php.ini configuration to your "commonscripts" directory. Just that simple.

httpwebwitch

1:45 pm on Nov 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



coopster, it worked! thanks for the tip.

that's a php ini setting I'd never messed around with before. very useful!