Full URL into a php variable

Discussion in 'PHP' started by webbusiness23, Sep 5, 2009.

  1. #1
    I need to know how to store the full URL into a PHP variable and echo that variable.

    I searched for it on Google but no result.
    Can you help me?
     
    webbusiness23, Sep 5, 2009 IP
  2. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #2
    I'm writing it without documentation at hand, but should be something like:

    $my_url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_FILENAME'];
    PHP:
     
    caprichoso, Sep 5, 2009 IP
  3. redpicasso

    redpicasso Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If i understood you:
    
    <?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
      echo curPageURL();
    ?>
    PHP:
     
    redpicasso, Sep 6, 2009 IP
  4. webbusiness23

    webbusiness23 Member

    Messages:
    153
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #4

    It's not working.

    I created a html page with this code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <?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
      echo curPageURL();
    ?>
    
    </body>
    </html>
    PHP:

    and i uploaded it on my server.

    Your code does not show what it was suppose to.
     
    webbusiness23, Sep 6, 2009 IP
  5. realturk

    realturk Active Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #5
    this code perfect... tanx my localhost run...

    screen :

    http://www.localhost/asd.php

    work is perfect :)
     
    realturk, Sep 13, 2009 IP