• A reminder that Forum Moderator applications are currently still open! If you're interested in joining an active team of moderators for one of the biggest Pokémon forums on the internet, click here for info.
  • Due to the recent changes with Twitter's API, it is no longer possible for Bulbagarden forum users to login via their Twitter account. If you signed up to Bulbagarden via Twitter and do not have another way to login, please contact us here with your Twitter username so that we can get you sorted.

A fully documented "Include Directory" script

Status
Not open for further replies.

Koroku

Ninja Master!
Joined
Apr 27, 2005
Messages
1,137
Reaction score
4
I needed this snippet, so I went digging at PC for it. I found Frosty's code and hacked it up and made it a bit more... useful. :)

Then I was bored and decided to document it to a sickening level.

What's the use? If you have like dex.php and want to have ?id=001 but don't want the entire directory to be 001.txt and 002.txt, then you can add it to a "dex" folder and then use this code.

Yeah.
PHP:
<?
/* 
######################################################################################
## Script Title: Include a Directory v0.1
## Author: JKaizer
## URL: http://www.jkaizer.net
## Usage: Have index.php?id=page with "page" being in a seperate directory
## Date: August 28, 2005
## Thanks: Jedi_Amara & Frosty
## Note: All sections that have a / * * / (no spaces) set surrounding them can be removed once you've got the code under control. They are there for expanation.
######################################################################################
*/

//
// Main variables - this is the only stuff you'll need to change
//

$folder = "folder/";
$ext = ".htm";
$main = "news.htm";
$error = "404.htm";

/*
Variable Expanation:
$folder = Where all the files are stored. For example, if I wanted jkaizer.net/?id=pagetwo to include jkaizer.net/site/v3/content/pagetwo.txt, I'd have $folder be "site/v3/content/" Note: you need the trailing slash.
$ext = What's the extention of the files?  All the included files must have the same extention.
$main = The main page, where they go if it's normal index.php
$error = The 404 page, what happens when they go to index.php?id=pagedoesnotexist
*/

/*
Optional Modification
If you're using CuteNews or a similar script that uses the $id variable already, you can fix it by editting this code slighty. Find the two instances of $_GET['id'] and change it to $_GET['somethingelse'] where something else is the new variable.  Your pages would then be index.php?somethingelse=pagename
*/

//
// Do not edit below this line unless you know what you're doing!
//

$page = "$folder" . $_GET['id'];
$newpage = $page . $ext;
if(!isset($_GET['id'])){
include $main;
} 
elseif(!file_exists($newpage)) {
include $error;
}
else {
include $newpage;
}

/*
Code Expanation:
$page = "$folder" . $_GET['id'];
-- Includes the files in $folder
$newpage = $page . $ext;
-- Adds the extention onto the files
if(!isset($_GET['id'])){
-- If $id isn't set...
include $main;
-- Include the main page
} 
-- This section is done
elseif(!file_exists($newpage)) {
-- If the file called doesn't exist...
include $error;
-- Include the error page
}
-- This part is done
else {
-- Otherwise, it must exist and the variable is set so...
include $newpage;
-- Include the page
}
-- And we're done
*/
?>
 
Status
Not open for further replies.
Back
Top Bottom