note 63388 rejected from function.dirname by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 19:05:04 +0000 |
| Subject: | note 63388 rejected from function.dirname by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142264@lists.php.net to get a copy of this message | ||
Note Submitter: jo at durchholz dot org
----
OK, I'll bite :-)
Things that could be improved are:
1. PHP_SELF isn't always reliable. (Particularly various (mis-)configured FastCgi
installations.) REQUEST_URI is, and it's reasonably easy to parse. Even better, REQUEST_URI
also contains the server name.
2. When concatenating, you don't need that initial empty string.
3. dirname() has a borderline case (at least in 4.4.4): if the URL ends with a slash, the path part
preceding it is stripped, too.
I.e. if the script is called as http://example.com/path/to/script/index.php,
dirname() will do the Right Thing, but http://example.com/path/to/script/ will come out as http://example.com/path/to. (*Very* bewildering if the
customer phones you about that...)
Luckily, that's easy to fix by adding a letter (actually anything but a slash) to the URL.
Giving:
<?php
list ($url, $query_string)
= explode ('?', $_SERVER ['REQUEST_URI'], 2);
$urldir = dirname ($url . 'x');
?>