开发者

How do I get the url of the current include script in PHP?

I have a file called index.php which includes a class from another file (class/event.class.php) with the following code:

 <?php
      require_once('class/event.class.php');
      $evt = new Event;
      $evt->doSome开发者_如何学Cthing();
 ?>

The file event.class.php should be able to read the URL used in the request (not $_SERVER["SCRIPT_FILENAME") to the file.

ie: http://localhost:8888/class/event.class.php

How do I do that?


Using the $_SERVER array, example:

$protocol = 'http';
$port = '';

if ( isset( $_SERVER['HTTPS'] ) && strcasecmp( $_SERVER['HTTPS'], 'on' ) == 0 ) {
    $protocol = 'https';
}
if ( isset( $_SERVER['SERVER_PORT'] ) ) {
    $port = ':' . $_SERVER['SERVER_PORT'];
}

// Don't display the standard ports :80 for http and :443 for https
if ( ( $protocol == 'http' && $port == ':80' ) || ( $protocol == 'https' && $port == ':443' ) ) {
    $port = '';
}

$host_url = $protocol . '://' . $_SERVER['SERVER_NAME'] . $port;
$base_url = rtrim( str_replace( basename( $_SERVER['SCRIPT_NAME'] ), '', $_SERVER['SCRIPT_NAME'] ), '/' );


Here is a nice succinct line I have used for a long time, and it works very nicely.

$myURL = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

echo $myURL;

EDIT Using an edited version of this function I lifted from the PHP manual, and the __FILE__ magic constant, it can be done like this (untested):

function getRelativePath ( $path, $compareTo ) {

    // Replace \ with / and remove leading/trailing slashes (for Windows)
    $path = trim(str_replace('\\','/',$path),'/');
    $compareTo = trim(str_replace('\\','/',$compareTo),'/');

    // simple case: $compareTo is in $path
    if (strpos($path, $compareTo) === 0) {
        $offset = strlen($compareTo) + 1;
        return substr($path, $offset);
    }

    $relative  = array();
    $pathParts = explode('/', $path);
    $compareToParts = explode('/', $compareTo);

    foreach($compareToParts as $index => $part) {
        if (isset($pathParts[$index]) && $pathParts[$index] == $part) {
            continue;
        }
        $relative[] = '..';
    }

    foreach( $pathParts as $index => $part ) {
        if (isset($compareToParts[$index]) && $compareToParts[$index] == $part) {
            continue;
        }
        $relative[] = $part;
    }

    return implode('/',$relative);

}

$myURL = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].'/'.ltrim(getRelativePath(realpath($_SERVER['PHP_SELF']),__FILE__),'/');

echo $myURL;


What I do to get a browser path to my files (I include this in index.php, but having this code in event.class.php would return the folder):

$this_file = dirname($_SERVER['PHP_SELF']) . '/';

$a_file_that_you_want_to_access_by_url = $this_file.'class/event.class.php';

Also, if you want to access the current directory 'root' in all of your .php files define a constant like this:

define('uri_root',          dirname($_SERVER['PHP_SELF']) . '/',  true);
$a_file_that_you_want_to_access_by_url = uri_root.'class/event.class.php';

This script will echo the script name with all sub-folders

echo dirname($_SERVER['PHP_SELF']).$_SERVER['PHP_SELF'];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜