Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get page URL

Status
Not open for further replies.

chrisjwray

Programmer
Jan 17, 2005
9
CA
Hi, can anybody tell me a function to get the name of the current page in PHP.

For example to call a function in index.php which will return "
Thanks in advance

Chris
 
Yep, PHP_SELF will return the name of the current page, but give this a go:

Code:
<?php
	$addr = "[URL unfurl="true"]http://".$_SERVER[/URL]['HTTP_HOST'].$_SERVER['PHP_SELF'];
	echo($addr);
?>

Marc
 
HMarcBower, Why not simply:

Code:
CODE
<?php
    $addr = "[URL unfurl="true"]http://{$_SERVER[/URL]['HTTP_HOST']}{$_SERVER['PHP_SELF']}";
    echo($addr);
?>

ps. I did not test your code, so I dont know if it is "perfect", but I know that gluing together strings is slow.

Olav Alexander Mjelde
Admin & Webmaster
 
DaButcher:
On my test system, I've found explicit use of the "." concatenation operator to be faster.

Here is my test code:

Code:
<?php
$foo = array ('one' => 'three', 'two' => '/four');

$start = microtime (TRUE);

for ($counter = 0; $counter < 1000000; $counter++)
{
	$addr = "[URL unfurl="true"]http://".$foo[/URL]['one'].$foo['two'];
	//$addr = "[URL unfurl="true"]http://{$foo[/URL]['one']}{$foo['two']}";
}

$end = microtime(TRUE);

print $end - $start;
?>

When the script is run as posted, the average runtime seems to be about 4.8 seconds. If I comment out the line which uses explicit concatenation and uncomment the line which uses implied (?) concatenation, the average runtime seems to be about 5.8 seconds.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks guys, that was perfect :). I needed a piece of code to switch language on a page without hard coding the page name.
Using
Code:
echo $_SERVER['PHP_SELF']."?lang=".$language
for the link sorted it.
 
Glad it worked for you Chris.

As for the concatenation... there are about six ways to do anything in PHP, it seems, and we all have our favourites. :)

Marc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top