使用这个之前要先搭载一个环境(这个推荐使用wampserver)
下载地址:
点击此处
如果安装成功,访问 http://localhost/ 会如下图:

codeigniter:
CodeIgniter 是一个小巧但功能强大的 PHP 框架,作为一个简单而“优雅”的工具包,它可以为开发者们建立功能完善的 Web 应用程序。
下载地址:
点击此处
安装成功后放在上面安装环境里面wamp安装目录下的www文件夹里面

打开之后点击application

这三个文件夹是不是很熟悉 就是mvc
了解了mvc 用起来就会得心应手
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/';
1
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写数据模型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 ();
}
}
?>
4.写视图
4.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>
4.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>
4.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>
4.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>
5.写控制器
在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 );
}
}
}
本文介绍如何使用CodeIgniter PHP框架搭建一个基本的用户注册与登录系统,包括数据库配置、模型、控制器和视图的实现。
862

被折叠的 条评论
为什么被折叠?



