note 56633 deleted from reserved.variables by mazzanet
| From: | mazzanet@php.net | Date: | Sun, 25 Dec 2005 23:56:48 +0000 |
| Subject: | note 56633 deleted from reserved.variables by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-100710@lists.php.net to get a copy of this message | ||
Note Submitter: phpclases at mad4dweb dot com
----
This code will help get just the name of the script your using:
THE PROBLEM
--------------------------------------------------------
Using $PHP_SELF or $_SERVER[PHP_SELF]
FOR THE URL > http://yourdomain.com/location/of/myphpscript.php
will output location/of/myphpscript.php
FOR THE URL > http://yourdomain.com/location/of/myphpscript.php?lang=es&id=2
will output location/of/myphpscript.php?lang=es&id=2
But you only want to output myphpscript.php this cannot be done using $_SERVER[PHP_SELF]
THE SOLUTION
--------------------------------------------------------
Detect if its a WINDOWS path and convert it into a UNIX path
If its a UNIX path it won do nothing, the result will be myphpscript.php
I think also will help correcting problems with WINDOWS path's
as you will notice in order to make a replace of a single \ you have to use 4 \\\\
$scriptrealpath = ereg_replace("\\\\",'/', realpath("."));
$scriptlocpath = ereg_replace("\\\\\\\\",'/', $_SERVER[PATH_TRANSLATED]);
$LOAD_SCRIPT = ereg_replace("$scriptrealpath/", "",
"$scriptlocpath");
--------------------------------------------------------
IN WINDOWS the output will be
realpath(".") - C:\html\location\of
$_SERVER[PATH_TRANSLATED] - C:\\html\\location\\of\\myphpscript.php
$scriptrealpath - C:/htmllocation/of
$scriptlocpath - C:/html/location/of/myphpscript.php
$LOAD_SCRIPT - myphpscript.php
--------------------------------------------------------
In UNIX the output will be
realpath(".") - /var/www/location/of
$_SERVER[PATH_TRANSLATED] - /var/www/location/of
$scriptrealpath - /var/www/location/of
$scriptlocpath - /var/www/location/of/myphpscript.php
$LOAD_SCRIPT - myphpscript.php