验证php加载application/config文件夹下面的配置文件一个config.php和config2.php

说明:在CiTest文件夹下面建立测试页面,比如:test.php提制代码进去,然后再建立appliction/config文件夹,在config文件夹下建config.php和 config2.php,以$config[‘aaa’]='aaa’数组形式建立配置项

define('APPPATH','application/');

class CI_Config{
	public $config=array();
	public $is_loaded=array();
	public $_config_paths=array(APPPATH);
	public function __construct(){
		$this->config=&get_config();
		if(empty($this->config['base_url'])){
			if(isset($_SERVER['SERVER_ADDR'])){
				if(strpos($_SERVER['SERVER_ADDR'],':')!==false){
					$server_addr='['.$_SERVER['SERVER_ADDR'].']';
				}else{
					$server_addr=$_SERVER['SERVER_ADDR'];
				}
				$base_url=(is_https()?'https':'http').'//'.$server_addr.substr($_SERVER['SCRIPT_NAME'],0,strpos($_SERVER['SCRIPT_NAME'],basename($_SERVER['SCRIPT_FILENAME'])));
			}else{
				$base_url='http://localhost/';
			}
			$this->set_item('base_url',$base_url);
		}
		//log_message('info','建立配置文件');
	}
	public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
	{
		$file = ($file === '') ? 'config' : str_replace('.php', '', $file);
		$loaded = FALSE;

		foreach ($this->_config_paths as $path)
		{
			foreach (array($file, DIRECTORY_SEPARATOR.$file) as $location)
			{
				$file_path = $path.'config/'.$location.'.php';
				if (in_array($file_path, $this->is_loaded, TRUE))
				{
					return TRUE;
				}

				if ( ! file_exists($file_path))
				{
					continue;
				}

				include($file_path);

				if ( ! isset($config) OR ! is_array($config))
				{
					if ($fail_gracefully === TRUE)
					{
						return FALSE;
					}

					//show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
				}

				if ($use_sections === TRUE)//以这个文件为索引,然后加载配置到这个索引下面的数组中
				{
					$this->config[$file] = isset($this->config[$file])
						? array_merge($this->config[$file], $config)
						: $config;
				}
				else
				{
					$this->config = array_merge($this->config, $config);
				}

				$this->is_loaded[] = $file_path;
				$config = NULL;
				$loaded = TRUE;
				//log_message('info', 'Config file loaded: '.$file_path);
			}
		}
	
		if ($loaded === TRUE)
		{
			return TRUE;
		}
		elseif ($fail_gracefully === TRUE)
		{
			return FALSE;
		}
	}
	public function set_item($item,$value){
			$this->config[$item]=$value;
	}
	public function item($item,$index=''){
		if($index==''){
			return isset($this->config[$item])?$this->config[$item]:null;
		}
		
		return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : NULL;
	}
}

if(!function_exists('get_config')){
	/*加载主要配置文件config.php
	*这个函数让我们抓取配置文件,即使config类没有实例化
	*@param array
	*@return array
	*/
	function &get_config(Array $replace=array()){
		static $config;
		if(empty($config)){
			$file_path=APPPATH.'config/config.php';
			$found=false;
			if(file_exists($file_path)){
				$found=true;
				require($file_path);
			}
			
			
			//配置文件是否在$config的数组中
			if(!isset($config) or ! is_array($config)){
				//set_status_header(503);
				echo '你的配置文件格式不正确';
				exit(3);//exit_config
			}
		}
		foreach($replace as $key=>$val){
			$config[$key]=$val;
		}
		return $config;
	}
}
if(!function_exists('is_https')){
	/*
	*确定应用是否通过加密访问(https)连接
	*@return bool
	*/
	function is_https(){
		if(!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !=='off'){
			return true;
		}elseif(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO'])==='https'){
			return true;
		}elseif(!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS'])!=='off'){
			return true;
		}
		return false;
	}
}
$config=new CI_Config();
print_r($config);
$index=$config->item('index_page');//直接输出index_page这个配置项
echo $index;
$config->load('config2',true);//以config2为索引,然后将配置项加载到这个索引下面,true就是表示加载到索引下面
$url=$config->item('ccc','config2');//查找config2这个索引文件里面的ccc这个配置项
echo $url;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值