Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 11th, 2008, 03:02 PM
Authorized User
 
Join Date: Dec 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default including files above site root

What I would like to do is href files that are above root. The files I'm attempting to include are .php and .css file. They are all related to the style and layout (theme) of a site.

The sites are located on a shared host.

Hosting root
|
---Templates
| |
| ------ Template1
| |
| ------ Template2
|
------ Site 1 Dir (site root)
|
------ Site 2 Dir (site root)
|
------ Site 3 Dir (site root)


The href source works fine when the paths are relative if the domain is the Hosting root domain

address bar: http://www.hostingroot.com/Site1/

<link href='../Templates/Template1/Template1.css' rel='stylesheet' type='text/css'> WORKS

It stops working if the domain for Site1 is entered in the address bar

address bar: http://www.Site1.com

<link href='../Templates/Template1/Template1.css' rel='stylesheet' type='text/css'> DOSE NOT WORK

If you require further explanation of what I am trying to do please reply. As I have been banging my head against a wall for sometime. I am not sure if what I am trying to do is possible.

Thanks for your efforts
 
Old August 22nd, 2008, 02:55 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

I do this a lot on my own sites to separate 3rd-party applications and various other things from my core code base that I keep in subversion.

The way you've tried to do this via the ../ syntax doesn't work because of security restrictions. You don't want people to be able to access any arbitrary file on your web server. You're limited to DocumentRoot for your own protection, otherwise script kiddies would be able to get things like usernames and passwords that you keep in plain text config files, or worse, customer data from your databases. Some years ago I recall a vulnerability that allowed that to work, which led to some pretty nasty exploits.

That said, it can be done. You just have to deploy a little server-side magic.

You haven't mentioned what web server you're using. If you're using Apache, you can use a nifty module called mod_rewrite.

First you need to identify what you want to redirect to a folder outside of DocumentRoot. Let's say you have a folder called /Applications that lives parallel to DocumentRoot, as a sibling folder.

Set-up a rule in httpd.conf, or in a .htaccess file that looks like this:
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # If the REQUEST_FILENAME does not exist as a file or directory
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) /index.php [L]
</IfModule>
I use this exact configuration in a .htaccess file. It redirects every request for a file or directory that doesn't exist in the file system to my index.php script. In the index.php script I can use PHP to parse the path and output file contents. I use this for every file on my website, which lets me have some files that are database-stored, and some that are actually in the server's file system... essentially I create my own file system. That makes it easier to control what goes into subversion, it makes my paths SEO friendly. On the outside my website appears to be completely static.

Of course, you don't have to make things so complicated, you can also use this script to reroute certain directories, so you can store them outside of your DocumentRoot. At the same time though, you'll also have to make your own 404 error handling.. since the mod_rewrite configuration redirects everything that doesn't exist.

So, I recommend exploring that. You don't have to call your script index.php, by the way, you can make it whatever you like, just update the path in mod_rewrite accordingly.

In your script, you want to parse the path with parse_url(), be sure to escape quotes though, cause that will happily leave you vulnerable to SQL injection vulnerabilities. Then, all you have to do is use PHP's file and directory handling functions to grab and output the right files. You'll have to set the Content-Type HTTP header manually...

So, if you have the path... http://www.example.com/Applications/something.js

You do this in your script...

Code:
<?php

$uri = parse_url($_SERVER['REQUEST_URI']);

$mime = '';
$php = false;

switch (true)
{
  case strstr($uri['path'], '.css'):
  {
    $mime = 'text/css';
    break;
  } 
  case strstr($uri['path'], '.js'):
  {
    $mime = 'application/javascript';
    break;
  }
  case strstr($uri['path'], '.html'):
  case strstr($uri['path'], '.htm'):
  {
    $mime = 'text/html';
    break;
  }
  case strstr($uri['path'], '.jpg'):
  {
    $mime = 'image/jpeg';
    break;
  }
  case strstr($uri['path'], '.png'):
  {
    $mime = 'image/png';
    break;
  }
  case strstr($uri['path'], '.gif'):
  {
    $mime = 'image/gif';
    break;
  }
  case strstr($uri['path'], '.php'):
  {
    $php = true;
    break;
  }
  default:
  {
    echo "Unsupported file type.";
  }
}

$absolutePath = '/absolute/path/to/file'.$uri['path'];

if (file_exists($absolutePath))
{
  // If /absolute/path/to/file/Applications/something.js exists
  if (!$php)
  {
    header('Content-Type: '.$mime);
    echo file_get_contents($absolutePath);
  }
  else
  {
    include $absolutePath;
  }
}
else
{
  // Write your custom 404 script here... 
}

?>
Or something along those lines... I've even used this to compress my css and javascript by stripping out excess whitespace, comments, and line breaks.

Hope this helps!

Richard


--
http://www.deadmarshes.com
Author, Various Wrox Books
 
Old August 29th, 2008, 03:11 PM
Authorized User
 
Join Date: Dec 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for you reply Richard,

I am new to .htaccess files so I am not sure where to start.

I have read some on htaccess over the week ( I didn't want to bugg you for a complete walk through), But I can't seem to get the htaccess files to do anything.

Could you give me a more detailed description. The server is an apache server.

Thanks again






Similar Threads
Thread Thread Starter Forum Replies Last Post
correct way of including files. madhukp Classic ASP Basics 3 January 20th, 2005 09:39 AM
Site root and document relative paths. nicnacs Dreamweaver (all versions) 5 October 5th, 2004 05:35 PM
Including files across folders dsunmedia Beginning PHP 3 July 26th, 2004 04:17 PM
acessing files outside document root wakhan Apache Tomcat 0 June 2nd, 2004 12:31 PM
Including same files in different directories jeremy108 BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 August 8th, 2003 01:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.