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
How to Pass Parameters into a PHP Script Through a Webpage
How to Get Current PHP Page Name
Continuing Overflowed Text in a Different Div
Simplexml Get Attributes with a Namespace
PHP Ltrim Behavior with Character List
Show Hide Payment Methods Based on Selected Shipping Method in Woocommerce
How to Obtain Anchor Part of Url After # in PHP
Leverage Browser Caching, How on Apache or .Htaccess
New Containing Div After Every 3 Records
Why Should I Use $_Get and $_Post Instead of $_Request
In PHP What Does It Mean by a Function Being Binary-Safe