Can't get PHP to get the current URL

I’m trying to make social buttons for my website and I’d like to have it where the buttons automatically use the current URL, no matter what page it is on.

I’ve managed to make the following code with what I’ve looked up (I have very little knowlegde or experience with PHP) :

[php]<?php
$shareUrl = ‘http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]’;
print ‘

Share on Social Media


Facebook
Twitter
Reddit
Tumblr


?>[/php]

This doesn’t work since it doesn’t use the variable that I’ve defined. How can I fix this?

It doesn’t work, because you are not including the variables in the string properly.

Starting out, how about you use the concatenation operator ( . ), on the variables instead of using the string literal single quote.
The other convoluted way is below.

 [php]
<?php $shareUrl = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; print "

Share on Social Media

Facebook Twitter Reddit Tumblr

" ?>[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service