网站公共文件 /common/common.php
在 PHP 文件的顶部,使用 include
方式加载 common/common.php
文件,即可使用配置文件中定义的常量和函数库中的所有函数,并且使其校验过程在当前页面中生效。
网站公共文件的作用:
设置 PHP的错误级别、网页字符集;
用于校验站点是否已经安装;
加载 config 中的所有文件、及 helpers 中的所有文件;
初始化SESSION和网站的公共变量;
网站的开启或关闭状态;
校验禁止访问的IP地址;
获取主导航的相关数据;
公共文件包含关系
代码详解
<?php
error_reporting(E_ALL & ~E_NOTICE);
header("content-type:text/html; charset=utf-8;");
//项目安装
if(!file_exists("install.lock"))
{
header("location:install/index.php");
exit;
}
/**
* 加载文件
*/
include "config/database.php"; //加载数据库配置文件
include "config/config.php"; //加载站点基础配置文件
include "helpers/mytpl.php"; //加载模板函数文件
include "helpers/mysql.php"; //加载数据库操作函数文件
include "helpers/user.php"; //加载数据库操作函数文件
include "helpers/func.php"; //加载公共函数文件
include "helpers/img.php"; //加载图片处理函数文件
include "helpers/fileupload.php"; //加载文件上传函数文件
include "helpers/code.php"; //加载图片验证码函数文件
include "helpers/page.php"; //加载分页函数文件
$keyWords = include "helpers/keywords.php"; //加载热搜关键词文件
session_start(); //启动 SESSION
//初始化变量
$web_name = WEB_NAME;
$web_btm = WEB_BTM;
$web_url = WEB_URL;
$web_icp = WEB_ICP;
$web_close = WEB_ISCLOSE;
$web_reg = WEB_REG;
$domain_resource = DOMAIN_RESOURCE;
//获取当前页面地址
$fileName = explode("/",$_SERVER["PHP_SELF"]);
$thispage = array_pop($fileName);
//网站关闭,管理员可以登录
if(empty($_POST["loginsubmit"]))
{
if($web_close && !$_COOKIE["udertype"] && $thispage!="logout.php")
{
echo $web_close."---".$_COOKIE["udertype"]."---".$thispage;
include "close.php";
exit;
}
}
//禁止IP
$Uip = ip2long($_SERVER["REMOTE_ADDR"]);
$ip = dbSelect("closeip","*","ip=".$Uip."","id desc",1);
if ($ip)
{
exit("本站不欢迎你,请离开!");
}
if ($_COOKIE["uid"])
{
$GGUser = dbSelect("user", "grade,picture", "uid=" . $_COOKIE["uid"], "", 1);
if ($GGUser)
{
$GGgrade = $GGUser[0]["grade"];
$GGpicture = $GGUser[0]["picture"];
}
}
//读取所有大版块信息,做导航
$headMenu = dbSelect("category","cid,classname","parentid=0 and ispass=1","orderby desc,cid desc");
基础配置 /config/config.php
包含以下配置项:
1 基础配置(时区|魔术转译)
2 静态资源
3 website
4 模版引擎
5 奖励
<?php
// 一
define("TIMEZONE", "Asia/shanghai"); //时区
define("GPC", get_magic_quotes_gpc() ? 0 : 1); //魔术转译
// 二
define("DOMAIN_RESOURCE", "http://funcbbs.yhsong.com/public"); //静态资源目录
// 三
define("WEB_NAME", "PHP学院"); //站点名称,将显示在浏览器窗口标题等位置
define("WEB_BTM", "NoAlike"); //网站名称,将显示在页面底部的联系方式处
define("WEB_URL", "http://www.phpxy.com/"); //网站 URL,将作为链接显示在页面底部
define("WEB_ICP", "京ICP备 89273号"); //页面底部可以显示 ICP 备案信息
define("WEB_ISCLOSE", false); //true暂时将站点关闭,其他人无法访问,但不影响管理员访问
define("WEB_REG", true); //true开启注册功能
// 四
define("TPL_SKIN", "theme/default"); //模版文件存放位置
define("TPL_CACHE", "compiled"); //模版文件缓存位置
// 五
define("REWARD_LOGIN", 2); //每天首次登陆赠送2积分
define("REWARD_REG", 50); //注册赠送50积分
define("REWARD_T", 2); //发帖赠送2积分
define("REWARD_H", 1); //回帖赠送1积分
数据库连接配置 /config/database.php
网站安装过程中会重写该文件 (确保该文件具有可写权限) 的内容,无需手动修改配置。
<?php
define("DB_HOST","localhost"); //数据库连接地址(IP)
define("DB_USER","root"); //数据库登陆账号
define("DB_PASS","123456"); //数据库登陆账号对应的密码
define("DB_NAME","apple_bbs"); //数据库名称
define("DB_CHARSET","utf8"); //数据库字符集
define("DB_PREFIX","bbs_"); //数据表前缀