How to Get Current PHP Page Name

How to get current PHP page name

You can use basename() and $_SERVER['PHP_SELF'] to get current page file name

echo basename($_SERVER['PHP_SELF']); /* Returns The Current PHP File Name */

PHP - how can i know the current page name im in it?

You can access a lot of information from the $_SERVER array.

$_SERVER['PHP_SELF'] 

will provide you with the current file name your calling it from, e.g :

example.php

you can then use a plethora of methods to remove the file extention, if you know what it is the easiest would be :

$filename = str_replace(".php","",$_SERVER['PHP_SELF']);

Here's a nice one liner from this question:

$filename = ucfirst(pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME))

How to get current .php or .html file name by JavaScript?

Window Location

Verify if this document can help you.

It must be something, like this:

var page = window.location.href;

Get the current script file name

Just use the PHP magic constant __FILE__ to get the current filename.

But it seems you want the part without .php. So...

basename(__FILE__, '.php'); 

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) {
return substr($filename, 0, strrpos($filename, '.'));
}

How can I get current page name including $_GET variable in URL?

The variable $_SERVER["REQUEST_URI"] gives you the file with GET parameters. Also includes folders in the url.

Edit: Use $page = end(explode('/', $_SERVER["REQUEST_URI"])); if you want to get rid of the folders from the url.

PHP trouble with current page name

$pagename = basename($_SERVER['PHP_SELF']);
if($pagename=='index.php'){
echo "class='active'";
}

How to get the current page name(url) and add it as a class to the body

Try this one:

<?php
$url = $_SERVER['REQUEST_URI'];
preg_match('|.*/(.*)|', $url, $matches);
?>
<body class = "<?php echo $matches[1]; ?>">

PHP Fetch current page name without extension?

Everyone loves one-liners:

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 ;-)

Get current domain

Try using this:

$_SERVER['SERVER_NAME']

Or parse:

$_SERVER['REQUEST_URI']

Reference: apache_request_headers()



Related Topics



Leave a reply



Submit