Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编码点火器';纯php中的s base_url()函数_Php_Codeigniter - Fatal编程技术网

编码点火器';纯php中的s base_url()函数

编码点火器';纯php中的s base_url()函数,php,codeigniter,Php,Codeigniter,有没有一种方法可以像codeigniter一样只使用php获取基本url(codeigniter中的url\u helper) 我的项目地址看起来像 http://localhost/proj1/ 它可以是任何名称 如何获取在结构树中的任何文件上提供相同基本URL的URL,这取决于所有情况,如http://或https://设置文件config.php <?php define('base_url','http://localhost/proj1/'); ?> 或者,如

有没有一种方法可以像codeigniter一样只使用php获取基本url(codeigniter中的url\u helper)

我的项目地址看起来像

http://localhost/proj1/
它可以是任何名称

如何获取在结构树中的任何文件上提供相同基本URL的URL,这取决于所有情况,如
http://
https://

设置文件config.php

<?php

   define('base_url','http://localhost/proj1/');

?>
或者,如果您正在使用子文件夹,则:

$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
define('base_url',$root);
编辑2:试试这个:

define('ROOTPATH', realpath(dirname(__FILE__)) . '/');


// installed in the docroot?
if (realpath(dirname(__FILE__)) == $_SERVER['DOCUMENT_ROOT'])
{
    define('ROOT', '/');
}
else
{
    define('ROOT', substr(ROOTPATH, strlen($_SERVER['DOCUMENT_ROOT'])+1));
}


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

define('base_url',$url);

echo base_url;exit;

或者,如果您想在php中使用它而不是codeigniter,请尝试php的$\u服务器变量

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

这是codeigniter的工作方式,直接从git存储库:

CodeIgniter/application/config/config.php

/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|   http://example.com/
|
| If this is not set then CodeIgniter will guess the protocol, domain and
| path to your installation.
|
*/
$config['base_url'] = '';
/**
 * Class constructor
 *
 * Sets the $config data from the primary config.php file as a class variable.
 *
 * @return  void
 */
public function __construct()
{
    $this->config =& get_config();
    log_message('debug', 'Config Class Initialized');

    // Set the base_url automatically if none was provided
    if (empty($this->config['base_url']))
    {
        if (isset($_SERVER['HTTP_HOST']))
        {
            $base_url = (is_https() ? 'https' : 'http')
                .'://'.$_SERVER['HTTP_HOST']
                .substr($_SERVER['SCRIPT_NAME'], 0, -strlen(basename($_SERVER['SCRIPT_NAME'])));
        }
        else
        {
            $base_url = 'http://localhost/';
        }

        $this->set_item('base_url', $base_url);
    }
}
CodeIgniter/system/core/Config.php

/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|   http://example.com/
|
| If this is not set then CodeIgniter will guess the protocol, domain and
| path to your installation.
|
*/
$config['base_url'] = '';
/**
 * Class constructor
 *
 * Sets the $config data from the primary config.php file as a class variable.
 *
 * @return  void
 */
public function __construct()
{
    $this->config =& get_config();
    log_message('debug', 'Config Class Initialized');

    // Set the base_url automatically if none was provided
    if (empty($this->config['base_url']))
    {
        if (isset($_SERVER['HTTP_HOST']))
        {
            $base_url = (is_https() ? 'https' : 'http')
                .'://'.$_SERVER['HTTP_HOST']
                .substr($_SERVER['SCRIPT_NAME'], 0, -strlen(basename($_SERVER['SCRIPT_NAME'])));
        }
        else
        {
            $base_url = 'http://localhost/';
        }

        $this->set_item('base_url', $base_url);
    }
}
您应该在配置文件中提供一个基本url,我认为这将由codeigniter自动加载,
base\u url()
将返回此值


每当config.php被实例化时,它都会检查是否在config.php中配置了基本url,如果没有,它会通过检查主机、脚本的协议基本名称等来尝试猜测。然后调用set_item方法,该方法将设置$config['base_url']无论上面的逻辑确定为基础url是什么,它如何可移植?如果我把它放在其他地方,地址会是其他的…如果地址已经改变,你必须改变config.php文件中的URL,因为你在每个页面上都使用常量
base\u URL
,所以如果你改变config.php文件中的URL,它将影响到我所知道的每个页面。。。但是如果没有这个呢?就像codeigniter自动猜测一样codeigniter自动猜测?在codeigniter中,您还必须在配置文件中设置一个基本url,然后只需将substr保持在$_SERVER['SERVER\u NAME']与$_SERVER['REQUEST\u URI']或$url=str\u replace($_SERVER['REQUEST\u URI'],“”,$_SERVER['SERVER\u NAME'])之间;你就完了。如果你认为这不是一个好的解决方案,我想进一步解释一下,如果你有时间来理解为什么。ThanksI已经提供了脚本并解释了codeigniter使用的代码,因此您可以自己实现它。