note 87637 added to function.dirname
| From: | joe dot naylor at gmail dot com | Date: | Sat, 13 Dec 2008 18:07:02 +0000 |
| Subject: | note 87637 added to function.dirname | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148128@lists.php.net to get a copy of this message | ||
The dirname function does not usually return a slash on the end, which might encourage you to create
links using code like this:
$url = dirname($_SERVER['PHP_SELF']) . '/somepage.php';
However dirname returns a slash if the path you specify is the root, so $url in that case would
become '//somepage.php'. If you put that URL as the action on a form, for example,
submitting the form will try to go to http://somepage.php.
I ran into this when I wrote a site on a url with a path, www.somehost.com/client/somepage.php,
where the code above works great, but then wanted to put it on a subdomain,
client.somehost.com/somepage.php, where things started breaking.
The best solution would be to create a function that generates absolute URLs and use that throughout
the site, but creating a safe_dirname function (and an htaccess rewrite to fix double-slashes just
in case) fixed the issue for me:
<?php
function safe_dirname($path)
{
$dirname = dirname($path);
return $dirname == '/' ? '' : $dirname;
}
?>
----
Server IP: 209.41.74.194
Probable Submitter: 98.202.131.150
----
Manual Page -- http://www.php.net/manual/en/function.dirname.php
Edit -- https://master.php.net/note/edit/87637
Del: integrated -- https://master.php.net/note/delete/87637/integrated
Del: useless -- https://master.php.net/note/delete/87637/useless
Del: bad code -- https://master.php.net/note/delete/87637/bad+code
Del: spam -- https://master.php.net/note/delete/87637/spam
Del: non-english -- https://master.php.net/note/delete/87637/non-english
Del: in docs -- https://master.php.net/note/delete/87637/in+docs
Del: other reasons-- https://master.php.net/note/delete/87637
Reject -- https://master.php.net/note/reject/87637
Search -- https://master.php.net/manage/user-notes.php