Storing current url in a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Storing current url in a variable

Post by kkonline »

I want to store the current page url in variable $link

I used

Code: Select all

$link=$_SERVER['PHP_SELF'];
But it gave me only pages.php if the current link is http://localhost/pages.php?catid=1&id=3

This is required as i am storing the url in a social bookmarking site

What to do?
Ballam
Forum Newbie
Posts: 4
Joined: Sun Sep 02, 2007 7:32 am

Post by Ballam »

I was actually doing the same thing yesterday, but for a different reason!


To get the full url you're best doing this:

$variable = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];



Hope this helps
B
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Post by kkonline »

Ballam wrote:I was actually doing the same thing yesterday, but for a different reason!


To get the full url you're best doing this:

$variable = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

Hope this helps
B
It works if i use

Code: Select all

$link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
I am wondering what should i write if there are some sub directories
http://localhost/article/pages.php?page=1&catid=1

will the above code still work???
Ballam
Forum Newbie
Posts: 4
Joined: Sun Sep 02, 2007 7:32 am

Post by Ballam »

yeah that should work fine :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There are a lot of other things you need to be concerned with regarding the rebuilding of the current URL.

viewtopic.php?t=37950&highlight=geturl
Post Reply