在includes/init.php目录下
因为工作原因,需要对ecshop二次开发,顺便记录一下对ecshop源代码的一些分析:
首先是init.php文件,这个文件在ecshop每个页面都会 调用到,习惯就先分析它:
php /** * ecshop 前台公用文件 *///防止非法调用 defined-判断常量是否已定义,如果没返回falseif (!defined('in_ecs')){ die('hacking attempt');//die-直接终止程序并输出} //报告所有错误error_reporting(e_all); //如果获取不到本文件if (__file__ == ''){ die('fatal error code: 0');} /*预定义常量__line__ 文件中的当前行号。__file__ 文件的完整路径和文件名。__function__ 函数名称(这是 php 4.3.0 新加的)。__class__ 类的名称(这是 php 4.3.0 新加的)。__method__ 类的方法名(这是 php 5.0.0 新加的)。 *//* 取得当前商城所在的根目录 */define('root_path', str_replace('includes/init.php', '', str_replace('\\', '/', __file__))); //检测是否已安装if (!file_exists(root_path . 'data/install.lock') && !file_exists(root_path . 'includes/install.lock') && !defined('no_check_install')){ header(location: ./install/index.php\n); exit;} /* 初始化设置 */@ini_set('memory_limit', '64m');//ini_set设置php.ini中的设置,memory_limit设定一个脚本所能够申请到的最大内存字节数@ini_set('session.cache_expire', 180);//指定会话页面在客户端cache中的有效期限(分钟),单位为分钟。@ini_set('session.use_trans_sid', 0);//关闭自动把session id嵌入到web的url中@ini_set('session.use_cookies', 1);//允许使用cookie在客户端保存会话id@ini_set('session.auto_start', 0);//在客户访问任何页面时都自动初始化会话,0-禁止@ini_set('display_errors', 1);//是否显示错误if (directory_separator == '\\')//如果装在windows上(directory_separator路径分隔符,linux上就是’/’ windows上是’\’){ @ini_set('include_path', '.;' . root_path);//include目录为当前目录和网站根目录,windows下用';'分隔} else{ @ini_set('include_path', '.:' . root_path);//include目录为当前目录和网站根目录,linux下用':'分隔} require(root_path . 'data/config.php');//包含配置文件(数据库相关)if (defined('debug_mode') == false)//如果常量debug_mode没有定义则定义为0,debug_mode用于设置ecshp的使用模式{ define('debug_mode', 0);} //设定用于所有日期时间函数的默认时区if (php_version >= '5.1' && !empty($timezone)){ date_default_timezone_set($timezone);//date_default_timezone_set 设置时区} //$_server['php_self']返回当前页面,获取$_server['php_self']最好用htmlspecialchars过滤一下,存在xss漏洞$php_self = isset($_server['php_self']) ? $_server['php_self'] : $_server['script_name']; if ('/' == substr($php_self, -1))//如果是/结尾,则加上index.php{ $php_self .= 'index.php';} define('php_self', $php_self);//放入常量require(root_path . 'includes/inc_constant.php');//包含预定义常量文件require(root_path . 'includes/cls_ecshop.php');//基础类 文件require(root_path . 'includes/cls_error.php');//错误类 文件require(root_path . 'includes/lib_time.php');//时间函数require(root_path . 'includes/lib_base.php');//基础函数库require(root_path . 'includes/lib_common.php');//基础函数库require(root_path . 'includes/lib_main.php');//公用函数库require(root_path . 'includes/lib_insert.php');//动态内容函数库require(root_path . 'includes/lib_goods.php');//商品相关函数库require(root_path . 'includes/lib_article.php');//文章及文章分类相关函数库/* 对用户传入的变量进行转义操作。*/if (!get_magic_quotes_gpc()){ if (!empty($_get)) { $_get = addslashes_deep($_get); } if (!empty($_post)) { $_post = addslashes_deep($_post); } $_cookie = addslashes_deep($_cookie); $_request = addslashes_deep($_request);} /* 创建 ecshop 对象 */$ecs = new ecs($db_name, $prefix);//参数说明:数据库名 表前缀define('data_dir', $ecs->data_dir());//数据目录define('image_dir', $ecs->image_dir());//图片目录/* 初始化数据库类 */require(root_path . 'includes/cls_mysql.php'); $db = new cls_mysql($db_host, $db_user, $db_pass, $db_name); /* 设置不允许进行缓存的表 */$db->set_disable_cache_tables(array($ecs->table('sessions'), $ecs->table('sessions_data'), $ecs->table('cart'))); $db_host = $db_user = $db_pass = $db_name = null; /* 创建错误处理对象 */$err = new ecs_error('message.dwt'); /* 载入系统参数 */$_cfg = load_config(); //载入配置信息函数在lib_common.php/* 载入语言文件 */require(root_path . 'languages/' . $_cfg['lang'] . '/common.php'); if ($_cfg['shop_closed'] == 1){ /* 商店关闭了,输出关闭的消息 */header('content-type: text/html; charset='.ec_charset); die('' . $_lang['shop_closed'] . '
' . $_cfg['close_comment'] . '
');} //判断是否为搜索引擎蜘蛛 函数在lib_main.phpif (is_spider()){ /* 如果是蜘蛛的访问,那么默认为访客方式,并且不记录到日志中 */if (!defined('init_no_users')) { define('init_no_users', true); /* 整合uc后,如果是蜘蛛访问,初始化uc需要的常量 */if($_cfg['integrate_code'] == 'ucenter') { $user = & init_users(); } } $_session = array(); $_session['user_id'] = 0; $_session['user_name'] = ''; $_session['email'] = ''; $_session['user_rank'] = 0; $_session['discount'] = 1.00;} //非搜索引擎蜘蛛,记录sessionif (!defined('init_no_users')){ /* 初始化session */include(root_path . 'includes/cls_session.php'); $sess = new cls_session($db, $ecs->table('sessions'), $ecs->table('sessions_data')); define('sess_id', $sess->get_session_id());} //如果使用smartyif (!defined('init_no_smarty')){ header('cache-control: private'); header('content-type: text/html; charset='.ec_charset); /* 创建 smarty 对象。*/require(root_path . 'includes/cls_template.php'); $smarty = new cls_template; $smarty->cache_lifetime = $_cfg['cache_time'];//缓存时间$smarty->template_dir = root_path . 'themes/' . $_cfg['template'];//模板所在$smarty->cache_dir = root_path . 'temp/caches';//缓存所在$smarty->compile_dir = root_path . 'temp/compiled';//模板编译后的文件所在if ((debug_mode & 2) == 2)//如果常量debug_mode值为 2、3、6、7.时 { $smarty->direct_output = true; //不使用缓存直接输出$smarty->force_compile = true; //强行编译 } else { $smarty->direct_output = false; $smarty->force_compile = false; } $smarty->assign('lang', $_lang); $smarty->assign('ecs_charset', ec_charset); if (!empty($_cfg['stylename']))//如果自己定义样式文件就用自己的 { $smarty->assign('ecs_css_path', 'themes/' . $_cfg['template'] . '/style_' . $_cfg['stylename'] . '.css'); } else { $smarty->assign('ecs_css_path', 'themes/' . $_cfg['template'] . '/style.css'); }} //非搜索引擎爬虫,记录用户信息if (!defined('init_no_users')){ /* 会员信息 初始化会员数据 lib_common.php */$user =& init_users(); if (!isset($_session['user_id'])) { /* 获取投放站点的名称 */$site_name = isset($_get['from']) ? $_get['from'] : addslashes($_lang['self_site']); $from_ad = !empty($_get['ad_id']) ? intval($_get['ad_id']) : 0; $_session['from_ad'] = $from_ad; // 用户点击的广告id$_session['referer'] = stripslashes($site_name); // 用户来源unset($site_name); if (!defined('ingore_visit_stats')) { visit_stats(); } } if (empty($_session['user_id'])) { if ($user->get_cookie()) { /* 如果会员已经登录并且还没有获得会员的帐户余额、积分以及优惠券 */if ($_session['user_id'] > 0) { update_user_info(); } } else { $_session['user_id'] = 0; $_session['user_name'] = ''; $_session['email'] = ''; $_session['user_rank'] = 0; $_session['discount'] = 1.00; if (!isset($_session['login_fail'])) { $_session['login_fail'] = 0; } } } /* 设置推荐会员 */if (isset($_get['u'])) { set_affiliate(); } if (isset($smarty)) { $smarty->assign('ecs_session', $_session); }} if ((debug_mode & 1) == 1)//如果常量debug_mode值为 1、3、5、7.时{ error_reporting(e_all);//报告全部错误} else{ error_reporting(e_all ^ e_notice); //报告除e_notice以外的所有错误} if ((debug_mode & 4) == 4)//如果常量debug_mode值为 4、5、6、7.时,调试程序{ include(root_path . 'includes/lib.debug.php');// } /* 判断是否支持 gzip 模式 如果使用smarty同时设置了网页压缩,则启用压缩 */if (!defined('init_no_smarty') && gzip_enabled()){ ob_start('ob_gzhandler');//压缩后放入缓冲区} else{ ob_start();//打开缓冲区,把下面要显示的内容先缓在服务器} /*ob_start相关函数了解:1、flush:刷新缓冲区的内容,输出。函数格式:flush()说明:这个函数经常使用,效率很高。2、ob_start :打开输出缓冲区函数格式:void ob_start(void)说明:当缓冲区激活时,所有来自php程序的非文件头信息均不会发送,而是保存在内部缓冲区。为了输出缓冲区的内容,可以使用ob_end_flush()或flush()输出缓冲区的内容。、ob_get_contents :返回内部缓冲区的内容。使用方法:string ob_get_contents(void)说明:这个函数会返回当前缓冲区中的内容,如果输出缓冲区没有激活,则返回 false 。4、ob_get_length:返回内部缓冲区的长度。使用方法:int ob_get_length(void)说明:这个函数会返回当前缓冲区中的长度;和ob_get_contents一样,如果输出缓冲区没有激活。则返回 false。5、ob_end_flush :发送内部缓冲区的内容到浏览器,并且关闭输出缓冲区。使用方法:void ob_end_flush(void)说明:这个函数发送输出缓冲区的内容(如果有的话)。6、ob_end_clean:删除内部缓冲区的内容,并且关闭内部缓冲区使用方法:void ob_end_clean(void)说明:这个函数不会输出内部缓冲区的内容而是把它删除!7、ob_implicit_flush:打开或关闭绝对刷新使用方法:void ob_implicit_flush ([int flag])说明:使用过perl的人都知道$|=x的意义,这个字符串可以打开/关闭缓冲区,而ob_implicit_flush函数也和那个一样,默认为关闭缓冲区,打开绝对输出后,每个脚本输出都直接发送到浏览器,不再需要调用 flush() */?>
以上就介绍了ecshop中的init.php文件详解,包括了ecshop,init.php文件方面的内容,希望对php教程有兴趣的朋友有所帮助。
