This time by doing a install.php to find that the difficulty is actually not big, or file write operation, installation in fact, the main operation is still the contents of the database, first to see how the file to write: (or the use of codeigiter, for the use of other frameworks or handwriting, only ideas can be referenced, with a lot of ci from the Hel per or library)
The code is as follows |
Copy Code |
$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; ";
|
In the file with N to wrap, because the inside contains the PHP code, which led to we can only use double quotes to avoid conflicts (otherwise we have to use, feel more work), for $DB, direct display must use single quotes, so there is this.
After writing the file, what we need to do is to perform a series of installation operations, that is, create TABLE, as well as creating a new user to log in, in model, I write:
The code is as follows |
Copy Code |
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 (MB) not NUL L 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 (MB) NOT NULL UNI QUE) ");
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 () 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 (=) NOT null U Nique, Password VARCHAR () 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 (m) not NULL, CO ntent text, Realname VARCHAR () not NULL, Studentnum VARCHAR is not NULL, college INT is not NULL, filename text is not NULL, Category INT not NULL) ");
$query 1 = $this->db->get_where (' Pr_level ', Array (' Level ' => 1, ' name ' => ' ordinary user '));
$query 2 = $this->db->get_where (' Pr_level ', Array (' Level ' => 5, ' name ' => ' admin '));
$query 3 = $this->db->get_where (' Pr_level ', Array (' Level ' =>, ' name ' => ' Super admin '));
if ($query 1->num_rows () = = 0)
$this->db->query ("INSERT into Pr_level (level, name) VALUES (1, ' Ordinary user ')");
if ($query 2->num_rows () = = 0)
$this->db->query ("INSERT into Pr_level (level, name) VALUES (5, ' admin ')");
if ($query 3->num_rows () = = 0)
$this->db->query ("INSERT into Pr_level (level, name) VALUES (99, ' super admin ')");
$this->username = $this->input->post (' username ');
$this->password = $this->input->post (' password ');
$this->level = 99;
$query 4 = $this->db->get_where (' pr_users ', Array (' username ' => $this->input->post (' username '));
if ($query 4->num_rows () = = 0) {
$this->db->insert (' pr_users ', $this);
return TRUE;
} else {
return FALSE;
}
}
|
In fact, the query volume is very efficient and low, but this effectively avoids the last installation was interrupted after the installation of the trouble, to detect whether a table has been created, whether there are new users and so on.
The code is as follows |
Copy Code |
Mysql_num_rows (mysql_query ("Show TABLES like ' Pr_category '"))
|
This can be queried whether the table exists in the database.
If the execution is successful, change the install.php name:
The code is as follows |
Copy Code |
Rename (' application/controllers/install.php ', ' application/controllers/install.lock ');
|
In other files to add detection install.php whether there is, if there is a jump to install.php, so do a simple installation process (must be placed before the model, otherwise you will be prompted without the database and will not jump).
The code is as follows |
Copy Code |
if (file_exists (' application/controllers/install.php ')) Redirect (' Install ');
|
As for what exists in the table and the like, because the CI will check and priority is higher than our own written error prompts, so here is not added.
Full Source (MVC):
Controller:
The code is as follows |
Copy Code |
<?php
Class Install extends Ci_controller {
function __construct () {
Parent::__construct ();
$this->load->helper (' url ');
$this->load->helper (' file ');
}
function index () {
$data [' title '] = ' installation Wizard ';
$this->load->helper (' form ');
$this->load->library (' form_validation ');
$this->form_validation->set_rules (' hostname ', ' host name ', ' Trim|required|xss_clean ');
$this->form_validation->set_rules (' rootname ', ' Database username ', ' trim|required|xss_clean ');
$this->form_validation->set_rules (' username ', ' username ', ' trim|required|xss_clean ');
$this->form_validation->set_rules (' Password ', ' 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 (!empty ($this->session->userdata[' login ')) {
$this->session->sess_destroy (); Destroy the session
}
Redirect (' Poster_admin/login ', ' Refresh ');
} else {
$data [' error '] = ' super Administrator username already exists ';
$this->load->view (' Install ', $data);
}
} else {
$data [' error '] = ' install failed, cannot write to file ';
$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 (MB) not NUL L 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 (MB) NOT NULL UNI QUE) ");
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 () 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 (=) NOT null U Nique, Password VARCHAR () 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 (m) not NULL, CO ntent text, Realname VARCHAR () not NULL, Studentnum VARCHAR is not NULL, college INT is not NULL, filename text is not NULL, Category INT not NULL) ");
$query 1 = $this->db->get_where (' Pr_level ', Array (' Level ' => 1, ' name ' => ' ordinary user '));
$query 2 = $this->db->get_where (' Pr_level ', Array (' Level ' => 5, ' name ' => ' admin '));
$query 3 = $this->db->get_where (' Pr_level ', Array (' Level ' =>, ' name ' => ' Super admin '));
if ($query 1->num_rows () = = 0)
$this->db->query ("INSERT into Pr_level (level, name) VALUES (1, ' Ordinary user ')");
if ($query 2->num_rows () = = 0)
$this->db->query ("INSERT into Pr_level (level, name) VALUES (5, ' admin ')");
if ($query 3->num_rows () = = 0)
$this->db->query ("INSERT into Pr_level (level, name) VALUES (99, ' super admin ')");
$this->username = $this->input->post (' username ');
$this->password = $this->input->post (' password ');
$this->level = 99;
$query 4 = $this->db->get_where (' pr_users ', Array (' username ' => $this->input->post (' username '));
if ($query 4->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> Setup Wizard </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= " typically localhost "id=" hostname "name=" hostname "placeholder=" host name 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= " Database name such as poster, create "id=" book Name= "book" placeholder= "Database name"/>
</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= " Connect to the database account "id=" Rootname "name=" Rootname "placeholder=" Database Account "/>
</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=" Connect the database Password "id=" pass "name=" pass "placeholder=" Database Password "/>
</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= "register a background Super Administrator account" class= " Form-control "id=" username "name=" username "placeholder=" "Backstage login account"/>
</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=" background Super admin password "class=" Form-control "id=" password "name=" password "placeholder=" "Backstage login password"/>
</div>
</div>
</div>
<div class= "Form-group" >
<div class= "Col-sm-offset-3 col-sm-9" >
<button type= "Submit" class= "Btn Btn-default" > Start installation </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.111cn.net/" > Microsoft Technology Club </a ></p>
</div>
</div>
</body>
</html> |