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:       : /mypage.php                 : /index.php : /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.

civicrm how to identify the php page

I want to edit civicrm custom form section. I can not find the PHP page.

how identify geographical location and redirect in particular page with php?

I would like to ask, how I can identify geographical location and redirect the user in their geographic web page using php. I know the redirection is like. <?php header(‘Location: mypage.php’); ?&g

How to identify server IP address in PHP

How can I identify the server IP address in PHP?

How to determine which content page requested in Master page

How I can determine which content page requested from master page load method?for ezample I have content pages that use a master page.How I can determine which of these three pages requsted by user fr

How to identify CMYK image in PHP

Can anyone tell me how to identify an image if it is in CMYK or RGB using PHP ?

Display PHP Errors Within a Requested Page

I have a some Ajax on a PHP page which requests another PHP page a bunch of times to process part numbers and return certain information. The only problem is when I accidentally make some sort of PHP

How to store Requested Page url?

By Default Person is Redirected to home page after logging in and if person request for any page without login, it will be automatically redirected to login page. Now my question is that how to store

How to rewrite any page to index page with requested URL as $_GET parameter

Something similar had been discussed here, but this is slight different. How to tell .htaccess to rewrite any requested page (regardless of it’s depth from the site root) with site’s index page, but w

Page Being Requested Twice

I’m having a trouble with my CodeIgniter site. I notice everytime I go to a page, the page is requested twice by the browser (Tried on Firefox and Chrome and different computers too). Here’s a screens

How do I limit my PHP script to only run when requested by localhost?

I’m having trouble getting my php page to only run when it is requested by the server itself. This is what I have right now: if ($_SERVER[‘SERVER_ADDR’] == $_SERVER[‘REMOTE_ADDR’]) { //process page }



Should return the actual script. But there are various methods.

I had a better link to a matrix of all the various file-related environment variables but I can’t find it. I’ll edit if it turns up.

Edit: I found a nice SO thread that details the differences between them.

  1. parse_url($_SERVER[‘REQUEST_URI’]) and then pathinfo($path) to get requested filename
  2. $_SERVER[‘PHP_SELF’] to get real filename
  3. $_SERVER[‘SCRIPT_NAME’] to get real filename

You are probably looking for $_SERVER[‘PATH_INFO’]

You can also do some other fancy things with it

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 me to be relative to the document root, just like PHP_SELF, but without the security vulnerability.

Here is the code I used to test this:

The output when requesting

__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<strong>XSS</strong>:

__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 security risks.

Its very old question and not very clear too. What I understood is that you want to know which page is sending request GET/POST. This can be implemented by:


Now, to get the actual page name, write like: = basename($_SERVER[‘HTTP_REFERER’]);

This will solve you concern.

Go get file name from the requested URL use following code.


use any one all all of those in the nested if condition so you will not miss file name any how.