php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #20902 Function needed to return name of current script independent of O/S
Submitted: 2002-12-09 06:12 UTC Modified: 2003-02-06 21:44 UTC
From: judd at ob-wan dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.2.3 OS: Win2K Server
Private report: No CVE-ID: None
 [2002-12-09 06:12 UTC] judd at ob-wan dot com
Finding the name of the currently running script (to save it, for instance, before branching) requires too much messing around to make it operating system independent. This sort of complexity should be hidden from end-user programmers.

An example of the hoops needed to be jumped through is shown below:

function me() {
/* returns the name of the current script, without the querystring portion.
 * this function is necessary because PHP_SELF and REQUEST_URI and PATH_INFO
 * return different things depending on a lot of things like your OS, Web
 * server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.) */

	if (getenv("REQUEST_URI")) {
		$me = getenv("REQUEST_URI");

	} elseif (getenv("PATH_INFO")) {
		$me = getenv("PATH_INFO");

	} elseif (ini_get('register_globals') && $GLOBALS["PHP_SELF"]) {
		$me = $GLOBALS["PHP_SELF"];

// Supply default case that works when register_globals = Off - RJ 
	} else $me = $_SERVER['SCRIPT_NAME'];

	return strip_querystring($me);
}

I think this can be improved. PHP_SELF is quoted as the standard solution, but I think you'll find those pushing it are relying on a particular O/S or server.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-06 21:44 UTC] iliaa@php.net
Use the __FILE__ constant.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 20 23:00:02 2025 UTC