Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Getting Current URL

Tags:

php

This seems like a common questions but none of them seem to be what i'm looking for. If there is a website:

www.example.com/test.php

I want to pull the test.php and if it's something like:

www.example.com/folder/folder1/test.php

I want again to just pull test.php but everything seems to pull the exact path instead of just the page. Any suggestions?

like image 427
Howdy_McGee Avatar asked Jun 25 '11 01:06

Howdy_McGee


People also ask

How do you get the current page URL?

Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.

How can I get core PHP URL?

php function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") {$pageURL . = "s";} $pageURL . = "://"; if ($_SERVER["SERVER_PORT"] !=

How get slug URL in PHP?

$slugs = explode("/", $_GET['params']); This will give you an array filled with every element in your URL. This allows you to use each element as you require.


3 Answers

$query = $_SERVER['PHP_SELF'];
$path = pathinfo( $query );
$what_you_want = $path['basename'];

Voila.

like image 120
Nahydrin Avatar answered Oct 24 '22 01:10

Nahydrin


This will get you the name of the file that was requested (based on the URL invoked, not real location of the file - just in case you are using mod_rewrite):

$filename = basename($_SERVER['REQUEST_URI']);

For both http://www.example.com/testing01.php and http://www.example.com/x/y/z/testing01.php will give you:

testing01.php
like image 37
Tadeck Avatar answered Oct 24 '22 02:10

Tadeck


$file = $_SERVER["SCRIPT_NAME"];
$break = explode('/', $file);
$pfile = $break[count($break) - 1]; 
like image 3
Matt Healy Avatar answered Oct 24 '22 00:10

Matt Healy