954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Display URL

I want to display the URL of a page using PHP.

Basically do this:

with PHP

stupidfly
Light Poster
36 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
echo $url
paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 13
Solved Threads: 4
 

The problem is that I used a form with "get". So I need all that information in the URL. when I use that javascript tag, it displays the correct URL, but it I can't insert the javascript into 'href=' like I can with PHP.

stupidfly
Light Poster
36 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

ok then well theres two ways to do this then...

$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."?".$_SERVER['QUERY_STRING'];
echo $url;


this will always assume there is arguments in the address so u will always see a question mark int the address, a better way of doing this would be to test if there actually was any arguments before just adding the question mark such as in this example:

$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
if (!empty($_SERVER["QUERY_STRING"]))
	$url .= "?".$_SERVER['QUERY_STRING'];

echo $url;


i hope this answers any questions you do have

paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 13
Solved Threads: 4
 

Thank you! It works wonderfully. Exactly what I wanted. May you live long and prosper.

Godspeed.

stupidfly
Light Poster
36 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

:eek:paradox814, you beet me tothe answer again. Good job m8!:p vbmenu_register("postmenu_82884", true);

mikeSQL
Junior Poster
196 posts since Dec 2004
Reputation Points: 16
Solved Threads: 3
 

hey i just rememberd one case in which this won't work!!!

if you are usign a secure ssl http session... in which case replace http:// with https:// (notice the s)

ok man, peace out

paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 13
Solved Threads: 4
 

No, I'm not. It works fine. Thanks for the heads up though.

stupidfly
Light Poster
36 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

how do you do that just with out the file in it... just with the directory in it.

ragstergurr2122
Newbie Poster
1 post since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You