PHP 网站安装实现程序

作者:袖梨 2022-06-24

这次顺便做了一个install.php才发现难度其实并不大,还是文件写入操作而已,安装其实主要操作的还是数据库里的内容,先来看看文件里怎么写:(还是用的Codeigiter,对于使用其他框架或者手写而言,仅思路可参考,用了挺多CI自带的helper或者是library的)

 代码如下 复制代码
 $content = "  $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;";

在文件里用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:

 代码如下 复制代码

  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 = "  $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 (!empty($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:





 
 
 
 
 
 
 
 <?php echo $title; ?> - SMU Poster



 

 

 
>
 

 'form-horizontal','role'=>'form')); ?>
 

安装向导


 

 

 

 
 
 

 

 

 

 

 

 
 
 

 

 

 

 

 

 
 
 

 

 

 

 

 

 
 
 

 

 

 

 

 

 
 
 

 

 

 

 

 

 
 
 

 

 

 

 

 
 

 

 
 

 




相关文章

精彩推荐