3

を使用してアプリケーションルートを取得できます

 define("ROOT",dirname(__FILE__); 

しかし、アプリケーションが存在する場所への完全な URL を動的に取得する必要があります。

私のドライブの例の場所は次のとおりです。

 c:\htdocs\website_name\index.php

それから私は必要です:

 http://localhost/website_name/

できれば正しいプロトコル (http または https) を使用する

現在、構成ファイルでアプリケーションの場所をハードコーディングしていますが、動的に実行できることがわかりました。

何か案は?$_SERVER['SERVER_NAME'] のようなものを使用すると名前が付けられると思いますが、残りの変数を機能させる方法がわかりません。

何か案は?

4

1 に答える 1

0

SERVER_NAME is not automatically told to reflect the actual domain name and path. This is defined in the VirtualHost configuration file usually in apache/conf/extra. You'll need to parse the HTTP_HOST in combination with the SERVER_PROTOCOL and SERVER_PORT to get the real domain name.

$domain =  $_SERVER['HTTP_HOST'];
$protocol = (strpos('https', strtolower($_SERVER['SERVER_PROTOCOL'])))
          ? 'https'
          : 'http';
$port = ($_SERVER['SERVER_PORT'] != 80 || $_SERVER['SERVER_PORT'] != 443 && $protocol != "https")
      ? ":".$_SERVER['SERVER_PORT']
      : "";

$path = sub_str(str_pos("index.php", $_SERVER['PHP_SELF']), -1,  $_SERVER['PHP_SELF']);

$done = $protocol . "://".$domain.$protocol.$path;

Code not tested but should show you the flow.

Edit: As for your path name you should parse the PHP_SELF variable, and remove everything starting from index.php (or your script file name).

于 2012-12-19T11:30:19.233 に答える