实现php后台安装数据库等一些操作,代码简单的写的。很多东西还不完善,请大家多多支持,意见可以写在评论中。
install.php
---------------------------------------------------------------------------nbsp;html/><br>
<br>
<br>
<style><br />
*{margin:0;padding:0;}<br />
h2{margin:10px;background:#000000;width:100%;height:50px;line-height:50px;color:#fff;}<br />
.content{font-size:16px;border:2px solid #ccc;font-weight:bold;width:800px;height:500px;overflow:scroll;padding:10px;margin:0 auto;}<br />
h3{margin:10px;background:#CC0033;width:100%;height:50px;line-height:50px;color:#fff;}<br />
a{text-decoration: none;}<br />
a:hover{text-decoration: underline; }<br />
table tr td{font-weight: bold;font-size:20px;padding-top:20px;}<br />
table tr .input{width: 200px;height: 30px;}<br />
table tr td .bt{width: 80px;height: 30px;border: 1px solid #ccc;}<br />
</style>
<br>
<br>
<center><h2>系统安装界面</h2></center>
<br>
<center>
<br>
<?php <br />
<br>
/******************************************************<br>
* 系统安装界面 *<br>
* 所有者:allens stevon *<br>
* 日期:2015/5/6 *<br>
******************************************************/<br>
<br>
/**<br>
*@系统安装类 <br>
*@生成数据库,生成初始化数据<br>
*/<br>
header("Content-type: text/html; charset=utf-8"); <br>
$reg = false;<br>
<br>
class Install<br>
{<br>
<br>
private $con; //数据库连接<br>
private $dbhost = 'localhost'; //数据库端口<br>
private $dbusername = 'root'; //数据库名称<br>
private $dbpw = ''; //数据库密码<br>
public $db = 'cute'; //db名称<br>
<br>
//构造方法<br>
function __construct()<br>
{<br>
//判断数据库是否连接成功<br>
if (!($this->con = @mysql_connect($this->dbhost,$this->dbusername,$this->dbpw)))<br>
die('Could not connect: ' . mysql_error());<br>
else{<br>
echo "连接成功<br>";<br>
$this->createdb();<br>
}<br>
}<br>
<br>
//销毁函数<br>
function __destruct(){<br>
<br>
//判断是否连接上数据库<br>
if($this->con){<br>
//在类结束的时候关闭数据库连接<br>
if(!$close = @mysql_close($this -> con))<br>
die('<br>Could not close db');<br>
}<br>
}<br>
<br>
//生成数据库<br>
private function createdb(){<br>
<br>
$i = 0;<br>
//数据库名称<br>
$queryinfo = array(<br>
0 => '创建'.$this->db,<br>
1 => '使用'.$this->db,<br>
2 => '创建'.$this->db . '_user',<br>
3 => '创建访问权限表'.$this->db.'_access',<br>
4 => '创建权限表' . $this->db . "_node",<br>
5 => '创建权限表' . $this->db . "_role",<br>
6 => '创建'.$this->db.'_role_user',<br>
7 => '创建栏目表'. $this->db .'_caid',<br>
);<br>
<br>
$filename = "data/testdata.sql";<br>
$sql=file_get_contents($filename); //把SQL语句以字符串读入$sql <br>
$sql = str_replace('premax', $this->db, $sql);<br>
<br>
file_put_contents($filename, $sql);<br>
<br>
//设置分隔符<br>
$a=explode("-- **",$sql); //用explode()函数把$sql字符串以“;”分割为数组 <br>
<br>
foreach($a as $b){ //遍历数组 <br>
<br>
$c=$b; //分割后是没有“;”的,因为SQL语句以“;”结束,所以在执行SQL前把它加上 <br>
<br>
if(@mysql_query($c)){ //执行SQL语句 <br>
echo $queryinfo[$i++]."操作成功..<br>";<br>
} else<br>
echo $queryinfo[$i++].'操作失败..' . mysql_error() . "<br>";<br>
<br>
}<br>
echo "<h3>完成所有操作</h3>";<br>
echo "<p><a>进入后台</a><a>进入前台</a></p>";<br>
<br>
} <br>
<br>
<br>
<br>
}<br>
if(array_key_exists('supermanager', $_POST) && array_key_exists('superpw', $_POST)){ <br>
if(!$_POST['supermanager'] == "" && !$_POST['superpw'] == ''){<br>
$reg = true;<br>
$install = new Install();<br>
mysql_select_db($install->db);<br>
$sql = 'insert into ' . $install->db . "_user(username,password)" . " values('" . $_POST["supermanager"] . "','".md5($_POST['superpw'])."')";<br>
<br>
if($result = mysql_query($sql))<br>
echo "创建超级管理员成功";<br>
else<br>
echo "创建超级管理员失败";<br>
} else<br>
echo "请输入帐号密码再提交";<br>
}<br>
?><br>
<br>
<?php <br />
if(!$reg){<br>
?><br>
<h3>注册超级管理用户</h3>
<br>
<form>
<br>
<table>
<br>
<tr>
<br>
<td>超级管理员:</td>
<td><input></td>
<br>
</tr>
<br>
<tr>
<br>
<td>管理员密码:</td>
<td><input></td>
<br>
</tr>
<br>
<tr>
<br>
<td></td>
<td>
<br>
<input><br>
<input><br>
</td>
<br>
</tr>
<br>
</table>
<br>
</form>
<br>
<?php <br />
}<br>
?><br>
</center>
<br>
---------------------------------------------------------------------------
testdata.sql-- 创建数据库<br>
CREATE DATABASE IF NOT EXISTS cute;<br>
-- **<br>
-- 使用此数据库<br>
use cute;<br>
-- **<br>
-- 创建User表<br>
CREATE TABLE IF NOT EXISTS cute_user (<br>
`id` smallint(10) unsigned NOT NULL AUTO_INCREMENT,<br>
`username` varchar(30) NOT NULL,<br>
`headpic` varchar(100) DEFAULT null,<br>
`password` varchar(30) NOT NULL,<br>
`status` tinyint(10) NOT NULL DEFAULT 1,<br>
`remark` varchar(255) NOT NULL,<br>
PRIMARY KEY (`id`)<br>
) ENGINE=MyISAM DEFAULT CHARSET=utf8;<br>
-- **<br>
-- 创建访问权限表<br>
CREATE TABLE IF NOT EXISTS `cute_access` (<br>
`role_id` smallint(6) unsigned NOT NULL,<br>
`node_id` smallint(6) unsigned NOT NULL,<br>
`level` tinyint(1) NOT NULL,<br>
`module` varchar(50) DEFAULT NULL,<br>
KEY `groupId` (`role_id`),<br>
KEY `nodeId` (`node_id`)<br>
) ENGINE=MyISAM DEFAULT CHARSET=utf8;<br>
-- **<br>
CREATE TABLE IF NOT EXISTS `cute_node` (<br>
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,<br>
`name` varchar(20) NOT NULL,<br>
`title` varchar(50) DEFAULT NULL,<br>
`status` tinyint(1) DEFAULT '0',<br>
`remark` varchar(255) DEFAULT NULL,<br>
`sort` smallint(6) unsigned DEFAULT NULL,<br>
`pid` smallint(6) unsigned NOT NULL,<br>
`level` tinyint(1) unsigned NOT NULL,<br>
PRIMARY KEY (`id`),<br>
KEY `level` (`level`),<br>
KEY `pid` (`pid`),<br>
KEY `status` (`status`),<br>
KEY `name` (`name`)<br>
) ENGINE=MyISAM DEFAULT CHARSET=utf8;<br>
-- **<br>
CREATE TABLE IF NOT EXISTS `cute_role` (<br>
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,<br>
`name` varchar(20) NOT NULL,<br>
`pid` smallint(6) DEFAULT NULL,<br>
`status` tinyint(1) unsigned DEFAULT NULL,<br>
`remark` varchar(255) DEFAULT NULL,<br>
PRIMARY KEY (`id`),<br>
KEY `pid` (`pid`),<br>
KEY `status` (`status`)<br>
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;<br>
-- **<br>
CREATE TABLE IF NOT EXISTS `cute_role_user` (<br>
`role_id` mediumint(9) unsigned DEFAULT NULL,<br>
`user_id` char(32) DEFAULT NULL,<br>
KEY `group_id` (`role_id`),<br>
KEY `user_id` (`user_id`)<br>
) ENGINE=MyISAM DEFAULT CHARSET=utf8;<br>
-- **<br>
-- 后台主体栏目表 --<br>
CREATE TABLE IF NOT EXISTS `cute_caid` (<br>
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,<br>
`name` varchar(20) NOT NULL,<br>
`title` varchar(50) DEFAULT NULL,<br>
`status` tinyint(1) DEFAULT '0',<br>
`remark` varchar(255) DEFAULT NULL,<br>
`sort` smallint(6) unsigned DEFAULT NULL,<br>
`href` varchar(150) not null,<br>
`pid` smallint(6) unsigned NOT NULL,<br>
`level` tinyint(1) unsigned NOT NULL,<br>
PRIMARY KEY (`id`),<br>
KEY `level` (`level`),<br>
KEY `pid` (`pid`),<br>
KEY `status` (`status`),<br>
KEY `name` (`name`)<br>
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
下载代码在下方
Install.rar ( 3.4 KB 下载:32 次 )
AD:真正免费,域名+虚机+企业邮箱=0元