Print url

Discussion in 'PHP' started by William, Oct 25, 2008.

  1. #1
    How can I get wordpress to automatically print the url of a that page on every page.

    Ex:
    You are browsing (URL here)
    (Using the same seo friendly urls as I set in the admin panel)
     
    William, Oct 25, 2008 IP
  2. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Hey, check out the php section on $_SERVER variables: http://us2.php.net/reserved.variables.server

    Specifically you would want HTTP_HOST and PHP_SELF.

    I think what your looking for would be like:

    
    echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER[PHP_SELF];
    
    PHP:
    That should give you what you need.
     
    ToddMicheau, Oct 25, 2008 IP
  3. William

    William Well-Known Member

    Messages:
    1,310
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Thanks. That might work but after having slept I found a more wordpress suitable solution:

    <?php the_permalink() ?>
    Code (markup):
     
    William, Oct 26, 2008 IP
  4. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No no no, this is bad code. It has an XSS exploit.

    What he wants is

    echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];


    you see, echoing php self can lead to XSS exploits by accessing http://site.com/file.php/<script>alert('XSS')</script>

    this is why you should use script name, or escape PHP self with htmlspecialchars or entities

    here's a demonstration of how PHP_SELF behaves:

    http://wocares.com/analyzerer.php/XSSHERE
     
    Kyosys, Oct 26, 2008 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    The above answers will not work on https or other ports. Taken from webcheatsheet.com:

    
    <?php
    function curPageURL() {
     $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:
    Peace,
     
    Barti1987, Oct 26, 2008 IP
  6. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    good point. You should check for the protocol
     
    Kyosys, Oct 26, 2008 IP