Ilja
Ilja

Reputation: 46479

How to detect current page with php?

I need to detect page to do some styling on a website, right now I'm using

$currentpage = $_SERVER['REQUEST_URI'];

but this returns something like /development/en/contact.php
I would like something that will only return contact.php
is there a method to achieve this?

Upvotes: 2

Views: 5468

Answers (4)

Andrew D.
Andrew D.

Reputation: 1022

basename(__FILE__) is also a way.
__FILE__ 'magic' constant

Upvotes: 2

TimWolla
TimWolla

Reputation: 32701

basename($_SERVER['SCRIPT_NAME']);

It will return only the file and strips the folders. For more information see basename()

Note the usage of SCRIPT_NAME instead of REQUEST_URI as REQUEST_URI may contain additional "crap":

http://example.com/dev/en/contact.php/this/is/crap
REQUEST_URI: /dev/en/contact.php/this/is/crap
SCRIPT_NAME: /dev/en/contact.php

Upvotes: 9

rspeed
rspeed

Reputation: 1662

This should get you what you want:

basename($_SERVER["SCRIPT_FILENAME"]);

Though be careful, as it will give you the same answer for files with the same name in different locations.

Upvotes: 3

user800014
user800014

Reputation:

substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

Upvotes: 1

Related Questions