Jump to content

Require/Include Files


TheFilmGod

Recommended Posts

I would like to require/include files from a subdirectory called "element" that is in my root directory.

 

This is difficult because this is my custom 404 error page. Someone can try opening up any files in any made up directory and the server "thinks" its in that directory.

 

For instance, if a user goes to mydomain.com/hello/yay/doesitwork.php, I need to include my files as "../../element"

 

This code SHOULD work, but it only works for pages called at one directory up i.e. mydomain.com/directory/somefile.php

 

<?php
// Grab Address, i.e "/hello/yay/doesitwork.php"
$address = $_SERVER['REQUEST_URI'];
// Count how many backslashes exists, i.e 3
$count = substr_count("$address", "/");
// Subtract 1 / i.e. 3-1 = 2
$count--;
// Create a repeated text string for more than 1 / i.e. ../../
$bslash = str_repeat("../", $count);
// Define Unique Page Elements, this is the main thing!
$element = "$bslash" . "element/";
// Require the Session Script
require ($element.'session.php');
?>

 

Here is the code without as many comments.

 

<?php
// Grab Adress, count bslash, create address to root
$address = $_SERVER['REQUEST_URI'];
$count = substr_count("$address", "/");
$count--;
$bslash = str_repeat("../", $count);
// Define Unique Page Elements
$element = "$bslash" . "element/";
$root = "$bslash";
// Require the Session Script
require ($element.'session.php');
?>

Link to comment
https://forums.phpfreaks.com/topic/111342-requireinclude-files/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.