PHP网站安装程序的原理及代码php
原理:
其实PHP程序的安装原理无非就是将数据库结构和内容导入到相应的数据库中,从这个过程中重新配置连接数据库的参数和文件,为了保证不被别人恶意使用安装文件,当安装完成后需要修改安装文件。
步骤:
1、...
原理:
其实PHP程序的安装原理无非就是将数据库结构和内容导入到相应的数据库中,从这个过程中重新配置连接数据库的参数和文件,为了保证不被别人恶意使用安装文件,当安装完成后需要修改安装文件。
步骤:
1、检查目录或文件的权限
2、修改或填加配置文件
3、检查配置文件正确性
4、导入数据库
5、锁定或删除安装文件
具体代码:
文件:由于只是展示原理,尽量让其简单化故用小Demo形式演示
install.html 为表单填写文件
doAction.php 为处理表单文件
dbconfig.php 数据库配置文件
install.html
<!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="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>安装程序</title>
</head>
<body>
<center>
<h2>PHP在线安装程序</h2>
<hr/>
<form action="doAction.php" method="post">
<table>
<tr>
<td>主机地址:</td>
<td><input type="text" name="host"/></td>
</tr>
<tr>
<td>数据库账号:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>数据库密码:</td>
<td><input type="password" name="password"/></td>
</tr>
<td>数据库名:</td>
<td><input type="text" name="dbname"/></td>
</tr>
<tr>
<td>数据库表前缀:</td>
<td><input type="text" name="flag"/></td>
</tr>
<tr>
<td colspan="2" style="text-align:center;">
<input type="submit" value="安装"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
doAction.php
<?php
$filename="dbconfig.php";
//配置文件内容
$config='<?php';
$config.="\n";
$config.='$host="'.$_POST["host"].'";';
$config.="\n";
$config.='$user="'.$_POST["username"].'";';
$config.="\n";
$config.='$pass="'.$_POST["password"].'";';
$config.="\n";
$config.='$dbname="'.$_POST["dbname"].'";';
$config.="\n";
$config.='$flag="'.$_POST["flag"].'";';
$config.="\n";
$config.="?>";
if(is_writable($filename)){//检测是否有权限可写
$handle=fopen($filename, "w+");
fwrite($handle, $config);
//连接数据库
include_once($filename);
if(!@$link=mysql_connect($host,$user,$pass)){
echo "数据库连接失败,<a href='install.php'>返回设置</a>";
}else{
mysql_query("create database if not exists `$dbname`");
mysql_select_db($dbname,$link);
//建表语句
$sql[]="CREATE TABLE IF NOT EXISTS `".$flag."access` (
`role_id` smallint(6) unsigned NOT NULL,
`node_id` smallint(6) unsigned NOT NULL,
`level` tinyint(1) NOT NULL,
`module` varchar(50) DEFAULT NULL,
KEY `groupId` (`role_id`),
KEY `nodeId` (`node_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$sql[]="CREATE TABLE IF NOT EXISTS `".$flag."node` (
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`title` varchar(50) DEFAULT NULL,
`status` tinyint(1) DEFAULT '0',
`remark` varchar(255) DEFAULT NULL,
`sort` smallint(6) unsigned DEFAULT NULL,
`pid` smallint(6) unsigned NOT NULL,
`level` tinyint(1) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `level` (`level`),
KEY `pid` (`pid`),
KEY `status` (`status`),
KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$sql[]="CREATE TABLE IF NOT EXISTS `".$flag."role` (
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`pid` smallint(6) DEFAULT NULL,
`status` tinyint(1) unsigned DEFAULT NULL,
`remark` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `pid` (`pid`),
KEY `status` (`status`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$sql[]="CREATE TABLE IF NOT EXISTS `".$flag."role_user` (
`role_id` mediumint(9) unsigned DEFAULT NULL,
`user_id` char(32) DEFAULT NULL,
KEY `group_id` (`role_id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
foreach ($sql as $value) {//由于mysql_query不支持一次性执行多条语句,所以用for循环遍历
mysql_query($value);
}
echo "<script>window.location='index.php';</script>";
rename("install.html", "install.lock");
}
}else{
echo "您没有权限操作。";
}
?>
dbconfig.php
<?php
$host="localhost";
$user="root";
$pass="";
$dbname="demo";
$flag="lcw_";
?>
index.php
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>首页</title>
</head>
<body>
<h2>^_^ 数据导入成功。</h2>
</body>
</html>
执行完安装文件(自动修改文件名):
数据库导入成功!
其实PHP程序的安装原理无非就是将数据库结构和内容导入到相应的数据库中,从这个过程中重新配置连接数据库的参数和文件,为了保证不被别人恶意使用安装文件,当安装完成后需要修改安装文件。
步骤:
1、检查目录或文件的权限
2、修改或填加配置文件
3、检查配置文件正确性
4、导入数据库
5、锁定或删除安装文件
具体代码:
文件:由于只是展示原理,尽量让其简单化故用小Demo形式演示
install.html 为表单填写文件
doAction.php 为处理表单文件
dbconfig.php 数据库配置文件
index.php 执行成功跳转页面
install.html
<!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="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>安装程序</title>
</head>
<body>
<center>
<h2>PHP在线安装程序</h2>
<hr/>
<form action="doAction.php" method="post">
<table>
<tr>
<td>主机地址:</td>
<td><input type="text" name="host"/></td>
</tr>
<tr>
<td>数据库账号:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>数据库密码:</td>
<td><input type="password" name="password"/></td>
</tr>
<td>数据库名:</td>
<td><input type="text" name="dbname"/></td>
</tr>
<tr>
<td>数据库表前缀:</td>
<td><input type="text" name="flag"/></td>
</tr>
<tr>
<td colspan="2" style="text-align:center;">
<input type="submit" value="安装"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
doAction.php
<?php
$filename="dbconfig.php";
//配置文件内容
$config='<?php';
$config.="\n";
$config.='$host="'.$_POST["host"].'";';
$config.="\n";
$config.='$user="'.$_POST["username"].'";';
$config.="\n";
$config.='$pass="'.$_POST["password"].'";';
$config.="\n";
$config.='$dbname="'.$_POST["dbname"].'";';
$config.="\n";
$config.='$flag="'.$_POST["flag"].'";';
$config.="\n";
$config.="?>";
if(is_writable($filename)){//检测是否有权限可写
$handle=fopen($filename, "w+");
fwrite($handle, $config);
//连接数据库
include_once($filename);
if(!@$link=mysql_connect($host,$user,$pass)){
echo "数据库连接失败,<a href='install.php'>返回设置</a>";
}else{
mysql_query("create database if not exists `$dbname`");
mysql_select_db($dbname,$link);
//建表语句
$sql[]="CREATE TABLE IF NOT EXISTS `".$flag."access` (
`role_id` smallint(6) unsigned NOT NULL,
`node_id` smallint(6) unsigned NOT NULL,
`level` tinyint(1) NOT NULL,
`module` varchar(50) DEFAULT NULL,
KEY `groupId` (`role_id`),
KEY `nodeId` (`node_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$sql[]="CREATE TABLE IF NOT EXISTS `".$flag."node` (
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`title` varchar(50) DEFAULT NULL,
`status` tinyint(1) DEFAULT '0',
`remark` varchar(255) DEFAULT NULL,
`sort` smallint(6) unsigned DEFAULT NULL,
`pid` smallint(6) unsigned NOT NULL,
`level` tinyint(1) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `level` (`level`),
KEY `pid` (`pid`),
KEY `status` (`status`),
KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$sql[]="CREATE TABLE IF NOT EXISTS `".$flag."role` (
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`pid` smallint(6) DEFAULT NULL,
`status` tinyint(1) unsigned DEFAULT NULL,
`remark` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `pid` (`pid`),
KEY `status` (`status`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$sql[]="CREATE TABLE IF NOT EXISTS `".$flag."role_user` (
`role_id` mediumint(9) unsigned DEFAULT NULL,
`user_id` char(32) DEFAULT NULL,
KEY `group_id` (`role_id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
foreach ($sql as $value) {//由于mysql_query不支持一次性执行多条语句,所以用for循环遍历
mysql_query($value);
}
echo "<script>window.location='index.php';</script>";
rename("install.html", "install.lock");
}
}else{
echo "您没有权限操作。";
}
?>
dbconfig.php
<?php
$host="localhost";
$user="root";
$pass="";
$dbname="demo";
$flag="lcw_";
?>
index.php
<!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="en"><head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>首页</title>
</head>
<body>
<h2>^_^ 数据导入成功。</h2>
</body>
</html>
执行完安装文件(自动修改文件名):
数据库导入成功!
上一篇:PHP上传压缩包并自解压方法
最新评论
热门推荐
我要评论