用户
 找回密码
 入住 CI 中国社区
搜索
查看: 35058|回复: 4
收起左侧

ci源码分析--common.php

[复制链接]
发表于 2009-3-10 12:35:34 | 显示全部楼层 |阅读模式
PHP复制代码

<?php
if ( ! defined('E_STRICT'))
{
 define('E_STRICT', 2048);
}
//检查文件是否可写
function is_really_writable($file)
{
 if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
 {
  return is_writable($file);
 }
 if (is_dir($file))
 {
  $file = rtrim($file, '/').'/'.md5(rand(1,100));
  if (($fp = fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
  {
   return FALSE;
  }
  fclose($fp);
  @chmod($file, DIR_WRITE_MODE);
  @unlink($file);
  return TRUE;
 }
 elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
 {
  return FALSE;
 }
 fclose($fp);
 return TRUE;
}
//载入配置文件
function &get_config()
{
 static $main_conf;
 if ( ! isset($main_conf))
 {
  if ( ! file_exists(APPPATH.'config/config'.EXT))
  {
   exit('配置文件不存在.');
  }
  require(APPPATH.'config/config'.EXT);
  if ( ! isset($config) OR ! is_array($config))
  {
   exit('你的配置文件格式不正确.');
  }
  $main_conf[0] =& $config;
 }
 return $main_conf[0];
}
//获取配置项
function config_item($item)
{
 static $config_item = array();
 if ( ! isset($config_item[$item]))
 {
  $config =& get_config();
  if ( ! isset($config[$item]))
  {
   return FALSE;
  }
  $config_item[$item] = $config[$item];
 }
 return $config_item[$item];
}
//载入类
function &load_class($class, $instantiate = TRUE)
{
 static $objects = array();
 if (isset($objects[$class]))
 {
  return $objects[$class];
 }
 require(BASEPATH.'libraries/'.$class.EXT);
 
 if ($instantiate == FALSE)
 {
  $objects[$class] = TRUE;
  return $objects[$class];
 }
 
 $objects[$class] =& new $class();
 return $objects[$class];
}
//写入日志
function log_message($level = 'error', $message, $php_error = FALSE)
{
 static $LOG;
 
 $config =& get_config();
 if ($config['log_threshold'] == 0)
 {
  return;
 }
 $LOG =& load_class('Log');
 $LOG->write_log($level, $message, $php_error);
}
//自定义错误函数
function _exception_handler($severity, $message, $filepath, $line)
{
 if ($severity == E_STRICT)
 {
  return;
 }
 $error =& load_class('Exceptions');
 if (($severity & error_reporting()) == $severity)
 {
  $error->show_php_error($severity, $message, $filepath, $line);
 }
 $config =& get_config();
 if ($config['log_threshold'] == 0)
 {
  return;
 }
 $error->log_exception($severity, $message, $filepath, $line);
}
//显示错误
function show_error($message)
{
 $error =& load_class('Exceptions');
 echo $error->show_error('系统出现错误:', $message);
 exit;
}
//无法显示网页
function show_404($page = '')
{
 $error =& load_class('Exceptions');
 $error->show_404($page);
 exit;
}
?>
复制代码
 楼主| 发表于 2009-3-10 12:38:28 | 显示全部楼层
common.php保存了一些通用的函数,最好对这几个函数比较熟悉,很多地方会用到的
发表于 2010-2-25 17:17:37 | 显示全部楼层
分析在哪里?

点评

注释就是分析了  发表于 2015-1-22 14:19
发表于 2012-3-1 22:01:53 | 显示全部楼层
这几个函数灰常有用

本版积分规则