Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 12-15-2005, 09:46 PM   PM User | #1
Pennimus
Senior Coder

 
Join Date: Jul 2005
Location: UK
Posts: 1,051
Thanks: 6
Thanked 13 Times in 13 Posts
Pennimus is on a distinguished road
Retrieving Full URL

I found this piece of code that will do it

http://dev.kanngard.net/Permalinks/I...507183447.html

However this seems quite long-winded for what I imagined would be quite a basic function. Is there a shorter way to do this (something akin to $_SERVER[PHP_SELF] but for the full URL)?
Pennimus is offline   Reply With Quote
Old 12-15-2005, 09:48 PM   PM User | #2
djdykes
New Coder

 
Join Date: Dec 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
djdykes is an unknown quantity at this point
PHP Code:
$url "http://" $_SERVER['HTTP_HOST'] . "/" $_SERVER['PHP_SELF'] . "/" $_SERVER['QUERYSTRING'] . "/"
or using javascript

window.document.location.toString()
__________________
Learn PHP at PHP for Beginners or perhaps a little Poker?
djdykes is offline   Reply With Quote
Old 12-15-2005, 11:03 PM   PM User | #3
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 824
Thanks: 2
Thanked 1 Time in 1 Post
Element is an unknown quantity at this point
Quote:
Originally Posted by djdykes
PHP Code:
$url "http://" $_SERVER['HTTP_HOST'] . "/" $_SERVER['PHP_SELF'] . "/" $_SERVER['QUERYSTRING'] . "/"
or using javascript

window.document.location.toString()
Umm there are a few errors there, and that won't catch the directories though. Its much better to use:

PHP Code:
 echo "http://" $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . "?" $_SERVER['QUERY_STRING']; 
Right?
__________________
Check out TV Shack, and get entertained!
Element is offline   Reply With Quote
Old 12-15-2005, 11:06 PM   PM User | #4
Pennimus
Senior Coder

 
Join Date: Jul 2005
Location: UK
Posts: 1,051
Thanks: 6
Thanked 13 Times in 13 Posts
Pennimus is on a distinguished road
Thanks for the nudge in the right direction guys, I have a working version now.
Pennimus is offline   Reply With Quote
Old 12-15-2005, 11:15 PM   PM User | #5
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 824
Thanks: 2
Thanked 1 Time in 1 Post
Element is an unknown quantity at this point
Quote:
Originally Posted by Pennimus
Thanks for the nudge in the right direction guys, I have a working version now.
Error in mine to, you don't need $_SERVER['QUERY_STRING'] and if you want a function to do it like. fetch_url() to use where ever like even in a form like:

Code:
<form name="form1" method="post" action="<?php echo fetch_url(); ?>">
And that sort of function is easy and a very basic so its even good to build off of.

PHP Code:
<?php

function fetch_url() {
  
$url "http://" $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  return 
$url;
}

// Example again
echo "<a href=\"" fetch_url() . "\">" fetch_url() . "</a>";

?>
__________________
Check out TV Shack, and get entertained!
Element is offline   Reply With Quote
Old 12-16-2005, 08:09 AM   PM User | #6
Bill Posters
Senior Coder

 
Join Date: Feb 2003
Posts: 1,665
Thanks: 0
Thanked 27 Times in 25 Posts
Bill Posters will become famous soon enough
Quote:
Originally Posted by Element
Umm there are a few errors there, and that won't catch the directories though. Its much better to use:

PHP Code:
…$_SERVER['REQUEST_URI']… 
I get the same output from PHP_SELF as I get from REQUEST_URI.
Both show the directories when concatinated within a string containing HTTP_HOST, PHP_SELF and QUERY_STRING to echo the full url for the current page.

What are the supposed differences and in what circumstances do they become evident?
Bill Posters is offline   Reply With Quote
Old 12-16-2005, 10:27 AM   PM User | #7
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
djdykes:

$_SERVER['REQUEST_URI'] shows only the path to the file, whereas $_SERVER['PHP_SELF'] gives the path to the file as well as the query string. You should be able to use this:

PHP Code:
$uri 'http://' $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; 
to get your URI as well, although either method is fine.
missing-score is offline   Reply With Quote
Old 12-16-2005, 11:08 AM   PM User | #8
Bill Posters
Senior Coder

 
Join Date: Feb 2003
Posts: 1,665
Thanks: 0
Thanked 27 Times in 25 Posts
Bill Posters will become famous soon enough
Quote:
Originally Posted by missing-score
djdykes:

$_SERVER['REQUEST_URI'] shows only the path to the file, whereas $_SERVER['PHP_SELF'] gives the path to the file as well as the query string
Hmmm, $_SERVER['PHP_SELF'] doesn't include the query string when I try it. To reference/echo the query string, I explicitly need to use $_SERVER['QUERY_STRING'].

Perhaps it's a version thing. I'm running PHP 5.01.
Bill Posters is offline   Reply With Quote
Old 12-16-2005, 04:41 PM   PM User | #9
trib4lmaniac
Regular Coder

 
trib4lmaniac's Avatar
 
Join Date: Feb 2004
Location: Cornwall, UK
Posts: 535
Thanks: 0
Thanked 0 Times in 0 Posts
trib4lmaniac is an unknown quantity at this point
What was wrong with the version you linked to? It even gets the protocol and port number by the looks of it. It doesn't matter if it's a little complex when it's tucked away in a function

Edit: And I'm pretty sure PHP_SELF doesn't include the query string in any version of PHP.

Last edited by trib4lmaniac; 12-16-2005 at 04:44 PM..
trib4lmaniac is offline   Reply With Quote
Old 12-16-2005, 06:42 PM   PM User | #10
circusbred
Regular Coder

 
Join Date: Apr 2004
Location: Birmingham, MI
Posts: 131
Thanks: 0
Thanked 0 Times in 0 Posts
circusbred is an unknown quantity at this point
$_SERVER['PHP_SELF'] will give you the EXECUTING script, so if you are using mod_rewrite, you may have some problems.
$_SERVER['REQUEST_URI'] gives the actual request (as entered in the address bar)
circusbred is offline   Reply With Quote
Old 12-16-2005, 07:37 PM   PM User | #11
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 824
Thanks: 2
Thanked 1 Time in 1 Post
Element is an unknown quantity at this point
Quote:
Originally Posted by missing-score
djdykes:

$_SERVER['REQUEST_URI'] shows only the path to the file, whereas $_SERVER['PHP_SELF'] gives the path to the file as well as the query string. You should be able to use this:

PHP Code:
$uri 'http://' $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; 
to get your URI as well, although either method is fine.
$_SERVER['REQUEST_URI'] does give the query string. It gives everything after the domain. Lol.
__________________
Check out TV Shack, and get entertained!
Element is offline   Reply With Quote
Old 12-16-2005, 08:04 PM   PM User | #12
Velox Letum
Senior Coder

 
Join Date: Apr 2005
Location: Colorado, United States
Posts: 1,208
Thanks: 0
Thanked 0 Times in 0 Posts
Velox Letum is an unknown quantity at this point
Indeed. Example from a phpinfo.php:

_SERVER["QUERY_STRING"] query=string&query2=string2
_SERVER["REQUEST_URI"] /phpinfo.php?query=string&query2=string2
_SERVER["SCRIPT_NAME"] /phpinfo.php
__________________
"$question = ( to() ) ? be() : ~be();"
Velox Letum is offline   Reply With Quote
Old 12-16-2005, 08:09 PM   PM User | #13
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 824
Thanks: 2
Thanked 1 Time in 1 Post
Element is an unknown quantity at this point
Quote:
Originally Posted by Velox Letum
Indeed. Example from a phpinfo.php:

_SERVER["QUERY_STRING"] query=string&query2=string2
_SERVER["REQUEST_URI"] /phpinfo.php?query=string&query2=string2
_SERVER["SCRIPT_NAME"] /phpinfo.php


_SERVER['HTTP_HOST'] => ameirkanmetz.rave5.com
_SERVER['QUERY_STRING'] => a=b&b=b
_SERVER['REQUEST_URI'] => /blah/demo/test/what.php?a=b&b=b
_SERVER['SCRIPT_NAME'] => /what.php
__________________
Check out TV Shack, and get entertained!
Element is offline   Reply With Quote
Old 05-08-2009, 09:31 AM   PM User | #14
jigishpthakar
New to the CF scene

 
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
jigishpthakar is an unknown quantity at this point
hii thanks for the above info,
but its not satisfying my requirement

what i need to trace is not ust url with query strin but i also need to trace #href

i.e. if my url is
http://jigishthakar.com/?p=2#cid

then till
http://jigishthakar.com/?p=2 ur code works fine but now how to trace #cid ?
jigishpthakar is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:35 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.