Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Hãy xem

function url_origin( $s, $use_forwarded_host = false )
{
    $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
    $sp       = strtolower( $s['SERVER_PROTOCOL'] );
    $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
    $port     = $s['SERVER_PORT'];
    $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
    $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
    $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
    return $protocol . '://' . $host;
}

function full_url( $s, $use_forwarded_host = false )
{
    return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
}

$absolute_url = full_url( $_SERVER );
echo $absolute_url;
3, tức là.

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

(Lưu ý rằng cú pháp chuỗi được trích dẫn kép là hoàn toàn chính xác)

Nếu bạn muốn hỗ trợ cả HTTP và HTTPS, bạn có thể sử dụng

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Lưu ý của biên tập viên: Sử dụng mã này có ý nghĩa bảo mật. Máy khách có thể đặt http_host và request_uri thành bất kỳ giá trị tùy ý nào mà nó muốn. using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.

Đã trả lời ngày 20 tháng 7 năm 2011 lúc 21:33Jul 20, 2011 at 21:33

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

ax.ax.ax.

57K7 Huy hiệu vàng78 Huy hiệu bạc71 Huy hiệu đồng7 gold badges78 silver badges71 bronze badges

57

Phiên bản ngắn để liên kết xuất trên trang web

$url =  "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '' . $escaped_url . '';

Dưới đây là một số chi tiết về các vấn đề và trường hợp cạnh của //example.com/path/ Định dạng

Phiên bản đầy đủ

function url_origin( $s, $use_forwarded_host = false )
{
    $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
    $sp       = strtolower( $s['SERVER_PROTOCOL'] );
    $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
    $port     = $s['SERVER_PORT'];
    $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
    $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
    $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
    return $protocol . '://' . $host;
}

function full_url( $s, $use_forwarded_host = false )
{
    return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
}

$absolute_url = full_url( $_SERVER );
echo $absolute_url;

Đây là phiên bản sửa đổi rất nhiều của http://snipplr.com/view.php?codeview&id=2734 (không còn tồn tại)

Cấu trúc URL:

scheme://username:password@domain:port/path?query_string#fragment_idusername:password@domain:port/path?query_string#fragment_id

Các phần in đậm sẽ không được bao gồm bởi chức năng

Notes:

  • Hàm này không bao gồm
    function url_origin( $s, $use_forwarded_host = false )
    {
        $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
        $sp       = strtolower( $s['SERVER_PROTOCOL'] );
        $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
        $port     = $s['SERVER_PORT'];
        $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
        $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
        $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
        return $protocol . '://' . $host;
    }
    
    function full_url( $s, $use_forwarded_host = false )
    {
        return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
    }
    
    $absolute_url = full_url( $_SERVER );
    echo $absolute_url;
    
    4 từ URL đầy đủ hoặc đoạn (băm).
  • Nó sẽ không hiển thị cổng 80 mặc định cho HTTP và cổng 443 cho HTTPS.
  • Chỉ được thử nghiệm với các sơ đồ HTTP và HTTPS.
  • function url_origin( $s, $use_forwarded_host = false )
    {
        $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
        $sp       = strtolower( $s['SERVER_PROTOCOL'] );
        $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
        $port     = $s['SERVER_PORT'];
        $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
        $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
        $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
        return $protocol . '://' . $host;
    }
    
    function full_url( $s, $use_forwarded_host = false )
    {
        return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
    }
    
    $absolute_url = full_url( $_SERVER );
    echo $absolute_url;
    
    5 không được máy khách (trình duyệt) gửi đến máy chủ và sẽ không được thêm vào URL đầy đủ.
  • function url_origin( $s, $use_forwarded_host = false )
    {
        $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
        $sp       = strtolower( $s['SERVER_PROTOCOL'] );
        $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
        $port     = $s['SERVER_PORT'];
        $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
        $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
        $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
        return $protocol . '://' . $host;
    }
    
    function full_url( $s, $use_forwarded_host = false )
    {
        return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
    }
    
    $absolute_url = full_url( $_SERVER );
    echo $absolute_url;
    
    6 sẽ chỉ chứa
    function url_origin( $s, $use_forwarded_host = false )
    {
        $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
        $sp       = strtolower( $s['SERVER_PROTOCOL'] );
        $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
        $port     = $s['SERVER_PORT'];
        $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
        $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
        $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
        return $protocol . '://' . $host;
    }
    
    function full_url( $s, $use_forwarded_host = false )
    {
        return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
    }
    
    $absolute_url = full_url( $_SERVER );
    echo $absolute_url;
    
    7 cho một URL như
    function url_origin( $s, $use_forwarded_host = false )
    {
        $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
        $sp       = strtolower( $s['SERVER_PROTOCOL'] );
        $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
        $port     = $s['SERVER_PORT'];
        $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
        $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
        $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
        return $protocol . '://' . $host;
    }
    
    function full_url( $s, $use_forwarded_host = false )
    {
        return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
    }
    
    $absolute_url = full_url( $_SERVER );
    echo $absolute_url;
    
    8.
  • Một số CMS và môi trường sẽ viết lại
    function url_origin( $s, $use_forwarded_host = false )
    {
        $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
        $sp       = strtolower( $s['SERVER_PROTOCOL'] );
        $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
        $port     = $s['SERVER_PORT'];
        $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
        $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
        $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
        return $protocol . '://' . $host;
    }
    
    function full_url( $s, $use_forwarded_host = false )
    {
        return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
    }
    
    $absolute_url = full_url( $_SERVER );
    echo $absolute_url;
    
    3 và trả về
    // ======= PATHINFO ====== //
    $x = pathinfo($url);
    $x['dirname']      🡺 https://example.com/subFolder
    $x['basename']     🡺                               myfile.php?var=blabla#555 // Unsecure!
    $x['extension']    🡺                                      php?var=blabla#555 // Unsecure!
    $x['filename']     🡺                               myfile
    
    // ======= PARSE_URL ====== //
    $x = parse_url($url);
    $x['scheme']       🡺 https
    $x['host']         🡺         example.com
    $x['path']         🡺                    /subFolder/myfile.php
    $x['query']        🡺                                          var=blabla
    $x['fragment']     🡺                                                     555
    
    //=================================================== //
    //========== Self-defined SERVER variables ========== //
    //=================================================== //
    $_SERVER["DOCUMENT_ROOT"]  🡺 /home/user/public_html
    $_SERVER["SERVER_ADDR"]    🡺 143.34.112.23
    $_SERVER["SERVER_PORT"]    🡺 80 (or 443, etc..)
    $_SERVER["REQUEST_SCHEME"] 🡺 https                                         //similar: $_SERVER["SERVER_PROTOCOL"]
    $_SERVER['HTTP_HOST']      🡺         example.com (or with WWW)             //similar: $_SERVER["SERVER_NAME"]
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    $_SERVER["QUERY_STRING"]   🡺                                             var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    __DIR__                    🡺 /home/user/public_html/subFolder              //same: dirname(__FILE__)
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺  /subFolder/myfile.php
    $_SERVER["PHP_SELF"]       🡺                       /subFolder/myfile.php
    
    // ==================================================================//
    //if "myfile.php" is included in "PARENTFILE.php" , and you visit  "PARENTFILE.PHP?abc":
    $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
    $_SERVER["PHP_SELF"]       🡺                       /parentfile.php
    $_SERVER["REQUEST_URI"]    🡺                       /parentfile.php?var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    
    // =================================================== //
    // ================= handy variables ================= //
    // =================================================== //
    // If the site uses HTTPS:
    $HTTP_or_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' );            //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO'])  ...
    
    // To trim values to filename, i.e.
    basename($url)             🡺 myfile.php
    
    // Excellent solution to find origin
    $debug_files = debug_backtrace();
    $caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1]['file'] : __FILE__;
    
    0 cho một URL như
    function url_origin( $s, $use_forwarded_host = false )
    {
        $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
        $sp       = strtolower( $s['SERVER_PROTOCOL'] );
        $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
        $port     = $s['SERVER_PORT'];
        $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
        $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
        $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
        return $protocol . '://' . $host;
    }
    
    function full_url( $s, $use_forwarded_host = false )
    {
        return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
    }
    
    $absolute_url = full_url( $_SERVER );
    echo $absolute_url;
    
    8, sử dụng
    // ======= PATHINFO ====== //
    $x = pathinfo($url);
    $x['dirname']      🡺 https://example.com/subFolder
    $x['basename']     🡺                               myfile.php?var=blabla#555 // Unsecure!
    $x['extension']    🡺                                      php?var=blabla#555 // Unsecure!
    $x['filename']     🡺                               myfile
    
    // ======= PARSE_URL ====== //
    $x = parse_url($url);
    $x['scheme']       🡺 https
    $x['host']         🡺         example.com
    $x['path']         🡺                    /subFolder/myfile.php
    $x['query']        🡺                                          var=blabla
    $x['fragment']     🡺                                                     555
    
    //=================================================== //
    //========== Self-defined SERVER variables ========== //
    //=================================================== //
    $_SERVER["DOCUMENT_ROOT"]  🡺 /home/user/public_html
    $_SERVER["SERVER_ADDR"]    🡺 143.34.112.23
    $_SERVER["SERVER_PORT"]    🡺 80 (or 443, etc..)
    $_SERVER["REQUEST_SCHEME"] 🡺 https                                         //similar: $_SERVER["SERVER_PROTOCOL"]
    $_SERVER['HTTP_HOST']      🡺         example.com (or with WWW)             //similar: $_SERVER["SERVER_NAME"]
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    $_SERVER["QUERY_STRING"]   🡺                                             var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    __DIR__                    🡺 /home/user/public_html/subFolder              //same: dirname(__FILE__)
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺  /subFolder/myfile.php
    $_SERVER["PHP_SELF"]       🡺                       /subFolder/myfile.php
    
    // ==================================================================//
    //if "myfile.php" is included in "PARENTFILE.php" , and you visit  "PARENTFILE.PHP?abc":
    $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
    $_SERVER["PHP_SELF"]       🡺                       /parentfile.php
    $_SERVER["REQUEST_URI"]    🡺                       /parentfile.php?var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    
    // =================================================== //
    // ================= handy variables ================= //
    // =================================================== //
    // If the site uses HTTPS:
    $HTTP_or_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' );            //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO'])  ...
    
    // To trim values to filename, i.e.
    basename($url)             🡺 myfile.php
    
    // Excellent solution to find origin
    $debug_files = debug_backtrace();
    $caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1]['file'] : __FILE__;
    
    2 trong trường hợp này.
  • Hãy nhớ rằng một URI =
    // ======= PATHINFO ====== //
    $x = pathinfo($url);
    $x['dirname']      🡺 https://example.com/subFolder
    $x['basename']     🡺                               myfile.php?var=blabla#555 // Unsecure!
    $x['extension']    🡺                                      php?var=blabla#555 // Unsecure!
    $x['filename']     🡺                               myfile
    
    // ======= PARSE_URL ====== //
    $x = parse_url($url);
    $x['scheme']       🡺 https
    $x['host']         🡺         example.com
    $x['path']         🡺                    /subFolder/myfile.php
    $x['query']        🡺                                          var=blabla
    $x['fragment']     🡺                                                     555
    
    //=================================================== //
    //========== Self-defined SERVER variables ========== //
    //=================================================== //
    $_SERVER["DOCUMENT_ROOT"]  🡺 /home/user/public_html
    $_SERVER["SERVER_ADDR"]    🡺 143.34.112.23
    $_SERVER["SERVER_PORT"]    🡺 80 (or 443, etc..)
    $_SERVER["REQUEST_SCHEME"] 🡺 https                                         //similar: $_SERVER["SERVER_PROTOCOL"]
    $_SERVER['HTTP_HOST']      🡺         example.com (or with WWW)             //similar: $_SERVER["SERVER_NAME"]
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    $_SERVER["QUERY_STRING"]   🡺                                             var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    __DIR__                    🡺 /home/user/public_html/subFolder              //same: dirname(__FILE__)
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺  /subFolder/myfile.php
    $_SERVER["PHP_SELF"]       🡺                       /subFolder/myfile.php
    
    // ==================================================================//
    //if "myfile.php" is included in "PARENTFILE.php" , and you visit  "PARENTFILE.PHP?abc":
    $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
    $_SERVER["PHP_SELF"]       🡺                       /parentfile.php
    $_SERVER["REQUEST_URI"]    🡺                       /parentfile.php?var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    
    // =================================================== //
    // ================= handy variables ================= //
    // =================================================== //
    // If the site uses HTTPS:
    $HTTP_or_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' );            //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO'])  ...
    
    // To trim values to filename, i.e.
    basename($url)             🡺 myfile.php
    
    // Excellent solution to find origin
    $debug_files = debug_backtrace();
    $caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1]['file'] : __FILE__;
    
    3, nhưng do sử dụng phổ biến, URL bây giờ có nghĩa là cả URI và URL.
  • Bạn nên xóa
    // ======= PATHINFO ====== //
    $x = pathinfo($url);
    $x['dirname']      🡺 https://example.com/subFolder
    $x['basename']     🡺                               myfile.php?var=blabla#555 // Unsecure!
    $x['extension']    🡺                                      php?var=blabla#555 // Unsecure!
    $x['filename']     🡺                               myfile
    
    // ======= PARSE_URL ====== //
    $x = parse_url($url);
    $x['scheme']       🡺 https
    $x['host']         🡺         example.com
    $x['path']         🡺                    /subFolder/myfile.php
    $x['query']        🡺                                          var=blabla
    $x['fragment']     🡺                                                     555
    
    //=================================================== //
    //========== Self-defined SERVER variables ========== //
    //=================================================== //
    $_SERVER["DOCUMENT_ROOT"]  🡺 /home/user/public_html
    $_SERVER["SERVER_ADDR"]    🡺 143.34.112.23
    $_SERVER["SERVER_PORT"]    🡺 80 (or 443, etc..)
    $_SERVER["REQUEST_SCHEME"] 🡺 https                                         //similar: $_SERVER["SERVER_PROTOCOL"]
    $_SERVER['HTTP_HOST']      🡺         example.com (or with WWW)             //similar: $_SERVER["SERVER_NAME"]
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    $_SERVER["QUERY_STRING"]   🡺                                             var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    __DIR__                    🡺 /home/user/public_html/subFolder              //same: dirname(__FILE__)
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺  /subFolder/myfile.php
    $_SERVER["PHP_SELF"]       🡺                       /subFolder/myfile.php
    
    // ==================================================================//
    //if "myfile.php" is included in "PARENTFILE.php" , and you visit  "PARENTFILE.PHP?abc":
    $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
    $_SERVER["PHP_SELF"]       🡺                       /parentfile.php
    $_SERVER["REQUEST_URI"]    🡺                       /parentfile.php?var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    
    // =================================================== //
    // ================= handy variables ================= //
    // =================================================== //
    // If the site uses HTTPS:
    $HTTP_or_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' );            //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO'])  ...
    
    // To trim values to filename, i.e.
    basename($url)             🡺 myfile.php
    
    // Excellent solution to find origin
    $debug_files = debug_backtrace();
    $caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1]['file'] : __FILE__;
    
    4 nếu bạn không có kế hoạch sử dụng proxy hoặc người cân bằng.
  • Thông số kỹ thuật nói rằng tiêu đề
    // ======= PATHINFO ====== //
    $x = pathinfo($url);
    $x['dirname']      🡺 https://example.com/subFolder
    $x['basename']     🡺                               myfile.php?var=blabla#555 // Unsecure!
    $x['extension']    🡺                                      php?var=blabla#555 // Unsecure!
    $x['filename']     🡺                               myfile
    
    // ======= PARSE_URL ====== //
    $x = parse_url($url);
    $x['scheme']       🡺 https
    $x['host']         🡺         example.com
    $x['path']         🡺                    /subFolder/myfile.php
    $x['query']        🡺                                          var=blabla
    $x['fragment']     🡺                                                     555
    
    //=================================================== //
    //========== Self-defined SERVER variables ========== //
    //=================================================== //
    $_SERVER["DOCUMENT_ROOT"]  🡺 /home/user/public_html
    $_SERVER["SERVER_ADDR"]    🡺 143.34.112.23
    $_SERVER["SERVER_PORT"]    🡺 80 (or 443, etc..)
    $_SERVER["REQUEST_SCHEME"] 🡺 https                                         //similar: $_SERVER["SERVER_PROTOCOL"]
    $_SERVER['HTTP_HOST']      🡺         example.com (or with WWW)             //similar: $_SERVER["SERVER_NAME"]
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    $_SERVER["QUERY_STRING"]   🡺                                             var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    __DIR__                    🡺 /home/user/public_html/subFolder              //same: dirname(__FILE__)
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺  /subFolder/myfile.php
    $_SERVER["PHP_SELF"]       🡺                       /subFolder/myfile.php
    
    // ==================================================================//
    //if "myfile.php" is included in "PARENTFILE.php" , and you visit  "PARENTFILE.PHP?abc":
    $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
    $_SERVER["PHP_SELF"]       🡺                       /parentfile.php
    $_SERVER["REQUEST_URI"]    🡺                       /parentfile.php?var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    
    // =================================================== //
    // ================= handy variables ================= //
    // =================================================== //
    // If the site uses HTTPS:
    $HTTP_or_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' );            //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO'])  ...
    
    // To trim values to filename, i.e.
    basename($url)             🡺 myfile.php
    
    // Excellent solution to find origin
    $debug_files = debug_backtrace();
    $caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1]['file'] : __FILE__;
    
    5 phải chứa số cổng trừ khi đó là số mặc định.

Các biến được kiểm soát của máy khách (trình duyệt):

  • function url_origin( $s, $use_forwarded_host = false )
    {
        $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
        $sp       = strtolower( $s['SERVER_PROTOCOL'] );
        $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
        $port     = $s['SERVER_PORT'];
        $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
        $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
        $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
        return $protocol . '://' . $host;
    }
    
    function full_url( $s, $use_forwarded_host = false )
    {
        return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
    }
    
    $absolute_url = full_url( $_SERVER );
    echo $absolute_url;
    
    3. Bất kỳ ký tự không được hỗ trợ nào được mã hóa bởi trình duyệt trước khi chúng được gửi.
  • // ======= PATHINFO ====== //
    $x = pathinfo($url);
    $x['dirname']      🡺 https://example.com/subFolder
    $x['basename']     🡺                               myfile.php?var=blabla#555 // Unsecure!
    $x['extension']    🡺                                      php?var=blabla#555 // Unsecure!
    $x['filename']     🡺                               myfile
    
    // ======= PARSE_URL ====== //
    $x = parse_url($url);
    $x['scheme']       🡺 https
    $x['host']         🡺         example.com
    $x['path']         🡺                    /subFolder/myfile.php
    $x['query']        🡺                                          var=blabla
    $x['fragment']     🡺                                                     555
    
    //=================================================== //
    //========== Self-defined SERVER variables ========== //
    //=================================================== //
    $_SERVER["DOCUMENT_ROOT"]  🡺 /home/user/public_html
    $_SERVER["SERVER_ADDR"]    🡺 143.34.112.23
    $_SERVER["SERVER_PORT"]    🡺 80 (or 443, etc..)
    $_SERVER["REQUEST_SCHEME"] 🡺 https                                         //similar: $_SERVER["SERVER_PROTOCOL"]
    $_SERVER['HTTP_HOST']      🡺         example.com (or with WWW)             //similar: $_SERVER["SERVER_NAME"]
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    $_SERVER["QUERY_STRING"]   🡺                                             var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    __DIR__                    🡺 /home/user/public_html/subFolder              //same: dirname(__FILE__)
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺  /subFolder/myfile.php
    $_SERVER["PHP_SELF"]       🡺                       /subFolder/myfile.php
    
    // ==================================================================//
    //if "myfile.php" is included in "PARENTFILE.php" , and you visit  "PARENTFILE.PHP?abc":
    $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
    $_SERVER["PHP_SELF"]       🡺                       /parentfile.php
    $_SERVER["REQUEST_URI"]    🡺                       /parentfile.php?var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    
    // =================================================== //
    // ================= handy variables ================= //
    // =================================================== //
    // If the site uses HTTPS:
    $HTTP_or_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' );            //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO'])  ...
    
    // To trim values to filename, i.e.
    basename($url)             🡺 myfile.php
    
    // Excellent solution to find origin
    $debug_files = debug_backtrace();
    $caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1]['file'] : __FILE__;
    
    7 và không phải lúc nào cũng có sẵn theo các bình luận trong hướng dẫn sử dụng PHP: http://php.net/manual/en/reserved.variables.php
  • // ======= PATHINFO ====== //
    $x = pathinfo($url);
    $x['dirname']      🡺 https://example.com/subFolder
    $x['basename']     🡺                               myfile.php?var=blabla#555 // Unsecure!
    $x['extension']    🡺                                      php?var=blabla#555 // Unsecure!
    $x['filename']     🡺                               myfile
    
    // ======= PARSE_URL ====== //
    $x = parse_url($url);
    $x['scheme']       🡺 https
    $x['host']         🡺         example.com
    $x['path']         🡺                    /subFolder/myfile.php
    $x['query']        🡺                                          var=blabla
    $x['fragment']     🡺                                                     555
    
    //=================================================== //
    //========== Self-defined SERVER variables ========== //
    //=================================================== //
    $_SERVER["DOCUMENT_ROOT"]  🡺 /home/user/public_html
    $_SERVER["SERVER_ADDR"]    🡺 143.34.112.23
    $_SERVER["SERVER_PORT"]    🡺 80 (or 443, etc..)
    $_SERVER["REQUEST_SCHEME"] 🡺 https                                         //similar: $_SERVER["SERVER_PROTOCOL"]
    $_SERVER['HTTP_HOST']      🡺         example.com (or with WWW)             //similar: $_SERVER["SERVER_NAME"]
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    $_SERVER["QUERY_STRING"]   🡺                                             var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    __DIR__                    🡺 /home/user/public_html/subFolder              //same: dirname(__FILE__)
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺  /subFolder/myfile.php
    $_SERVER["PHP_SELF"]       🡺                       /subFolder/myfile.php
    
    // ==================================================================//
    //if "myfile.php" is included in "PARENTFILE.php" , and you visit  "PARENTFILE.PHP?abc":
    $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
    $_SERVER["PHP_SELF"]       🡺                       /parentfile.php
    $_SERVER["REQUEST_URI"]    🡺                       /parentfile.php?var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    
    // =================================================== //
    // ================= handy variables ================= //
    // =================================================== //
    // If the site uses HTTPS:
    $HTTP_or_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' );            //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO'])  ...
    
    // To trim values to filename, i.e.
    basename($url)             🡺 myfile.php
    
    // Excellent solution to find origin
    $debug_files = debug_backtrace();
    $caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1]['file'] : __FILE__;
    
    8 được thiết lập bởi các bộ cân bằng và không được đề cập trong danh sách các biến
    // ======= PATHINFO ====== //
    $x = pathinfo($url);
    $x['dirname']      🡺 https://example.com/subFolder
    $x['basename']     🡺                               myfile.php?var=blabla#555 // Unsecure!
    $x['extension']    🡺                                      php?var=blabla#555 // Unsecure!
    $x['filename']     🡺                               myfile
    
    // ======= PARSE_URL ====== //
    $x = parse_url($url);
    $x['scheme']       🡺 https
    $x['host']         🡺         example.com
    $x['path']         🡺                    /subFolder/myfile.php
    $x['query']        🡺                                          var=blabla
    $x['fragment']     🡺                                                     555
    
    //=================================================== //
    //========== Self-defined SERVER variables ========== //
    //=================================================== //
    $_SERVER["DOCUMENT_ROOT"]  🡺 /home/user/public_html
    $_SERVER["SERVER_ADDR"]    🡺 143.34.112.23
    $_SERVER["SERVER_PORT"]    🡺 80 (or 443, etc..)
    $_SERVER["REQUEST_SCHEME"] 🡺 https                                         //similar: $_SERVER["SERVER_PROTOCOL"]
    $_SERVER['HTTP_HOST']      🡺         example.com (or with WWW)             //similar: $_SERVER["SERVER_NAME"]
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    $_SERVER["QUERY_STRING"]   🡺                                             var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    __DIR__                    🡺 /home/user/public_html/subFolder              //same: dirname(__FILE__)
    $_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
    parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺  /subFolder/myfile.php
    $_SERVER["PHP_SELF"]       🡺                       /subFolder/myfile.php
    
    // ==================================================================//
    //if "myfile.php" is included in "PARENTFILE.php" , and you visit  "PARENTFILE.PHP?abc":
    $_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
    $_SERVER["PHP_SELF"]       🡺                       /parentfile.php
    $_SERVER["REQUEST_URI"]    🡺                       /parentfile.php?var=blabla
    __FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
    
    // =================================================== //
    // ================= handy variables ================= //
    // =================================================== //
    // If the site uses HTTPS:
    $HTTP_or_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' );            //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO'])  ...
    
    // To trim values to filename, i.e.
    basename($url)             🡺 myfile.php
    
    // Excellent solution to find origin
    $debug_files = debug_backtrace();
    $caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1]['file'] : __FILE__;
    
    9 trong hướng dẫn sử dụng PHP.

Các biến điều khiển máy chủ:

  • // (Let's say, if WordPress is installed in subdirectory:  http://example.com/wpdir/)
    home_url()                      🡺 http://example.com/wpdir/        // If is_ssl() is true, then it will be "https"
    get_stylesheet_directory_uri()  🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
    get_stylesheet_directory()      🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
    plugin_dir_url(__FILE__)        🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
    plugin_dir_path(__FILE__)       🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
    
    0. Máy khách chọn sử dụng điều này, nhưng máy chủ trả về giá trị thực của trống hoặc "BẬT".
  • // (Let's say, if WordPress is installed in subdirectory:  http://example.com/wpdir/)
    home_url()                      🡺 http://example.com/wpdir/        // If is_ssl() is true, then it will be "https"
    get_stylesheet_directory_uri()  🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
    get_stylesheet_directory()      🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
    plugin_dir_url(__FILE__)        🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
    plugin_dir_path(__FILE__)       🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
    
    1. Máy chủ chỉ chấp nhận các số được phép làm cổng.
  • // (Let's say, if WordPress is installed in subdirectory:  http://example.com/wpdir/)
    home_url()                      🡺 http://example.com/wpdir/        // If is_ssl() is true, then it will be "https"
    get_stylesheet_directory_uri()  🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
    get_stylesheet_directory()      🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
    plugin_dir_url(__FILE__)        🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
    plugin_dir_path(__FILE__)       🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
    
    2. Máy chủ chỉ chấp nhận các giao thức nhất định.
  • // (Let's say, if WordPress is installed in subdirectory:  http://example.com/wpdir/)
    home_url()                      🡺 http://example.com/wpdir/        // If is_ssl() is true, then it will be "https"
    get_stylesheet_directory_uri()  🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
    get_stylesheet_directory()      🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
    plugin_dir_url(__FILE__)        🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
    plugin_dir_path(__FILE__)       🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
    
    3. Nó được đặt thủ công trong cấu hình máy chủ và không có sẵn cho IPv6 theo Kralyk.

Related:

Sự khác biệt giữa http_host và server_name trong PHP là gì? Số cổng có được yêu cầu trong tham số tiêu đề "Máy chủ" HTTP không? https://stackoverflow.com/a/28049503/175071
Is Port Number Required in HTTP "Host" Header Parameter?
https://stackoverflow.com/a/28049503/175071

Đã trả lời ngày 17 tháng 1 năm 2012 lúc 8:57Jan 17, 2012 at 8:57

Timo Huovinentimo HuovinenTimo Huovinen

51.2K33 Huy hiệu vàng145 Huy hiệu bạc136 Huy hiệu đồng33 gold badges145 silver badges136 bronze badges

24

Ví dụ cho:

// (Let's say, if WordPress is installed in subdirectory:  http://example.com/wpdir/)
home_url()                      🡺 http://example.com/wpdir/        // If is_ssl() is true, then it will be "https"
get_stylesheet_directory_uri()  🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
get_stylesheet_directory()      🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
plugin_dir_url(__FILE__)        🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
plugin_dir_path(__FILE__)       🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
4
// (Let's say, if WordPress is installed in subdirectory:  http://example.com/wpdir/)
home_url()                      🡺 http://example.com/wpdir/        // If is_ssl() is true, then it will be "https"
get_stylesheet_directory_uri()  🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
get_stylesheet_directory()      🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
plugin_dir_url(__FILE__)        🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
plugin_dir_path(__FILE__)       🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
4

// ======= PATHINFO ====== //
$x = pathinfo($url);
$x['dirname']      🡺 https://example.com/subFolder
$x['basename']     🡺                               myfile.php?var=blabla#555 // Unsecure!
$x['extension']    🡺                                      php?var=blabla#555 // Unsecure!
$x['filename']     🡺                               myfile

// ======= PARSE_URL ====== //
$x = parse_url($url);
$x['scheme']       🡺 https
$x['host']         🡺         example.com
$x['path']         🡺                    /subFolder/myfile.php
$x['query']        🡺                                          var=blabla
$x['fragment']     🡺                                                     555

//=================================================== //
//========== Self-defined SERVER variables ========== //
//=================================================== //
$_SERVER["DOCUMENT_ROOT"]  🡺 /home/user/public_html
$_SERVER["SERVER_ADDR"]    🡺 143.34.112.23
$_SERVER["SERVER_PORT"]    🡺 80 (or 443, etc..)
$_SERVER["REQUEST_SCHEME"] 🡺 https                                         //similar: $_SERVER["SERVER_PROTOCOL"]
$_SERVER['HTTP_HOST']      🡺         example.com (or with WWW)             //similar: $_SERVER["SERVER_NAME"]
$_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
$_SERVER["QUERY_STRING"]   🡺                                             var=blabla
__FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
__DIR__                    🡺 /home/user/public_html/subFolder              //same: dirname(__FILE__)
$_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺  /subFolder/myfile.php
$_SERVER["PHP_SELF"]       🡺                       /subFolder/myfile.php

// ==================================================================//
//if "myfile.php" is included in "PARENTFILE.php" , and you visit  "PARENTFILE.PHP?abc":
$_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
$_SERVER["PHP_SELF"]       🡺                       /parentfile.php
$_SERVER["REQUEST_URI"]    🡺                       /parentfile.php?var=blabla
__FILE__                   🡺 /home/user/public_html/subFolder/myfile.php

// =================================================== //
// ================= handy variables ================= //
// =================================================== //
// If the site uses HTTPS:
$HTTP_or_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' );            //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO'])  ...

// To trim values to filename, i.e.
basename($url)             🡺 myfile.php

// Excellent solution to find origin
$debug_files = debug_backtrace();
$caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1]['file'] : __FILE__;

Notice!

  • Hashtag
    // (Let's say, if WordPress is installed in subdirectory:  http://example.com/wpdir/)
    home_url()                      🡺 http://example.com/wpdir/        // If is_ssl() is true, then it will be "https"
    get_stylesheet_directory_uri()  🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
    get_stylesheet_directory()      🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
    plugin_dir_url(__FILE__)        🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
    plugin_dir_path(__FILE__)       🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
    
    5 các bộ phận được sử dụng thủ công trong ví dụ trên chỉ cho mục đích minh họa, tuy nhiên, các ngôn ngữ phía máy chủ (bao gồm cả PHP) không thể phát hiện ra chúng (chỉ JavaScript mới có thể làm điều đó, vì hashtag chỉ là chức năng phụ của trình duyệt/máy khách) .
  • // (Let's say, if WordPress is installed in subdirectory:  http://example.com/wpdir/)
    home_url()                      🡺 http://example.com/wpdir/        // If is_ssl() is true, then it will be "https"
    get_stylesheet_directory_uri()  🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
    get_stylesheet_directory()      🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
    plugin_dir_url(__FILE__)        🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
    plugin_dir_path(__FILE__)       🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
    
    6 trả về
    // (Let's say, if WordPress is installed in subdirectory:  http://example.com/wpdir/)
    home_url()                      🡺 http://example.com/wpdir/        // If is_ssl() is true, then it will be "https"
    get_stylesheet_directory_uri()  🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
    get_stylesheet_directory()      🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
    plugin_dir_url(__FILE__)        🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
    plugin_dir_path(__FILE__)       🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
    
    7 cho lưu trữ kiểu Windows, thay vì
    // (Let's say, if WordPress is installed in subdirectory:  http://example.com/wpdir/)
    home_url()                      🡺 http://example.com/wpdir/        // If is_ssl() is true, then it will be "https"
    get_stylesheet_directory_uri()  🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
    get_stylesheet_directory()      🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
    plugin_dir_url(__FILE__)        🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
    plugin_dir_path(__FILE__)       🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
    
    8.

____

Cho WordPress

// (Let's say, if WordPress is installed in subdirectory:  http://example.com/wpdir/)
home_url()                      🡺 http://example.com/wpdir/        // If is_ssl() is true, then it will be "https"
get_stylesheet_directory_uri()  🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
get_stylesheet_directory()      🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
plugin_dir_url(__FILE__)        🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
plugin_dir_path(__FILE__)       🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Đã trả lời ngày 15 tháng 11 năm 2014 lúc 9:59Nov 15, 2014 at 9:59

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

T.ToduaT.ToduaT.Todua

50.3K19 Huy hiệu vàng218 Huy hiệu bạc214 Huy hiệu đồng19 gold badges218 silver badges214 bronze badges

5

Đây là một giải pháp sử dụng câu lệnh ternary, giữ mã tối thiểu:

$url = "http" . (($_SERVER['SERVER_PORT'] == 443) ? "s" : "") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

Đây là cách nhỏ nhất và dễ nhất để thực hiện việc này, giả sử máy chủ web của một người đang sử dụng cổng tiêu chuẩn 443 cho HTTPS.

Blackbam

15.8K23 Huy hiệu vàng87 Huy hiệu bạc142 Huy hiệu đồng23 gold badges87 silver badges142 bronze badges

Đã trả lời ngày 24 tháng 4 năm 2012 lúc 13:30Apr 24, 2012 at 13:30

Honyovkhonyovkhonyovk

2.68718 Huy hiệu bạc26 Huy hiệu đồng18 silver badges26 bronze badges

3

Phương pháp nền tảng chéo yêu thích của tôi để tìm URL hiện tại là:

$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Yukulélé

14.4K10 Huy hiệu vàng63 Huy hiệu bạc90 Huy hiệu Đồng10 gold badges63 silver badges90 bronze badges

Đã trả lời ngày 18 tháng 5 năm 2014 lúc 1:54May 18, 2014 at 1:54

1

Đơn giản chỉ cần sử dụng:

$uri = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Đã trả lời ngày 9 tháng 12 năm 2015 lúc 9:07Dec 9, 2015 at 9:07

HappyCoderhappyCoderHappyCoder

5.7376 Huy hiệu vàng41 Huy hiệu bạc72 Huy hiệu đồng6 gold badges41 silver badges72 bronze badges

5

function full_path()
{
    $s = &$_SERVER;
    $ssl = (!empty($s['HTTPS']) && $s['HTTPS'] == 'on') ? true:false;
    $sp = strtolower($s['SERVER_PROTOCOL']);
    $protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
    $port = $s['SERVER_PORT'];
    $port = ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
    $host = isset($s['HTTP_X_FORWARDED_HOST']) ? $s['HTTP_X_FORWARDED_HOST'] : (isset($s['HTTP_HOST']) ? $s['HTTP_HOST'] : null);
    $host = isset($host) ? $host : $s['SERVER_NAME'] . $port;
    $uri = $protocol . '://' . $host . $s['REQUEST_URI'];
    $segments = explode('?', $uri, 2);
    $url = $segments[0];
    return $url;
}

Lưu ý: Tôi vừa thực hiện bản cập nhật cho mã của Timo Huovinen, vì vậy bạn sẽ không nhận được bất kỳ tham số nào trong URL. URL này là đơn giản và loại bỏ những thứ như

// (Let's say, if WordPress is installed in subdirectory:  http://example.com/wpdir/)
home_url()                      🡺 http://example.com/wpdir/        // If is_ssl() is true, then it will be "https"
get_stylesheet_directory_uri()  🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
get_stylesheet_directory()      🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
plugin_dir_url(__FILE__)        🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
plugin_dir_path(__FILE__)       🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/
9.

Example:

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
0

sẽ được hiển thị như:

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
1

Điều này là tốt trừ khi bạn sử dụng Get paramater để xác định một số nội dung cụ thể, trong trường hợp đó bạn nên sử dụng mã của anh ấy! :-)

Trig

9.8346 huy hiệu vàng55 Huy hiệu bạc106 Huy hiệu đồng6 gold badges55 silver badges106 bronze badges

Đã trả lời ngày 26 tháng 10 năm 2012 lúc 13:15Oct 26, 2012 at 13:15

2

Clear Code, làm việc trong tất cả các webserver (Apache, nginx, IIS, ...):

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
2

Đã trả lời ngày 27 tháng 4 năm 2016 lúc 15:57Apr 27, 2016 at 15:57

AndreasandreasAndreas

2.76523 Huy hiệu bạc 30 Huy hiệu Đồng23 silver badges30 bronze badges

Kỹ thuật tương tự như câu trả lời được chấp nhận, nhưng với hỗ trợ HTTPS và dễ đọc hơn:

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
3

Trên đây cho những dấu gạch chéo không mong muốn. Trên thiết lập của tôi yêu cầu_uri có dấu gạch chéo dẫn đầu và theo dõi. Điều này hoạt động tốt hơn cho tôi.Request_URI has leading and trailing slashes. This works better for me.

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
4

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Rohit Gupta

3,47812 Huy hiệu vàng26 Huy hiệu bạc40 Huy hiệu đồng12 gold badges26 silver badges40 bronze badges

Đã trả lời ngày 1 tháng 11 năm 2014 lúc 21:14Nov 1, 2014 at 21:14

Đồi Jonathon HilljonathonJonathon Hill

3,3581 Huy hiệu vàng33 Huy hiệu bạc31 Huy hiệu đồng1 gold badge33 silver badges31 bronze badges

2

Http_host và request_uri phải có báo giá, nếu không nó sẽ gây ra lỗi trong Php 7.2

Sử dụng:

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
5

Nếu bạn muốn hỗ trợ cả HTTP và HTTPS:

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
6

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Đã trả lời ngày 28 tháng 6 năm 2018 lúc 6:04Jun 28, 2018 at 6:04

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

AvataravatarAvatar

13.4K8 Huy hiệu vàng113 Huy hiệu bạc185 Huy hiệu Đồng8 gold badges113 silver badges185 bronze badges

Đây là giải pháp của tôi - Mã được lấy cảm hứng từ Tracy Debuger. Nó đã được thay đổi để hỗ trợ các cổng máy chủ khác nhau. Bạn có thể nhận được URL đầy đủ hiện tại bao gồm

function url_origin( $s, $use_forwarded_host = false )
{
    $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
    $sp       = strtolower( $s['SERVER_PROTOCOL'] );
    $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
    $port     = $s['SERVER_PORT'];
    $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
    $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
    $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
    return $protocol . '://' . $host;
}

function full_url( $s, $use_forwarded_host = false )
{
    return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
}

$absolute_url = full_url( $_SERVER );
echo $absolute_url;
3 hoặc chỉ URL máy chủ cơ bản. Kiểm tra chức năng của tôi:

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
7

Đây là mã kiểm tra:

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
8

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Đã trả lời ngày 7 tháng 8 năm 2013 lúc 10:41Aug 7, 2013 at 10:41

OzzyczechozzyczechOzzyCzech

9.0172 Huy hiệu vàng47 Huy hiệu bạc31 Huy hiệu Đồng2 gold badges47 silver badges31 bronze badges

1

Tôi đã thực hiện chức năng này để xử lý URL:

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
9

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Đã trả lời ngày 21 tháng 5 năm 2014 lúc 8:06May 21, 2014 at 8:06

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Điều này khá dễ dàng để thực hiện với các biến môi trường Apache của bạn. Điều này chỉ hoạt động với Apache 2, mà tôi cho rằng bạn đang sử dụng.

Chỉ cần sử dụng mã PHP sau:

$url =  "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '' . $escaped_url . '';
0

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Đã trả lời ngày 20 tháng 7 năm 2011 lúc 21:52Jul 20, 2011 at 21:52

AmarjitamarjitAmarjit

3112 Huy hiệu bạc10 Huy hiệu đồng2 silver badges10 bronze badges

1

Sử dụng một lớp lót này để tìm URL thư mục chính (nếu bạn không có quyền truy cập vào http_build_url () đi kèm với PECL_HTTP):

$url =  "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '' . $escaped_url . '';
1

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Đã trả lời ngày 14 tháng 2 năm 2015 lúc 4:50Feb 14, 2015 at 4:50

1

Đây là giải pháp cho vấn đề của bạn:

$url =  "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '' . $escaped_url . '';
2

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Đã trả lời ngày 26 tháng 9 năm 2013 lúc 6:33Sep 26, 2013 at 6:33

Vaibhav Jainvaibhav JainVaibhav Jain

4732 Huy hiệu vàng7 Huy hiệu bạc13 Huy hiệu đồng2 gold badges7 silver badges13 bronze badges

Thử cái này:

$url =  "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '' . $escaped_url . '';
3

// ======= PATHINFO ====== //
$x = pathinfo($url);
$x['dirname']      🡺 https://example.com/subFolder
$x['basename']     🡺                               myfile.php?var=blabla#555 // Unsecure!
$x['extension']    🡺                                      php?var=blabla#555 // Unsecure!
$x['filename']     🡺                               myfile

// ======= PARSE_URL ====== //
$x = parse_url($url);
$x['scheme']       🡺 https
$x['host']         🡺         example.com
$x['path']         🡺                    /subFolder/myfile.php
$x['query']        🡺                                          var=blabla
$x['fragment']     🡺                                                     555

//=================================================== //
//========== Self-defined SERVER variables ========== //
//=================================================== //
$_SERVER["DOCUMENT_ROOT"]  🡺 /home/user/public_html
$_SERVER["SERVER_ADDR"]    🡺 143.34.112.23
$_SERVER["SERVER_PORT"]    🡺 80 (or 443, etc..)
$_SERVER["REQUEST_SCHEME"] 🡺 https                                         //similar: $_SERVER["SERVER_PROTOCOL"]
$_SERVER['HTTP_HOST']      🡺         example.com (or with WWW)             //similar: $_SERVER["SERVER_NAME"]
$_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
$_SERVER["QUERY_STRING"]   🡺                                             var=blabla
__FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
__DIR__                    🡺 /home/user/public_html/subFolder              //same: dirname(__FILE__)
$_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺  /subFolder/myfile.php
$_SERVER["PHP_SELF"]       🡺                       /subFolder/myfile.php

// ==================================================================//
//if "myfile.php" is included in "PARENTFILE.php" , and you visit  "PARENTFILE.PHP?abc":
$_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
$_SERVER["PHP_SELF"]       🡺                       /parentfile.php
$_SERVER["REQUEST_URI"]    🡺                       /parentfile.php?var=blabla
__FILE__                   🡺 /home/user/public_html/subFolder/myfile.php

// =================================================== //
// ================= handy variables ================= //
// =================================================== //
// If the site uses HTTPS:
$HTTP_or_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' );            //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO'])  ...

// To trim values to filename, i.e.
basename($url)             🡺 myfile.php

// Excellent solution to find origin
$debug_files = debug_backtrace();
$caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1]['file'] : __FILE__;
9 là một mảng chứa thông tin như tiêu đề, đường dẫn và vị trí tập lệnh. Các mục trong mảng này được tạo bởi máy chủ web. Không có gì đảm bảo rằng mọi máy chủ web sẽ cung cấp bất kỳ máy chủ nào trong số này; Máy chủ có thể bỏ qua một số, hoặc cung cấp những người khác không được liệt kê ở đây. Điều đó nói rằng, một số lượng lớn các biến này được tính trong các đặc tả »CGI/1.1, vì vậy bạn sẽ có thể mong đợi những điều đó.

$url = "http" . (($_SERVER['SERVER_PORT'] == 443) ? "s" : "") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
2 chứa cùng một thông tin ban đầu, nhưng không phải là một siêu thị. (Lưu ý rằng
$url = "http" . (($_SERVER['SERVER_PORT'] == 443) ? "s" : "") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
2 và
// ======= PATHINFO ====== //
$x = pathinfo($url);
$x['dirname']      🡺 https://example.com/subFolder
$x['basename']     🡺                               myfile.php?var=blabla#555 // Unsecure!
$x['extension']    🡺                                      php?var=blabla#555 // Unsecure!
$x['filename']     🡺                               myfile

// ======= PARSE_URL ====== //
$x = parse_url($url);
$x['scheme']       🡺 https
$x['host']         🡺         example.com
$x['path']         🡺                    /subFolder/myfile.php
$x['query']        🡺                                          var=blabla
$x['fragment']     🡺                                                     555

//=================================================== //
//========== Self-defined SERVER variables ========== //
//=================================================== //
$_SERVER["DOCUMENT_ROOT"]  🡺 /home/user/public_html
$_SERVER["SERVER_ADDR"]    🡺 143.34.112.23
$_SERVER["SERVER_PORT"]    🡺 80 (or 443, etc..)
$_SERVER["REQUEST_SCHEME"] 🡺 https                                         //similar: $_SERVER["SERVER_PROTOCOL"]
$_SERVER['HTTP_HOST']      🡺         example.com (or with WWW)             //similar: $_SERVER["SERVER_NAME"]
$_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
$_SERVER["QUERY_STRING"]   🡺                                             var=blabla
__FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
__DIR__                    🡺 /home/user/public_html/subFolder              //same: dirname(__FILE__)
$_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?var=blabla
parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺  /subFolder/myfile.php
$_SERVER["PHP_SELF"]       🡺                       /subFolder/myfile.php

// ==================================================================//
//if "myfile.php" is included in "PARENTFILE.php" , and you visit  "PARENTFILE.PHP?abc":
$_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
$_SERVER["PHP_SELF"]       🡺                       /parentfile.php
$_SERVER["REQUEST_URI"]    🡺                       /parentfile.php?var=blabla
__FILE__                   🡺 /home/user/public_html/subFolder/myfile.php

// =================================================== //
// ================= handy variables ================= //
// =================================================== //
// If the site uses HTTPS:
$HTTP_or_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' );            //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO'])  ...

// To trim values to filename, i.e.
basename($url)             🡺 myfile.php

// Excellent solution to find origin
$debug_files = debug_backtrace();
$caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1]['file'] : __FILE__;
9 là các biến khác nhau và PHP xử lý chúng như vậy)

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Ram Sharma

8,5467 Huy hiệu vàng44 Huy hiệu bạc55 Huy hiệu Đồng7 gold badges44 silver badges55 bronze badges

Đã trả lời ngày 6 tháng 11 năm 2013 lúc 7:52Nov 6, 2013 at 7:52

0

Bạn có thể sử dụng http_build_url mà không có đối số để có được URL đầy đủ của trang hiện tại:

$url =  "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '' . $escaped_url . '';
4

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Pang

9.223146 Huy hiệu vàng85 Huy hiệu bạc118 Huy hiệu đồng146 gold badges85 silver badges118 bronze badges

Đã trả lời ngày 24 tháng 11 năm 2014 lúc 4:56Nov 24, 2014 at 4:56

Luke Mlsnaluke MlsnaLuke Mlsna

4584 Huy hiệu bạc16 Huy hiệu đồng4 silver badges16 bronze badges

1

$url =  "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '' . $escaped_url . '';
5

Đã trả lời ngày 15 tháng 7 năm 2018 lúc 9:46Jul 15, 2018 at 9:46

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

SmarteistSmarteistsmarteist

1.20211 Huy hiệu bạc13 Huy hiệu đồng11 silver badges13 bronze badges

1

Dưới đây là cơ sở của phiên bản an toàn hơn của câu trả lời được chấp nhận, sử dụng chức năng Filter_Input của PHP, cũng bù đắp cho việc thiếu tiềm năng của

function url_origin( $s, $use_forwarded_host = false )
{
    $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
    $sp       = strtolower( $s['SERVER_PROTOCOL'] );
    $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
    $port     = $s['SERVER_PORT'];
    $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
    $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
    $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
    return $protocol . '://' . $host;
}

function full_url( $s, $use_forwarded_host = false )
{
    return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
}

$absolute_url = full_url( $_SERVER );
echo $absolute_url;
3:

$url =  "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '' . $escaped_url . '';
6

Bạn có thể sử dụng một số bộ lọc khác nhau để điều chỉnh nó theo ý thích của bạn.

Đã trả lời ngày 18 tháng 6 năm 2018 lúc 19:06Jun 18, 2018 at 19:06

CodercoderCoder

2.6362 Huy hiệu vàng21 Huy hiệu bạc23 Huy hiệu đồng2 gold badges21 silver badges23 bronze badges

Tôi đã sử dụng tuyên bố này.

$url =  "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '' . $escaped_url . '';
7

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Đã trả lời ngày 14 tháng 3 năm 2015 lúc 13:41Mar 14, 2015 at 13:41

PhonpanomphonpanomPhonPanom

3775 Huy hiệu bạc7 Huy hiệu Đồng5 silver badges7 bronze badges

Tôi đã sử dụng mã dưới đây và nó hoạt động tốt cho tôi, cho cả hai trường hợp, HTTP và HTTPS.

$url =  "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '' . $escaped_url . '';
8

Thử nghiệm

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Đã trả lời ngày 22 tháng 1 năm 2017 lúc 5:48Jan 22, 2017 at 5:48

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

2

Sử dụng rất đơn giản:

$url =  "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '' . $escaped_url . '';
9

Đã trả lời ngày 12 tháng 5 năm 2018 lúc 17:22May 12, 2018 at 17:22

Abbas Arifabbas ArifAbbas Arif

3543 Huy hiệu bạc14 Huy hiệu Đồng3 silver badges14 bronze badges

3

Use:

function url_origin( $s, $use_forwarded_host = false )
{
    $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
    $sp       = strtolower( $s['SERVER_PROTOCOL'] );
    $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
    $port     = $s['SERVER_PORT'];
    $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
    $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
    $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
    return $protocol . '://' . $host;
}

function full_url( $s, $use_forwarded_host = false )
{
    return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
}

$absolute_url = full_url( $_SERVER );
echo $absolute_url;
0

Nguồn: Phát hiện root, đường dẫn và URL của PHP

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Đã trả lời ngày 12 tháng 1 năm 2015 lúc 10:36Jan 12, 2015 at 10:36

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Hpakniahpakniahpaknia

2.6613 Huy hiệu vàng33 Huy hiệu bạc59 Huy hiệu Đồng3 gold badges33 silver badges59 bronze badges

1

Bạn có thể sử dụng

$url = "http" . (($_SERVER['SERVER_PORT'] == 443) ? "s" : "") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
6 như được minh họa trong đoạn trích bên dưới:

function url_origin( $s, $use_forwarded_host = false )
{
    $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
    $sp       = strtolower( $s['SERVER_PROTOCOL'] );
    $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
    $port     = $s['SERVER_PORT'];
    $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
    $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
    $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
    return $protocol . '://' . $host;
}

function full_url( $s, $use_forwarded_host = false )
{
    return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
}

$absolute_url = full_url( $_SERVER );
echo $absolute_url;
1

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

Nyedidikeke

6.2427 Huy hiệu vàng43 Huy hiệu bạc53 Huy hiệu Đồng7 gold badges43 silver badges53 bronze badges

Đã trả lời ngày 13 tháng 4 năm 2017 lúc 8:35Apr 13, 2017 at 8:35

Ninjaninjaninja

4651 Huy hiệu vàng7 Huy hiệu bạc13 Huy hiệu đồng1 gold badge7 silver badges13 bronze badges

2

Tôi nghĩ phương pháp này là tốt..ture nó

function url_origin( $s, $use_forwarded_host = false )
{
    $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
    $sp       = strtolower( $s['SERVER_PROTOCOL'] );
    $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
    $port     = $s['SERVER_PORT'];
    $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
    $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
    $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
    return $protocol . '://' . $host;
}

function full_url( $s, $use_forwarded_host = false )
{
    return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
}

$absolute_url = full_url( $_SERVER );
echo $absolute_url;
2

Đã trả lời ngày 2 tháng 6 năm 2015 lúc 3:00Jun 2, 2015 at 3:00

Hướng dẫn how do i find my php url? - làm cách nào để tìm url php của tôi?

UWU_SANDUNUWU_SANDUNUWU_SANDUN

1.06713 Huy hiệu bạc18 Huy hiệu đồng13 silver badges18 bronze badges

Làm thế nào tôi có thể nhận URL trong PHP?

Các biến superglobal cần thiết, chẳng hạn như $ _server ['https'], $ _server ['request_uri'], $ _server ['server_port'] được sử dụng để có URL đầy đủ trong PHP. HTTPS biến có thể dễ dàng truy xuất giao thức trong URL của trang web. Nếu nó trả về một giá trị trên mạng, thì giao thức là HTTPS.$_SERVER['HTTPS'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PORT'] are used to get full URL in PHP. The variable HTTPS can easily retrieve the protocol in the URL of a webpage. If it returns a value “on”, then the protocol is HTTPS.

URL PHP là gì?

Định nghĩa của URL PHP.Nói chung, URL có nghĩa là người định vị tài nguyên thống nhất.Tương tự như vậy, URL trong ngôn ngữ lập trình PHP cũng giống nhau.URL không có gì ngoài một địa chỉ trang web.Nó giúp kết nối máy khách và máy chủ khi được duyệt bằng một liên kết cụ thể.Uniform Resource Locater. Likewise, URL in PHP Programming Language is also the same. URL is nothing but a website address. It helps in connecting the client and the server when browsed using a specific link.

Làm cách nào để nhận được một URL đầy đủ?

Nối HTTP_HOST (máy chủ mà chúng tôi đã yêu cầu, ví dụ: www.google.com, www.yourdomain.com, v.v.) Tên của máy chủ.Nối thêm request_uri (tài nguyên mà chúng tôi đã yêu cầu, ví dụ: /chỉ mục. PHP, v.v.) vào chuỗi URL.

Làm thế nào để tôi có được URL hiện tại trong HTML?

Trả lời: Sử dụng thuộc tính Window.location.Href PropertyHref để có toàn bộ URL của trang hiện tại bao gồm tên máy chủ, chuỗi truy vấn, nhận dạng phân đoạn, v.v.Use the window. location. href Property href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.