工作需要写一个简单的网站,就学习了下PHP,读了一套小型的论坛网站的代码,这里通过博客记录一下这几天的工作过程。
首先是选择变成语言,虽然我懂Java,可是PHP这类脚本语言的入门不会太久,并且对于web开发,PHP市场占有率貌似达到百分之八十多,有问题也便于解决,所以呢,我们就选择了PHP。关于小型的论坛网站,在github上找了几个,貌似有比carbon小的,但是因为版本问题,没有跑通,我就没有去详细调试,直接选用了比较小的Carbon-Forum。github链接
使用的话,就是解压到安装的apache的目录下,修改权限,然后在浏览器打开localhost/install
网页,按内容填写,就可以进行安装了。主要调用过程是运行了./install/index,php
文件。下面我们看下,代码还是很简单的。
<meta charset="utf-8">
<?php
@set_time_limit(0);
......
//这里是点击提交代码的时候
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$fp = fopen(__DIR__ . '/database.sql', "r") or die("SQL文件无法打开。 The SQL File could not be opened.");
//dobefore
if (isset($_POST["Language"]) && isset($_POST["DBHost"]) && isset($_POST["DBName"]) && isset($_POST["DBUser"]) && isset($_POST["DBPassword"])) {
$Language = $_POST['Language'];
$DBHost = $_POST['DBHost'];
$DBName = $_POST['DBName'];
$DBUser = $_POST['DBUser'];
$DBPassword = $_POST['DBPassword'];
$SearchServer = $_POST['SearchServer'];
$SearchPort = $_POST['SearchPort'];
$EnableMemcache = $_POST['EnableMemcache'];
$MemCachePrefix = $_POST['MemCachePrefix'];
} else {
die("An Unexpected Error Occured!");
}
//$WebsitePath = $_POST['WebsitePath'];
$WebsitePath = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
if (preg_match('/(.*)\/install/i', $WebsitePath, $WebsitePathMatch)) {
$WebsitePath = $WebsitePathMatch[1];
} else {
$WebsitePath = '';
}
//初始化数据库操作类
require('../library/PDO.class.php');
//为了安全性
$DB = new Db($DBHost, 3306, '', $DBUser, $DBPassword);
$DatabaseExist = $DB->single("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = :DBName", array('DBName' => $DBName));
if (empty($DatabaseExist)) {
$DB->query("CREATE DATABASE IF NOT EXISTS " . $DBName . ";");
}
//生成db类,可以操作数据库
$DB = new Db($DBHost, 3306, $DBName, $DBUser, $DBPassword);
//数据库安装
while ($SQL = GetNextSQL()) {
$DB->query($SQL);
}
//这里是一些动态数据,写入数据库
$DB->query("INSERT INTO `" . PREFIX . "config` VALUES ('WebsitePath', '" . $WebsitePath . "')");
$DB->query("INSERT INTO `" . PREFIX . "config` VALUES ('LoadJqueryUrl', '" . $WebsitePath . "/static/js/jquery.js')");
$DB->query("UPDATE `" . PREFIX . "config` SET `ConfigValue`='" . date('Y-m-d') . "' WHERE `ConfigName`='DaysDate'");
$DB->query("UPDATE `" . PREFIX . "config` SET `ConfigValue`='" . $Version . "' WHERE `ConfigName`='Version'");
$DB->CloseConnection();
fclose($fp) or die("Can’t close file");
//写入config文件
$ConfigPointer = fopen(__DIR__ . '/config.tpl', 'r');
$ConfigBuffer = fread($ConfigPointer, filesize(__DIR__ . '/config.tpl'));
$ConfigBuffer = str_replace("{{Language}}", $Language, $ConfigBuffer);
$ConfigBuffer = str_replace("{{DBHost}}", $DBHost, $ConfigBuffer);
$ConfigBuffer = str_replace("{{DBName}}", $DBName, $ConfigBuffer);
$ConfigBuffer = str_replace("{{DBUser}}", $DBUser, $ConfigBuffer);
$ConfigBuffer = str_replace("{{DBPassword}}", $DBPassword, $ConfigBuffer);
$ConfigBuffer = str_replace("{{SearchServer}}", $SearchServer, $ConfigBuffer);
$ConfigBuffer = str_replace("{{SearchPort}}", $SearchPort, $ConfigBuffer);
$ConfigBuffer = str_replace("{{EnableMemcache}}", $EnableMemcache, $ConfigBuffer);
$ConfigBuffer = str_replace("{{MemCachePrefix}}", $MemCachePrefix, $ConfigBuffer);
fclose($ConfigPointer);
$ConfigPHP = fopen("../config.php", "w+");
fwrite($ConfigPHP, $ConfigBuffer);
fclose($ConfigPHP);
//写入htaccess文件
$HtaccessPointer = fopen(__DIR__ . '/htaccess.tpl', 'r');
$HtaccessBuffer = fread($HtaccessPointer, filesize(__DIR__ . '/htaccess.tpl'));
$HtaccessBuffer = str_replace("{{WebSitePath}}", $WebsitePath, $HtaccessBuffer);
//Server Software Type
if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {//IIS(ISAPI_Rewrite)
$HtaccessBuffer = str_replace("{{RedirectionType}}", "[QSA,NU,PT,L]", $HtaccessBuffer);
} else {//Others
$HtaccessBuffer = str_replace("{{RedirectionType}}", "[L]", $HtaccessBuffer);
}
fclose($HtaccessPointer);
$Htaccess = fopen("../.htaccess", "w+");
fwrite($Htaccess, $HtaccessBuffer);
fclose($Htaccess);
//rewrite文件配置
$Message = '安装成功。<br />Installed successfully! <br /><a href="../register">点我马上注册管理员账号<br />The first registered users will become administrators.</a>';
if (!file_exists('install.lock')) {
touch('install.lock');
}
if (!file_exists('../update/update.lock')) {
touch("../update/update.lock");
}
}
......
//从文件中逐条取SQL
function GetNextSQL()
{
global $fp;
$sql = "";
while ($line = fgets($fp, 40960)) {
$line = trim($line);
//以下三句在高版本php中不需要,在部分低版本中也许需要修改
/*
$line = str_replace("","//",$line);
$line = str_replace("/’","’",$line);
$line = str_replace("//r//n",chr(13).chr(10),$line);
*/
//$line = stripcslashes($line);
if (strlen($line) > 1) {
if ($line[0] == "-" && $line[1] == "-") {
continue;
}
}
$sql .= $line . chr(13) . chr(10);
if (strlen($line) > 0) {
if ($line[strlen($line) - 1] == ";") {
break;
}
}
}
return $sql;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cmn-Hans" lang="zh-cmn-Hans">
//这里及时html文件,这里只关注PHP相关部分
</html>
其实这里逻辑非常简单,第一次浏览器打开是通过get方法,PHP进行判断,如果是通过form的post的方法,那么就开始初始化数据库, 如果不是,那么就要显示基本的html,用来注册。我们只要关注那个判断语句即可:
/database.sql
这个文件存储了初始化数据库的所有文件,还需要判断form里面的东西是否初始化完全,,然后是通过dbdao初始化了数据库,生成一个confing.php文件,生成一个锁文件,最终显示安装完成,
本来想着写长一点,但是忽然发现有些简单,浪费时间,就不好了,看看以后如何完成全部。