PHP网站安装实现程序
发布:smiling 来源: PHP粉丝网 添加日期:2014-08-27 09:14:32 浏览: 评论:0
网站安装过程我们需要几处非常简单,一个是我们要让用户输入用户名密码然后连接数据库之后再把我们以前准备好.sql文件利用php读取并执行,最后简单配置一下站点,这样一个完整的php网站安装过程就完美的搞定了.
这次顺便做了一个install.php才发现难度其实并不大.还是文件写入操作而已.安装其实主要操作的还是数据库里的内容.先来看看文件里怎么写:还是用的Codeigiter,对于使用其他框架或者手写而言,仅思路可参考,用了挺多CI自带的helper或者是library的,代码如下:
- $content = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');n";
- $content .= '$active_group'. "= 'default';n";
- $content .= '$active_record'." = TRUE;n";
- $content .= '$db'."['default']['hostname'] = '".$this->input->post('hostname')."';n";
- $content .= '$db'."['default']['username'] = '".$this->input->post('rootname')."';n";
- $content .= '$db'."['default']['password'] = '".$this->input->post('pass')."';n";
- $content .= '$db'."['default']['database'] = '".$this->input->post('book')."';n";//开源代码phpfensi.com
- $content .= '$db'."['default']['dbdriver'] = 'mysql';n";
- $content .= '$db'."['default']['dbprefix'] = '';n";
- $content .= '$db'."['default']['pconnect'] = TRUE;n";
- $content .= '$db'."['default']['db_debug'] = TRUE;n";
- $content .= '$db'."['default']['cache_on'] = FALSE;n";
- $content .= '$db'."['default']['cachedir'] = '';n";
- $content .= '$db'."['default']['char_set'] = 'utf8';n";
- $content .= '$db'."['default']['dbcollat'] = 'utf8_general_ci';n";
- $content .= '$db'."['default']['swap_pre'] = '';n";
- $content .= '$db'."['default']['autoinit'] = TRUE;n";
- $content .= '$db'."['default']['stricton'] = FALSE;";
在文件里用n来换行,因为里面包括了PHP的代码,这导致了我们只能用双引号避免冲突,否则的话就得用了,感觉工作量更大,针对$db,直接显示必须要用单引号,于是就出现了这个.
写入文件之后,接着我们需要做的是执行一系列安装操作,也就是CREATE TABLE,以及创建一个新用户用于登陆,在model里,我这么写,代码如下:
- function install() {
- if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_category'")) == 0)
- $this->db->query("CREATE TABLE pr_category(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, category VARCHAR(100) NOT NULL UNIQUE, deadline INT NOT NULL)");
- if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_college'")) == 0)
- $this->db->query("CREATE TABLE pr_college(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL UNIQUE)");
- if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_level'")) == 0)
- $this->db->query("CREATE TABLE pr_level(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, level INT NOT NULL, name VARCHAR(20) NOT NULL)");
- if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_rates'")) == 0)
- $this->db->query("CREATE TABLE pr_rates(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, pid INT NOT NULL, ip VARCHAR(40) NOT NULL, category INT NOT NULL)");
- if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_users'")) == 0)
- $this->db->query("CREATE TABLE pr_users(uid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, username VARCHAR(45) NOT NULL UNIQUE, password VARCHAR(50) NOT NULL, level INT NOT NULL )");
- if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_works'")) == 0)
- $this->db->query("CREATE TABLE pr_works(pid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, title VARCHAR(100) NOT NULL, content TEXT, realname VARCHAR(20) NOT NULL, studentnum VARCHAR(20) NOT NULL, college INT NOT NULL, filename TEXT NOT NULL, category INT NOT NULL)");
- $query1 = $this->db->get_where('pr_level',array('level' => 1, 'name' => '普通用户'));
- $query2 = $this->db->get_where('pr_level',array('level' => 5, 'name' => '管理员'));
- $query3 = $this->db->get_where('pr_level',array('level' => 99, 'name' => '超级管理员'));
- if ($query1->num_rows() == 0)
- $this->db->query("INSERT INTO pr_level(level, name) VALUES (1, '普通用户')");
- if ($query2->num_rows() == 0)
- $this->db->query("INSERT INTO pr_level(level, name) VALUES (5, '管理员')");
- if ($query3->num_rows() == 0)
- $this->db->query("INSERT INTO pr_level(level, name) VALUES (99, '超级管理员')");
- $this->username = $this->input->post('username');
- $this->password = $this->input->post('password');
- $this->level = 99;
- $query4 = $this->db->get_where('pr_users',array('username' => $this->input->post('username')));
- if ($query4->num_rows() == 0) {
- $this->db->insert('pr_users', $this);
- return TRUE;
- } else {
- return FALSE;
- }
- }
其实这么写查询量很大效率又低,不过这有效的避免了上一次安装被打断之后重新安装遇到的麻烦,检测是否已经创建了某个表,是否已经有新用户了之类的,代码如下:
mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_category'"))
这句可以查询数据库中是否存在这个表,执行完如果顺利的话,将install.php改个名字,代码如下:
- rename('application/controllers/install.php', 'application/controllers/install.lock');
在其他文件里加入检测install.php是否存在,如果存在的话就跳转到install.php,这样就做好了简单的安装流程了,必须放在model前,否则会提示没有数据库而不会跳转,代码如下:
- if (file_exists('application/controllers/install.php'))
- redirect('install');
至于什么是否存在表之类的,因为CI会check而且优先级也高于我们自己写的错误提示,所以这里就不加了,完整源码(MVC),Controller代码如下:
- <?php
- class Install extends CI_Controller {
- function __construct() {
- parent::__construct();
- $this->load->helper('url');
- $this->load->helper('file');
- }
- function index() {
- $data['title'] = '安装向导';
- $this->load->helper('form');
- $this->load->library('form_validation');
- $this->form_validation->set_rules('hostname', '主机名', 'trim|required|xss_clean');
- $this->form_validation->set_rules('rootname', '数据库用户名', 'trim|required|xss_clean');
- $this->form_validation->set_rules('username', '用户名', 'trim|required|xss_clean');
- $this->form_validation->set_rules('password', '密码', 'trim|required|xss_clean|md5');
- $data['error'] = '';
- if ($this->form_validation->run() == FALSE) {
- $this->load->view('install', $data);
- } else {
- $config['hostname'] = $this->input->post('hostname');
- $config['username'] = $this->input->post('rootname');
- $config['password'] = $this->input->post('pass');
- $config['database'] = $this->input->post('book');
- $config['dbdriver'] = 'mysql';
- $config['dbprefix'] = '';
- $config['pconnect'] = TRUE;
- $config['db_debug'] = TRUE;
- $config['cache_on'] = FALSE;
- $config['cachedir'] = '';
- $config['char_set'] = 'utf8';
- $config['dbcollat'] = 'utf8_general_ci';
- $config['swap_pre'] = '';
- $config['autoinit'] = TRUE;
- $config['stricton'] = FALSE;
- if ($this->load->database($config, TRUE)) {
- $content = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');n";
- $content .= '$active_group'. "= 'default';n";
- $content .= '$active_record'." = TRUE;n";
- $content .= '$db'."['default']['hostname'] = '".$this->input->post('hostname')."';n";
- $content .= '$db'."['default']['username'] = '".$this->input->post('rootname')."';n";
- $content .= '$db'."['default']['password'] = '".$this->input->post('pass')."';n";
- $content .= '$db'."['default']['database'] = '".$this->input->post('book')."';n";
- $content .= '$db'."['default']['dbdriver'] = 'mysql';n";
- $content .= '$db'."['default']['dbprefix'] = '';n";
- $content .= '$db'."['default']['pconnect'] = TRUE;n";
- $content .= '$db'."['default']['db_debug'] = TRUE;n";
- $content .= '$db'."['default']['cache_on'] = FALSE;n";
- $content .= '$db'."['default']['cachedir'] = '';n";
- $content .= '$db'."['default']['char_set'] = 'utf8';n";
- $content .= '$db'."['default']['dbcollat'] = 'utf8_general_ci';n";
- $content .= '$db'."['default']['swap_pre'] = '';n";
- $content .= '$db'."['default']['autoinit'] = TRUE;n";
- $content .= '$db'."['default']['stricton'] = FALSE;";
- if (write_file('application/config/database.php', $content)) {
- $this->load->model('install_model');
- if ($this->install_model->install()) {
- rename('application/controllers/install.php', 'application/controllers/install.lock');
- $this->load->library('session');
- if (!emptyempty($this->session->userdata['login'])) {
- $this->session->sess_destroy(); // destroy the session
- }
- redirect('poster_admin/login', 'refresh');
- } else {
- $data['error'] = '超级管理员用户名已存在';
- $this->load->view('install', $data);
- }
- } else {
- $data['error'] = '安装失败,无法写入文件';
- $this->load->view('install', $data);
- }
- }
- }
- }
- }
- ?>
Model:
- class Install_model extends CI_Model {
- function __construct() {
- parent::__construct();
- $this->load->database();
- $this->load->dbforge();
- }
- function install() {
- if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_category'")) == 0)
- $this->db->query("CREATE TABLE pr_category(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, category VARCHAR(100) NOT NULL UNIQUE, deadline INT NOT NULL)");
- if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_college'")) == 0)
- $this->db->query("CREATE TABLE pr_college(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL UNIQUE)");
- if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_level'")) == 0)
- $this->db->query("CREATE TABLE pr_level(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, level INT NOT NULL, name VARCHAR(20) NOT NULL)");
- if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_rates'")) == 0)
- $this->db->query("CREATE TABLE pr_rates(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, pid INT NOT NULL, ip VARCHAR(40) NOT NULL, category INT NOT NULL)");
- if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_users'")) == 0)
- $this->db->query("CREATE TABLE pr_users(uid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, username VARCHAR(45) NOT NULL UNIQUE, password VARCHAR(50) NOT NULL, level INT NOT NULL )");
- if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'pr_works'")) == 0)
- $this->db->query("CREATE TABLE pr_works(pid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, title VARCHAR(100) NOT NULL, content TEXT, realname VARCHAR(20) NOT NULL, studentnum VARCHAR(20) NOT NULL, college INT NOT NULL, filename TEXT NOT NULL, category INT NOT NULL)");
- $query1 = $this->db->get_where('pr_level',array('level' => 1, 'name' => '普通用户'));
- $query2 = $this->db->get_where('pr_level',array('level' => 5, 'name' => '管理员'));
- $query3 = $this->db->get_where('pr_level',array('level' => 99, 'name' => '超级管理员'));
- if ($query1->num_rows() == 0)
- $this->db->query("INSERT INTO pr_level(level, name) VALUES (1, '普通用户')");
- if ($query2->num_rows() == 0)
- $this->db->query("INSERT INTO pr_level(level, name) VALUES (5, '管理员')");
- if ($query3->num_rows() == 0)
- $this->db->query("INSERT INTO pr_level(level, name) VALUES (99, '超级管理员')");
- $this->username = $this->input->post('username');
- $this->password = $this->input->post('password');
- $this->level = 99;
- $query4 = $this->db->get_where('pr_users',array('username' => $this->input->post('username')));
- if ($query4->num_rows() == 0) {
- $this->db->insert('pr_users', $this);
- return TRUE;
- } else {
- return FALSE;
- }
- }
- }
View:
- <!DOCTYPE html>
- <html lang="Zh-CN">
- <html>
- <head>
- <meta charset="utf-8"/>
- <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
- <link rel="stylesheet" href="<?php echo base_url() . 'css/bootstrap.css' ?>">
- <link rel="stylesheet" href="<?php echo base_url() . 'css/login.css' ?>">
- <link rel="icon" type="image/x-icon" href="<?php echo base_url() . 'favicon.ico' ?>"/>
- <script src="<?php echo base_url() . 'js/jquery-1.9.0.js' ?>"></script>
- <script src="<?php echo base_url() . 'js/bootstrap.js' ?>"></script>
- <title><?php echo $title; ?> - SMU Poster</title>
- </head>
- <body>
- <div class="container">
- <div class="row">
- <div class="col-sm-4 col-sm-offset-4">
- <div <?php $err = validation_errors(); echo !(empty($error) && empty($err)) ? 'class="alert alert-danger text-center"' : '' ?>><?php echo validation_errors(); ?>
- <?php echo $error ?></div>
- <?php echo form_open('install', array('class'=>'form-horizontal','role'=>'form')); ?>
- <div class="col-sm-sm-4 col-sm-offset-4"><h2>安装向导</h2></div>
- <div class="form-group">
- <div class="col-sm-9 col-sm-offset-2">
- <div class="input-group">
- <span class="input-group-addon"><span class="glyphicon glyphicon-cloud"></span></span>
- <input type="text" class="form-control" onMouseOver="$(this).tooltip('show')" data-toggle="tooltip" title="一般为localhost" id="hostname" name="hostname" placeholder="主机名" value="localhost"/>
- </div>
- </div>
- </div>
- <div class="form-group">
- <div class="col-sm-9 col-sm-offset-2">
- <div class="input-group">
- <span class="input-group-addon"><span class="glyphicon glyphicon-book"></span></span>
- <input type="text" class="form-control" onMouseOver="$(this).tooltip('show')" data-toggle="tooltip" title="数据库名如poster,请自行创建" id="book" name="book" placeholder="数据库名"/>
- </div>
- </div>
- </div>
- <div class="form-group">
- <div class="col-sm-9 col-sm-offset-2">
- <div class="input-group">
- <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
- <input type="text" class="form-control" onMouseOver="$(this).tooltip('show')" data-toggle="tooltip" title="连接数据库的账号" id="rootname" name="rootname" placeholder="数据库账号"/>
- </div>
- </div>
- </div>
- <div class="form-group">
- <div class="col-sm-9 col-sm-offset-2">
- <div class="input-group">
- <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
- <input type="password" class="form-control" onMouseOver="$(this).tooltip('show')" data-toggle="tooltip" title="连接数据库的密码" id="pass" name="pass" placeholder="数据库密码"/>
- </div>
- </div>
- </div>
- <div class="form-group">
- <div class="col-sm-9 col-sm-offset-2">
- <div class="input-group">
- <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
- <input type="text" onMouseOver="$(this).tooltip('show')" data-toggle="tooltip" title="注册一个后台超级管理员账号" class="form-control" id="username" name="username" placeholder="后台登录账号"/>
- </div>
- </div>
- </div>
- <div class="form-group">
- <div class="col-sm-9 col-sm-offset-2">
- <div class="input-group">
- <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
- <input type="password" onMouseOver="$(this).tooltip('show')" data-toggle="tooltip" title="后台超级管理员密码" class="form-control" id="password" name="password" placeholder="后台登陆密码"/>
- </div>
- </div>
- </div>
- <div class="form-group">
- <div class="col-sm-offset-3 col-sm-9">
- <button type="submit" class="btn btn-default">开始安装</button>
- </div>
- </div>
- </form>
- </div>
- </div>
- </div>
- <div id="footer">
- <div class="container">
- <p class="text-center copyright text-muted">Designed By <a href="http://www.phpfensi.com/">微软技术俱乐部</a></p>
- </div>
- </div>
- </body>
- </html>
Tags: PHP网站安装 PHP实现程序
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)