- How can I get the current script file name, but without the extension «.php»?
- 18 Answers 18
- How To Identify The Requested Page In PHP
- 5 Answers 5
- PHP Fetch current page name without extension?
- 7 Answers 7
- How to get URL of current page in PHP [duplicate]
- 5 Answers 5
- How to Get the Current Page URL of a Web Page Using PHP
- Full URL of Current Page
- Actual PHP Output of this Page
- Directory Pathway and File name
- Actual PHP Output of this Page
- Just the Directory Pathway
- Actual PHP of this Page
- Just the File Name
- Actual PHP Output of this Page
- Just the File Name Without the File Extension
How can I get the current script file name, but without the extension «.php»?
If I have PHP script, how can I get the filename of the currently executed file without its extension? Given the name of a script of the form «jquery.js.php», how can I extract just the «jquery.js» part?
18 Answers 18
Just use the PHP magic constant __FILE__ to get the current filename.
But it seems you want the part without .php . So.
A more generic file extension remover would look like this.
function chopExtension($filename) < return pathinfo($filename, PATHINFO_FILENAME); >var_dump(chopExtension('bob.php')); // string(3) "bob" var_dump(chopExtension('bob.i.have.dots.zip')); // string(15) "bob.i.have.dots"
Using standard string library functions is much quicker, as you’d expect.
function chopExtension($filename)
@ThiefMaster Because there is something built into PHP to handle file extensions. The wheel exists, and rolls well.
While __FILE__ gives you the .php file that line is in, you actually want $_SERVER[‘SCRIPT_NAME’] for the currently running top-level script (that which was invoked by the web server or directly on the command line)
When you want your include to know what file it is in (ie. what script name was actually requested), use:
basename($_SERVER["SCRIPT_FILENAME"], '.php')
Because when you are writing to a file you usually know its name.
Edit: As noted by Alec Teal, if you use symlinks it will show the symlink name instead.
This is wrong, it wont get the actual PHP file, but the file the webserver resolved to the request. Different if you use simlinks.
pathinfo(__FILE__, PATHINFO_FILENAME);
Here is the difference between basename(__FILE__, «.php») and basename($_SERVER[‘REQUEST_URI’], «.php») .
basename(__FILE__, «.php») shows the name of the file where this code is included — It means that if you include this code in header.php and current page is index.php, it will return header not index.
basename($_SERVER[«REQUEST_URI»], «.php») — If you use include this code in header.php and current page is index.php, it will return index not header.
which is safer SCRIPT_FILENAME or REQUEST_URI ? I know they both are server vars but isn’t REQUEST_URI a user tampered value? it enables a «URI injection» threat
both have their own impotence, But you can safe your url using different filers, like mysql_real_escape_string, stripslashes etc..
@KhandadNiazi basename($_SERVER[«REQUEST_URI»], «.php»); will return the folder’s name if the link is of the form http://example.com/somefolder . While basename($_SERVER[‘PHP_SELF’], «.php»); will always return the script’s name, in this case index .
it will work even if you are using include.
I have 2 files, header.php and home.php, the header.php is called in home.php how can i detect in header that the current page is home.php or contact.php so i could change some banner etc.
Here is a list what I’ve found recently searching an answer:
//self name with file extension echo basename(__FILE__) . '
'; //self name without file extension echo basename(__FILE__, '.php') . '
'; //self full url with file extension echo __FILE__ . '
'; //parent file parent folder name echo basename($_SERVER["REQUEST_URI"]) . '
'; //parent file parent folder name with //s echo $_SERVER["REQUEST_URI"] . '
'; // parent file name without file extension echo basename($_SERVER['PHP_SELF'], ".php") . '
'; // parent file name with file extension echo basename($_SERVER['PHP_SELF']) . '
'; // parent file relative url with file etension echo $_SERVER['PHP_SELF'] . '
'; // parent file name without file extension echo basename($_SERVER["SCRIPT_FILENAME"], '.php') . '
'; // parent file name with file extension echo basename($_SERVER["SCRIPT_FILENAME"]) . '
'; // parent file full url with file extension echo $_SERVER["SCRIPT_FILENAME"] . '
'; //self name without file extension echo pathinfo(__FILE__, PATHINFO_FILENAME) . '
'; //self file extension echo pathinfo(__FILE__, PATHINFO_EXTENSION) . '
'; // parent file name with file extension echo basename($_SERVER['SCRIPT_NAME']);
How To Identify The Requested Page In PHP
Is there any easy way to identify the file initially handling the request, ignoring get arguments and handling (at least basic) mappings like / to /index.php ? Ideally what I’m looking for is something like $_SERVER[‘REQUEST_URI’] , except it returns the same value regardless of the get arguments and that value is the file requested, not the URI, nor the currently executing file ( $_SERVER[‘PHP_SELF’] ). In other words, a $_SERVER[‘REQUESTED_FILE’] or something. I haven’t seen anything like that. Does it exist, or do I need to write something manually? Update Here are some example URLs paired with what I would like the result to be:
example.com/mypage.php : /mypage.php example.com/ : /index.php example.com/foo/?hello=world : /foo/index.php
And these return values are true even in included files. See my answer below before answering, I think I’ve found what I was looking for.
Can you update your question and add a hypothetical example of what you are looking for? It’s not clear whether you are interested in the requesting URL (example.org) or the file that is used to serve the request (/var/www/. /index.php)
5 Answers 5
I decided to test it out myself. The $_SERVER[‘SCRIPT_NAME’] variable serves up the path to the requested file, even if it’s an index file, and without get parameters or anything else. The PHP documentation states this contains the path of the file, but it seems to be relative to the document root, just like PHP_SELF , but without the security vulnerability.
The output when requesting example.com/?foo=bar :
__FILE__: /var/www/index.php PHP_SELF: /index.php SCRIPT_NAME: /index.php REQUEST_URI: /?foo=bar parse_url(REQUEST_URI): / __FILE__: /var/www/pathtest.php PHP_SELF: /index.php SCRIPT_NAME: /index.php REQUEST_URI: /?foo=bar parse_url(REQUEST_URI): /
And the output when requesting example.com/index.php/XSS :
__FILE__: /var/www/index.php PHP_SELF: /index.php/XSS # note the XSS exploit (this is bold in browser) SCRIPT_NAME: /index.php # No exploit here REQUEST_URI: /index.php/%3Cstrong%3EXSS%3C/strong%3E parse_url(REQUEST_URI): /index.php/%3Cstrong%3EXSS%3C/strong%3E __FILE__: /var/www/pathtest.php PHP_SELF: /index.php/XSS SCRIPT_NAME: /index.php REQUEST_URI: /index.php/%3Cstrong%3EXSS%3C/strong%3E parse_url(REQUEST_URI): /index.php/%3Cstrong%3EXSS%3C/strong%3E
As you can see, $_SERVER[‘SCRIPT_NAME’] always gives back the file that originally handled the request, i.e. the file in the URL, without any XSS risks.
PHP Fetch current page name without extension?
I want to show the current pages filename as the page title, but without the extension. And if possible, the first character should be capitalized. Is this possible?
7 Answers 7
ucfirst(pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME))
The second argument to pathinfo() strips the path and extension from the file name (PHP >= 5.2)
Btw, I’m using $_SERVER[‘PHP_SELF’] instead of __FILE__ because otherwise it would break if the code is ran from another file 😉
Yeah, noticed that with a similar solution. So, this is located at nav.php, but I need the title from a file similar to kalvot.php.
So, a page in my project consist of 3 pages: nav.php containerpage.php and footer.php . However, every page should have a different title, and placing this script to nav.php would only echo Nav , right? I need it to echo Containerpage 🙂
@ChristianNikkanen that’s what my code does . it uses PHP_SELF which is roughly what can be seen in the location bar of the browser.
Yes, you can do it, but you may want to do some extra parsing for spaces. However, this is the simplest way, and directly according to your instructions.
echo '';
What I did here is to get the FILE constant which is the name of the file being executed. Find the .php extension and replace it with blank (assuming ‘.php’ doesn’t occur anywhere else in the filename).
Function ucwords() capitalizes the first letter of substrings separated by spaces.
If you want to do further parsing for spaces, you’ll need to recognize the format of your file names and do string replacements/regex accordingly.
How to get URL of current page in PHP [duplicate]
In PHP, how can I get the URL of the current page? Preferably just the parts after http://domain.example .
I find it funny that this question is marked as a duplicate of another despite being asked two years earlier
@cameronjonesweb And that the other question has a totally different scope (getting the full URL), as opposed to this one (getting the current page only)
5 Answers 5
For more details on what info is available in the $_SERVER array, see the PHP manual page for it.
If you also need the query string (the bit after the ? in a URL), that part is in this variable:
iirc, PHP_SELF and REQUEST_URI will have different values if the page was redirected via mod_rewrite — the former has the path to the actual script, the latter has the originally requested path.
$_SERVER[‘REQUEST_URI’] is also contaning all query strings, why should I use $_SERVER[‘QUERY_STRING’] ?
If you want just the parts of URL after http://domain.example , try this:
If the current URL was http://domain.example/some-slug/some-id , echo will return only /some-slug/some-id .
If you want the full URL, try this:
how about replacing http with REQUEST_SCHEME: $_SERVER[‘REQUEST_SCHEME’]. «://» . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘REQUEST_URI’]
This will give you the requested directory and file name. If you use mod_rewrite, this is extremely useful because it tells you what page the user was looking at.
If you need the actual file name, you might want to try either $_SERVER[‘PHP_SELF’] , the magic constant __FILE__ , or $_SERVER[‘SCRIPT_FILENAME’] . The latter 2 give you the complete path (from the root of the server), rather than just the root of your website. They are useful for includes and such.
$_SERVER[‘PHP_SELF’] gives you the file name relative to the root of the website.
$relative_path = $_SERVER['PHP_SELF']; $complete_path = __FILE__; $complete_path = $_SERVER['SCRIPT_FILENAME'];
How to Get the Current Page URL of a Web Page Using PHP
In this tutorial, we show how to get the current page URL of a web page using PHP.
Full URL of Current Page
To get the full URL of the current page, which takes the form http://www.website.com/Directory/pagename.html, you can use the following code below.
The $_SERVER[‘HTTP_HOST’] gets the www.website.com name of the website in use. For this web page you are current on, this would return www.learningaboutelectronics.com
The $_SERVER[‘REQUEST_URI’] returns the directory pathway and file name of the page you are on.
Actual PHP Output of this Page
Directory Pathway and File name
To return just the directory pathway and the file name of the web page, you can use the following code below.
This is the part after http://www.website.com/
So this code above returns the part after the type of domain name the web page is.
Actual PHP Output of this Page
Just the Directory Pathway
To return just the directory pathway of the current web page URL, you can use the following code below.
This code above returns just the directory pathway to the current page URL.
Actual PHP of this Page
Just the File Name
To just return the file name of the current page, you can use the following code below.
This code above returns only the file name of the current page URL.
Actual PHP Output of this Page
Just the File Name Without the File Extension
Sometimes you may just want to return the file name without the file extension.
To do this, you can use the following code below.
The code above returns just the file name without the file extension included.