Copy link to clipboard
Copied
Is there such a thing as a variable that can be included in a PHP document that would translate into the full url of the current page?
For example, at the bottom of every CNN news article, there's a Facebook link designed to share the URL of the current page on Facebook. I doubt this link is hard-coded. That's got to be a variable.
How would I go about doing that? In other words, to replace this :
<a href="http://www.facebook.com/share.php?u=http://domain.com/file.php">
With this :
<a href="http://www.facebook.com/share.php?u=('$CURRENT_URL')">
(Or something like that.)
What would the syntax be?
Copy link to clipboard
Copied
mjyeager wrote:
Is there such a thing as a variable that can be included in a PHP document that would translate into the full url of the current page?
Yes. $_SERVER['PHP_SELF'].
Copy link to clipboard
Copied
Hi David,
How would you incorporate this into a static link?
For instance, to submit a url to Facebook, one would use the format :
http://www.facebook.com/share.php?u=http://domain.com/folder/file.html
So my first instinct (as a total n00b) was to do this :
<a href="http://www.facebook.com/share.php?u=<?php $_SERVER['PHP_SELF'] ?>">Submit to Facebook</a>
Which doesn't work. I also tried :
<a href="http://www.facebook.com/share.php?u=$_SERVER['PHP_SELF']">Submit to Facebook</a>
Which didn't work either.
What would be the correct syntax for a static link that would submit different urls depending on what page is using said link?
Thanks.
Copy link to clipboard
Copied
$_SERVER['PHP_SELF'] is a variable like any other. It contains a value that's ready for you to use, but it doesn't do anything by itself. If you want to display that value, you need to use echo.
<a href="http://www.facebook.com/share.php?u=<?php echo $_SERVER['PHP_SELF']; ?>">Submit to Facebook</a>
Copy link to clipboard
Copied
That worked. Except it did not refer the full url. Only the folders + file.
<a href="http://www.facebook.com/share.php?u=<?php echo $_SERVER['PHP_SELF']; ?>">Facebook</a>
Will produce :
http://www.facebook.com/share.php?u=/folder/filename.php
Now granted, I can very easily enough hard-code "http://domain.com/" between the u= and the echo, but I was wondering if you had a better way to fo it.
PS: Can I thank you one more time, and point out how many countless hours you've saved me with your help in various threads? Well, even if I can't, I'm going to do so. 😃
Copy link to clipboard
Copied
This should do it, as long as it's supported on your server:
echo $_SERVER['SCRIPT_FILENAME'];
Can I thank you one more time, and point out how many countless hours you've saved me with your help in various threads? Well, even if I can't, I'm going to do so. 😃
No problem. I received a lot of help from others when I was learning. There's absolutely no obligation to do so, but if you ever plan to buy anything from Amazon, go through one of the links on my website (http://foundationphp.com/). Amazon pays me a small commission, at no extra cost to you. It helps pay for the upkeep of my site.
Copy link to clipboard
Copied
That string actually fetches "/home/user/domain.com/folder/file.htm" (in other words, the full path on the server, and not the full url of the webpage).
Copy link to clipboard
Copied
Sorry, so it does. I've never had the need to do this before, but the following should do the trick:
echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
You'll need to test it on your server. Not all servers support the full range of $_SERVER variables.
Copy link to clipboard
Copied
Worked like a charm! Thanks.