[thelist] Hello, I'm new here...php question

Richard Harb rharb at earthling.net
Sat Mar 27 22:37:22 CST 2004


Friday, March 26, 2004, 9:26:54 PM, you wrote:

> Good day all,
>     My name is Miles Havok, I run a web hosting company in a
> state-of-the-art neutral datacenter in Waltham, Massachusetts. I am in the
> process of totally re-designing the company website, but I have little-to-no
> PHP experence.

> I simply want to be able to have a single index.php file and have it call
> pages according to what index.php?page=variable is

> Example: http://www.mysite.com/index.php?page=about - will show the page
> containing our company infomation and history.

> If anyone can point me to an online tutorial, or explain how to do this it
> would be much appreciated.

As you seem to be using an Apache webserver you could easily do it by
looking at the $_SERVER['PATH_INFO'] variable.
(this has previously been posted on this list - I adopted it myself -
works nicely :) - thanks to whoever the initial poster was.

That variable will only be set if a page is requested in the style of:
http://address/mypage.php/somefile/whatever
and will contain the "/somefile/whatever" part ...

A quick way to do this would be:


if (!empty($_SERVER['PATH_INFO'])) {

  $file = 'path/to/pages/' . $_SERVER['PATH_INFO'];
  if (is_readable($file)) {
     include ($file);
  } else {
    do_something_else();
  }

} else {
  do_something_else();
}


---
I like that solution because it looks rather clean - especially when
the apache negotiates the content and the file extension (.php) can be
left out: for the unwary (=99%) observer it looks like the content is
being served from within a directory. Nice for having consistent
paths.

If you have all your pages (about, contact, whatever) in a single
hierarchy I'd recommend a slightly more thorough check so there can't
be just any file be passed along:

  $temp = explode('/', substr($_SERVER['PATH_INFO'], 1));
  $file = 'path/to/pages/' . $temp[0];

so only the first "name" or "path" will be passed along - no nesting
to higher directory levels etc. Whatever you do - I'd recommend
doing some checks on the input (it can be easily modified by any user)

---
If you want to do it your way - no problem:

if (!empty($_REQUEST['page'])) {

  $file = 'path/to/pages/' . $_REQUEST['page'];
...

---

note: do_something_else(); is just a placeholder some output you might
give if no argument was passed or the page isn't there/can't be read.
I'd substitute it with an
include('no_page.inc.php');
or some such.
---

this isn't perfect, but it might be a start ...

Richard

--
REALITY.SYS corrupted. Reboot universe [Y/n]?



More information about the thelist mailing list