View Full Version : index.php?page=aboutme
daynah
07-01-2002, 09:04 PM
Some people wonder how they can make their pages look like: http://mysite.com/index.php?page=aboutme so here's a tutorial I wrote on it. :)
<?PHP
// Sample script written by Daynah
// PHP-Princess.net
// Where all your text files are located at
$directory = 'version2/';
// If the variable exists
// Example: If the url is index.php?page=aboutme
if($page)
{
// Does the file $directory/$page.txt exist?
if(is_file("$directory$page.txt"))
include("$directory$page.txt");
else
print "Sorry, this page does not exist";
}
// If the page is just called as index.php
// Then just include the default main.txt page
else
{
$mypage = 'main.txt';
include("$directory$mypage");
}
?>
I also attached a zip file that has all the files in it's proper directory. Unzip it, put it on your domain. Then go to the index.php site. Ex. http://yourdomain.php/phpsample1/index.php
You can go and edit http://yourdomain.php/phpsample1/version2/main.txt or http://yourdomain.php/phpsample1/version2/aboutme.txt if you like.
If you want to add more files so you can call the page like: http://yourdomain.php/phpsample1/index.php?page=downloads
Just add another file called downloads.txt to the version2 directory. :) Test it out and see if it works. If you need more help, please post here. :)
You can download the files here (http://board.php-princess.net/viewthread.php?action=attachment&tid=7&pid=11) . :)
This code is insecure.
Consider the URL http://www.yourdomain.com/index.php?page=../../secretfiles/big_secret -- that is, an access with the "page" value of "../../secretfiles/big_secret". With the given implementation, the user can access any file (that ends in .txt and which the apache process has read permissions on, granted) on your system (the .txt restriction is fairly significant, but a site author who has both, say, .html and .txt files would be likely to just chop that bit of code off so the snippet worked for them).
Basically, you should never let the user pass a text string which is directly treated as a file (or passed to MySQL, or, god forbid, executed).
The best way to implement this functionality would be to define an array (or, better yet, create a MySQL table (escape the page value first!)) which associates strings like "about", "contact", "hotpix", "main", etc, with actual URLs. Since your script will only ever give the user URLs from the array/table, they can't load anything you don't explicitly allow.
If you have a prohibitively large number of files to do this manually, write a script which will go through your directories and generate the array code or the MySQL table.
If, for some reason, you absolutely must not use an array or table, at least secure the string the user passes: remove all references to "..", leading /'s or ~'s, etc. It's not nearly as secure or clean as a lookup table, but much better than nothing.
iDxMan
09-08-2002, 02:56 PM
... at the least it should use basename() on the string.
http://www.php.net/manual/en/function.basename.php
Originally posted by iDxMan
... at the least it should use basename() on the string.
http://www.php.net/manual/en/function.basename.php
This will prohibit you from doing "downloads/downloads.php", but is a good fix for simple implementations of this functionality.
Grizzly
09-17-2002, 04:47 PM
This approach is trying to be what FuseBox (http://www.fusebox.org/) has always been. I'd highly suggest reading up on it and trying to apply the methodology. I've been using it for well over a year and I'll never program another web app without it.
Also see BombusBee (http://bombusbee.com/), this site is more dedicated to the PHP implementation of Fusebox.
skidooer
11-14-2002, 01:15 PM
This method also also great for making PHP modules. Put all the layout code in the calling PHP script, and the PHP in the included file will also be executed. You should ensure that the input is sane before including the file though.
B1nary
03-31-2004, 08:34 PM
I made a code similar to that, but it is probably easier to use(once I add comments to the tags to edit). Anyway, I'll post it.
(put this code where you want your content)
<?php
$abspath = ".";
// Change this to the directory that holds all of you content files. "." is the default.
$extension = "txt";
// Change this to the extentions of your content file(leave out the ".").
$defaultfile = "news.txt";
// This is the file that shows up with adding the include line(index.php?id=page). It would just be index.php.
$errorfile = "404.html";
// This is the file for the broken links(example: index.php?id=mispelled) This will make it redirect to the error file.
$query = "id";
// Change this to your query(index.php?"id"=page).
// Don't change anything below.
clearstatcache();
$includestring = "";
$mainpage = urldecode($$query);
$mainstring = $abspath."/".$mainpage.".".$extension;
if (!$mainpage) {
$includestring = $abspath."/".$defaultfile;
} elseif (ereg("\.\.", $mainpage) || substr($mainpage,0,2) == "./" || substr($mainpage,0,3) == "../") {
die("Screw off.");
} else {
if (file_exists($mainstring) && is_file($mainstring)) {
$includestring = $mainstring;
} else {
$includestring = $abspath."/".$errorfile;
}
}
@include($includestring);
?>
If there's an error with this code, tell me and I'll edit it.
dboorn
06-28-2005, 04:16 PM
Dynamic site are great and there are many ways of implementing them. Here is a way of creating a dynamic index page that is templated.
<?
//include templated header
include('template/template1/header.php');
//Load index content
$case = $_REQUEST['go']; // get post or get varable for index driver
//please note that chdir doesn't change current root directory so
switch($case) {
case "about" : // fetch about
include('about.php');
break;
case "contact" : // fetch contact
include('contact.php');
break;
case "services" : // fetch services
include('services');
break;
default: // default and errors fetch home page
include('index_homepage');
break;
}// end of index content
// fetch templated footer
include('template/template1/footer.php');
?>
This is just one example of any esay aspect orented design that allows for easy templated dynamic content. You could even hack this to work with javascript displaying different templates based on the time of day. Hope this helps, and best of luck.
dboorn
06-28-2005, 04:17 PM
Dynamic site are great and there are many ways of implementing them. Here is a way of creating a dynamic index page that is templated.
<?
//include templated header
include('template/template1/header.php');
//Load index content
$case = $_REQUEST['go']; // get post or get varable for index driver
// use simple switch statment for index driver
switch($case) {
case "about" : // fetch about
include('about.php');
break;
case "contact" : // fetch contact
include('contact.php');
break;
case "services" : // fetch services
include('services');
break;
default: // default and errors fetch home page
include('index_homepage');
break;
}// end of index content
// fetch templated footer
include('template/template1/footer.php');
?>
This is just one example of any esay aspect orented design that allows for easy templated dynamic content. You could even hack this to work with javascript displaying different templates based on the time of day. Hope this helps, and best of luck.
dboorn
06-28-2005, 04:40 PM
Creating a dynamic index page is a great step in the right direction for clean and good web design. But you must always remember to optamize with clean urls.
What are clean urls also known as SEO URLS?
typical dynamic urls - http://dboorn.com/estate/demo/index.php?go=search
clean / seo urls - http://dboorn.com/estate/demo/index/search
Why is this important?
Dynamic scripts can stop search bots from crawling your site. They are afraid of endless loops.
What stop a typical bot such as google?
usally google will not crawl a url with a ? & or even a dnyamic extention such a .php .php3 .cfm etc...
So what are my options for clean / seo urls?
There is many ways of making clean urls, pending you what server you use and how much access you have to it.
I.E. - any one with access to .htaccess file
using forse type
<Files index>
ForceType application/x-httpd-php
</Files>
place this in your .htaccess file - what this does is tells the server such as apache to take the file named "index" (notice no . extention) and compile it as php.
This option is the easyest for sites that map out to -
contact.php
index.php
would be changed to
contact
index
and the .htaccess would be
<Files index>
ForceType application/x-httpd-php
</Files>
<Files contact>
ForceType application/x-httpd-php
</Files>
now you have http://yoursite.com/contact or http://yoursite.com/index
Now that is only the first step. BUT remember that in a dynamic index page we have varables. So we have to take this one more step.
1st go back to your .htaccess file and only place a forse type for the index page and rename all other files back with .php extention.
in our index page we will add a url varable driver
what this will do is take the url string and expload it at the "/" char
say we have the following url - yoursite.com?go=here&where=there
rewriten to clean url - yoursite.com/here/there
as you can see we only leave the varable contant and separate them by a "/" char.
in the index driver we want to expoad the url on the "/" char using the expload function with the current path
below is working code taken from another tutorial
/* Define our global variables */
global $REQUEST_URI;
global $SCRIPT_NAME;
/* Assign the value of $SCRIPT_NAME to $base_href.
This value is used at the beginning of all links in the site
so that absolute hrefs are created. Relative hrefs will not
work with this method. */
$base_href = $SCRIPT_NAME;
/* Create an array ($path) out of $base_href
so that we can get the name of the current template. */
$path = explode("/",$base_href);
/* Pop the template name at the end of the array and assign it to $template. */
$template = array_pop ($path);
/* Convert $path back to a string to use later in the template.
This variable is used to reference CSS and JavaScript
source files since $base_href includes the template name. */
$path = implode ("/",$path);
/* Extract the values from the end of the URI.
Note that variable names are not included
in the URI, just the values.
It its therefore important that you always
use the same order for your variables
when creating links. The order itself is not
important, so long as it's always the same. */
$vars = str_replace($SCRIPT_NAME, "", $REQUEST_URI);
/* create an array from the string $vars, then
loop over the array, extract each
value, and assign each to a temporary variable */
$array = explode("/",$vars);
$num = count($array); // How many items in the array?
for ($i = 1 ; $i < $num ; $i++) {
$url_array["arg".$i] = $array[$i];
}
now you have a nice array with all you url varables in them.
$url_array['arg1'] = first varable
so if we had an index driver with a switch statment such as in the prevous post we would know that the first varable is always going to tell us what page to load.
// get driver varable
$case = $url_array['arg1'];
switch($case) {
case "aboutus" :
include('about.php');
break;
case "example" :
include('example.php');
break;
default: //default and errors
include('index_home.php');
}
For more examples google php and search engines. Hope this helps. Best of luck!
sirbeavis
04-20-2007, 02:05 PM
I dont understand why CSO on PHP is so important anyway, wont google pick up the URL even if it is dynamic?
Viper007Bond
04-21-2007, 08:29 AM
I dont understand why CSO on PHP is so important anyway, wont google pick up the URL even if it is dynamic?
Yes. In the old days, your URL mattered a lot for page ranking (as did <meta> tags and such). This is no longer the case (due to evolution as well as abuse).
However, I believe Google will only crawl so many URLs with a ? in the URL on a site in order to avoid getting stuck in an infinite loop.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.