Forum Moderators: coopster

Message Too Old, No Replies

Alternatives to $_SERVER['REQUEST_URI']

Looking for "/" and also "/index.php"

         

fish_eye

1:47 am on Aug 8, 2005 (gmt 0)

10+ Year Member



Hi,

I'm writing a 'breadcrumb' generator and also a table of contents generator using an array containing the structure of my (sub-)site. Something like this:


$site_structure = array (
array ('index.php', 'Widgets', 0),
array ('010000_blue-widgets.php', 'Blue Widgets', 1),
array ('010100_intro.php', 'Introduction', 2),...

$site_structure[n][1] is the file name
$site_structure[n][2] is the name displayed in title, breadcrumb, table of contents etc
$site_structure[n][3] identifies the heirarchy of the page.

I'm testing


$site_structure[n][0] against basename($_SERVER['REQUEST_URI'])

Everything works perfectly, except when I come in on the 'root' of this subdir (eg. [emample.com...]

If I come in on (or navigate using the array) to [emample.com...] it works fine (of course).

Is there something (or some technique) where I can find a match on both "/" and also "/index.php"?

Many thanks, Sam.

fish_eye

1:54 am on Aug 8, 2005 (gmt 0)

10+ Year Member



Hmmm - I guess I could strip off the "index.php" using .htaccess or somesuch and test for an empty (or whatever) $_SERVER['REQUEST_URI'].

Any comments / warnings on that one?

mincklerstraat

8:34 am on Aug 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I were writing this script I'd first try just adding an entry for the '/' or '' possibility (check which it is which is coming in, and use that value -- or add both).

$site_structure = array (
array ('/', 'Widgets', 0), // <- line added
array ('index.php', 'Widgets', 0),
array ('010000_blue-widgets.php', 'Blue Widgets', 1),
array ('010100_intro.php', 'Introduction', 2),...

fish_eye

1:06 pm on Aug 8, 2005 (gmt 0)

10+ Year Member



Ah hah... another clue....

basename($_SERVER['REQUEST_URI'])

is returning

/home/example/public_html

on any string that ends with "/".

(But then you probably all knew that - guess I should've checked that one out long ago.)

fish_eye

1:08 pm on Aug 9, 2005 (gmt 0)

10+ Year Member



For anyone who's interested the solution to my problem was to leave index.php in the array and test

$site_structure[n][0] against basename($_SERVER['REDIRECT_URL'])

This not only behaves the same when encountering "/" and "index.php" ("/" is interpreted by $_SERVER['REDIRECT_URL'] as /index.php) it also works with a query string on the back (which $_SERVER['REQUEST_URI'] does not).

I guess this may be server setup specific but it works fine for me on a pretty standard virtual host.

To answer my own question $_SERVER['REDIRECT_URL'] is not an alternative $_SERVER['REQUEST_URI'], but in this case it's heaps better.