PHP開發之使用CodeIgniter搭建一個簡單的專案
阿新 • • 發佈:2019-01-31
搭建PHP開發環境請參考PHP開發環境搭建
下載CodeIgniter
新建一個專案
將CodeIgniter中所有檔案copy到專案中。
專案建好了,現在我們要做個簡單的註冊登入功能。
別急,跟我慢慢來。
1.寫sql,建立user表
DROP DATABASE IF EXISTS first_ci;
CREATE DATABASE first_ci;
USE first_ci;
DROP TABLE IF EXISTS _user;
CREATE TABLE _user(
_id INT PRIMARY KEY AUTO_INCREMENT NOT NULL COMMENT'主鍵id' ,
_name VARCHAR(100) NOT NULL COMMENT '使用者名稱',
_password VARCHAR(100) NOT NULL COMMENT'密碼'
) ;
COMMIT;
2.修改CI配置檔案
2.1修改檔案application/config/config.php:
$config['base_url'] = 'http://127.0.0.1:80/first_ci/';
2.2.配置CodeIgniter資料庫連線,修改檔案application/config/database.php
'hostname' => 'localhost' ,
'username' => 'root',
'password' => '',//WAMP中MySQL預設沒有密碼
'database' => 'first_ci',
'dbdriver' => 'mysql',
2.3修改檔案application/config/routes.php(此步驟不是必須)
$route['default_controller'] = 'usercontroller';
2.4修改檔案application/config/autoload.php
$autoload['packages'] = array ();
$autoload['libraries'] = array('database', 'session');
$autoload['drivers'] = array();
$autoload['helper'] = array('url');
$autoload['config'] = array();
$autoload['language'] = array();
$autoload['model'] = array();
3.修改其他配置檔案
3.1.修改Apache的httpd.conf檔案(此步驟不是必須)。
找到LoadModule rewrite_module modules/mod_rewrite.so,並將這行程式碼前的“#”註釋去掉。
3.2.新增或修改專案中的.htaccess檔案,.htaccess在專案的根目錄下(此步驟不是必須)
可以參考
中的“刪除 index.php 檔案”。
以下是我修改後的.htaccess檔案全部內容:
<IfModule mod_rewrite.c>
<Files ~ "^.(htaccess|htpasswd)$">
deny from all
</Files>
Options -Indexes
Options +FollowSymLinks
Options +Includes
ErrorDocument 404 /404.htm
ErrorDocument 500 /404.htm
DirectoryIndex index.php
order deny,allow
RewriteEngine on
RewriteBase /first_ci
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
4.寫資料模型UserModel
在application/models目錄下新建UserModel.php,
程式碼如下:
<?php
class UserModel extends CI_Model {
public $table_name = 'user';
public function __construct() {
parent::__construct ();
}
public function setTableName($table_name){
$this->table_name = $table_name;
}
public function register($name, $password) {
if ($this->db->insert ( $this->table_name, array (
'name' => $name,
'password' => $password
) )) {
return $this->db->insert_id();
} else {
log_message ( 'error', 'register error-->' . $this->db->last_query () );
return false;
}
}
public function login($name, $password) {
$this->db->where ( array (
'name' => $name,
'password' => $password
) );
$query = $this->db->get ( $this->table_name );
return $query->row_array ();
}
}
?>
5.寫檢視
5.1在application/views的目錄中編寫register.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>註冊</title>
</head>
<body>
<h1>註冊</h1>
<form action="<?php echo base_url()?>/usercontroller/register"
method="post">
<table>
<tr>
<td>使用者名稱</td>
<td><input name="name"></td>
</tr>
<tr>
<td>密碼</td>
<td><input name="password"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
5.2在application/views的目錄中編寫login.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>註冊</title>
</head>
<body>
<h1>登入</h1>
<form action="<?php echo base_url()?>/usercontroller/login"
method="post">
<table>
<tr>
<td>使用者名稱</td>
<td><input name="name"
value="<?php if (isset($_REQUEST)&&isset($_REQUEST['name'])) {
echo $_REQUEST['name'];
}?>"></td>
</tr>
<tr>
<td>密碼</td>
<td><input name="password" value="<?php if (isset($_REQUEST)&&isset($_REQUEST['password'])) {
echo $_REQUEST['password'];
}?>"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
5.3在application/views的目錄中編寫welcome.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<h1>
You login success!Welcome to First CI!
</h1>
<h2>You username is <?php echo $_REQUEST["name"]?></h2>
</body>
</html>
5.4在application/views的目錄中編寫fail.php
<!DOCTYPE html>
<html>
<head>
<title>fail</title>
</head>
<body>
<p>Login fail! please check username and password.</p>
</body>
</html>
6.寫控制器
在application/controllers目錄下新建UserController.php,
程式碼如下:
<?php
class UserController extends CI_Controller {
public function __construct() {
parent::__construct ();
}
public function index() {
$this->load->view ( "register" );
}
public function register() {
$name = $this->input->post ( 'name' );
$password = $this->input->post ( 'password' );
$this->load->model ( 'UserModel' );
$result = $this->UserModel->register ( $name, $password );
if (gettype ( $result ) == "boolean" && ! $result) {
$this->load->view ( 'errors/cli/error_404.php' );
} else {
$this->load->view ( 'login',$result );
}
}
public function showLogin() {
$this->load->view ( "login.php" );
}
public function login() {
$name = $this->input->post ( 'name' );
$password = $this->input->post ( 'password' );
$this->load->model ( 'UserModel' );
$result = $this->UserModel->login ( $name, $password );
if (! $result) {
$this->load->view ( 'fail' );
} else {
$this->load->view ( 'welcome',$result );
}
}
}
輸入用使用者名稱密碼點選提交:
提交之後可以看見以下介面:
什麼都不管直接點提交後可看到這個介面:
在瀏覽器輸入“http://127.0.0.1/first_ci/usercontroller/showlogin”,再次進入登入介面,這回要進行錯誤的使用者名稱密碼測試登入:
點選提交進入以下介面: