How do I get the current URL into 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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I get the current URL into a variable?

Post by simonmlewis »

I need to get hold of the URL to identify if it is a /blog/ URL.

I have tried this on PHP7.1, but it is echoing nothing.

Code: Select all

<?php  
echo "The query string is: ".$_SERVER['QUERY_STRING'];  
?> 
What am I doing wrong?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I get the current URL into a variable?

Post by requinix »

$_SERVER[QUERY_STRING] is half right.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I get the current URL into a variable?

Post by simonmlewis »

$url = $_SERVER['REQUEST_URI'];
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: How do I get the current URL into a variable?

Post by thinsoldier »

var_dump( $_SERVER );

And look at all the things you have to choose from. Google the key names to find out more about each.
Warning: I have no idea what I'm talking about.
Post Reply