Getting url of a page from an include on the page

Discussion in 'PHP' started by offthedome, Aug 26, 2010.

  1. #1
    I have a whole slew of pages, for example ourspacer.com/graphics. In every page on ourspacer.com, I have the same include that shows the link trades and purchased links on the right. In the links on the right, I had a facebook "Like" button. The problem with that Like button is that when you click on it, it puts the URL of the include on facebook, not the URL of the page you are visiting.

    Is there a way of using PHP to get the URL of the page being viewed from an include on that page?

    The original function that I was using to determine the URL of the site, which actually gave me the URL of the include, is the following:

    
    <?php 
    function PageURL() {
     $pageURL = 'http';
     if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
     $pageURL .= "://";
     if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
     } else {
      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
     }
     return $pageURL;
    }
    ?>
    
    PHP:
     
    Last edited: Aug 26, 2010
    offthedome, Aug 26, 2010 IP
  2. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #2
    Thats weird REQUEST_URI doesn't usually do that. You can try $_SERVER['SCRIPT_NAME'] and $_SERVER['PHP_SELF']
     
    Narrator, Aug 26, 2010 IP
  3. offthedome

    offthedome Active Member

    Messages:
    446
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    95
    #3
    I just tested the code I posted above, and apparently it matters whether you use a relative or absolute URL:

    http://addressmunger.com/test/
     
    offthedome, Aug 26, 2010 IP
  4. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #4
    I would of thought it would just be easier to use Javascript to get this done, might save you alot of time and effort!

    Kind regards,

    Glen Hughes
     
    HuggyEssex, Aug 27, 2010 IP
  5. offthedome

    offthedome Active Member

    Messages:
    446
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    95
    #5
    That's what I ended up doing. I don't know why the thought didn't even cross my mind. Must be a "slow" day ;-)
     
    offthedome, Aug 27, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    Im not too sure what you exactly are looking for but if your looking to retrieve the url of the included file you can look into get_included_files() (which returns an array) and can use it along with the pathinfo() function.
     
    danx10, Aug 27, 2010 IP