Skip to main content
Known Participant
January 2, 2010
Answered

Is there a PHP call for "current url" string?

  • January 2, 2010
  • 1 reply
  • 734 views

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?

This topic has been closed for replies.
Correct answer David_Powers

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'].

1 reply

David_Powers
David_PowersCorrect answer
Inspiring
January 2, 2010

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'].

jyeager11Author
Known Participant
January 7, 2010

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.

David_Powers
Inspiring
January 7, 2010

$_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>