效果图
sql代码
-- phpMyAdmin SQL Dump -- version phpStudy 2014 -- http://www.phpmyadmin.net -- -- 主机: localhost -- 生成日期: 2014 年 12 月 05 日 09:29 -- 服务器版本: 5.5.38 -- PHP 版本: 5.3.28 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- 数据库: `login` -- -- -------------------------------------------------------- -- -- 表的结构 `xp_user` -- CREATE TABLE IF NOT EXISTS `xp_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `logintime` text NOT NULL, `loginip` varchar(50) NOT NULL, `lock` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=2 ; -- -- 转存表中的数据 `xp_user` -- INSERT INTO `xp_user` (`id`, `username`, `password`, `logintime`, `loginip`, `lock`) VALUES (1, 'admin', '21232f297a57a5a743894a0e4a801fc3', '1417742214', '127.0.0.1', 0); /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
config.php
<?php return array( //'配置项'=>'配置值' //'USERNAME'=>'admin', //赋值 //数据库配置信息 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址 'DB_NAME' => 'login', // 数据库名 'DB_USER' => 'root', // 用户名 'DB_PWD' => 'root', // 密码 'DB_PORT' => 3306, // 端口 'DB_PREFIX' => 'xp_', // 数据库表前缀 //其他项目配置参数 // ... ); ?>
index.html
<html> <body> <form action="{:U('Index/login')}" method="post"> name: <input type="text" name="name"><br> password: <input type="text" name="password"><br> code: <input type="text" name="code"><br> <img src="{:U('Index/verify')}"/> <input type="submit"> </form> </body> </html>
IndexAction.class.php
<?php // 本类由系统自动生成,仅供测试用途 class IndexAction extends Action { public function index(){ $this->display(); } Public function login(){ if(!IS_POST) halt('can not find the page'); if (I('code','','md5') != session('verify')){ $this->error('the code is error'); } //echo 'hello'; $username = I('name'); $pwd = I('password','','md5'); $user = M('user')->where(array('username'=>$username))->find(); //var_dump($user); //var_dump($username); //var_dump($user); //var_dump($password); if ( !$user || $user['password'] != $pwd){ $this->error('账号或密码错误'); } //echo 'hello'; if ($user['lock']) $this->error('用户被锁定'); //dump($user); $data=array( 'id'=>$user['id'], 'logintime'=>time(), 'loginip'=>get_client_ip(), ); //dump($data); M('user')->save($data); session('uid',$user['id']); session('username',$user['username']); session('logintime',date('Y-m-d H:i;s',$user['logintime'])); session('loginip',$user['loginip']); $this->redirect('Admin/index'); //echo $_SESSION['verify'];//显示验证码的SESSION值 //echo'<br/>'; //echo md5($_POST['code']);//显示验证码的md5值 //echo'<br/>'; //var_dump($_POST); } Public function verify(){ ob_clean(); //ob_clean函数 清空先前输出 import('ORG.Util.Image'); //import调用的是message/ThinkPHP框架目录下的扩展包Extend/Library/ORG/Util/中的Image.class.php类文件 Image::buildImageVerify(4,1,'png'); //调用buildImageVerify方法生成验证码,默认参数为($length=4, $mode=1, $type='png', $width=48, $height=22, $verifyName='verify'),有兴趣的朋友可以研究下Image类 } Public function admin(){ echo 'hello world'; } }
AdminAction.class.php
<?php class AdminAction extends CommonAction { //Public function _initialize(){ //echo '这是自动运行的方法'; //} public function index(){ echo 'hello 123'; } Public function copy(){ echo '1111'; } Public function loginout(){ session_unset(); session_destroy(); $this->redirect('Index/index'); } }
CommonAction.class.php
<?php Class CommonAction extends Action{ Public function _initialize(){ if (!isset($_SESSION['uid']) || !isset($_SESSION['username'])){ $this->redirect('Index/index'); } //echo '123123123'; //$this->redirect('Index/index'); } }