1、在Yii下建立工程并实现用户注册登陆图文解析在Yii下建立工程并实现用户注册登陆【下载Yii库,解压并使用命令建立工程】以库文件在桌面并把工程sys建立在F盘的wamp/www文件夹下为例输入指令:cd Desktopcd yii/frameworkyiic webapp F:wamp/www/sys过程中如果出现”php.exe”不是内部或外部命令,也不是可运行的程序的提示解决方法:打开yiic.bat,在set PHP_COMMAND后添加php的路径修改path环境变量(把php.exe的路径设置到环境变量上去,在分号后面添加即可)数据库数据库代码:- phpMyAdmin SQL Du
2、mp- version 3.4.5- - 主机: localhost- 生成日期: 2015 年 04 月 01 日 09:04- 服务器版本: 5.5.16- PHP 版本: 5.3.8SET 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_COLLA
3、TION_CONNECTION=COLLATION_CONNECTION */;/*!40101 SET NAMES utf8 */;- 数据库: sys- - 表的结构 user-CREATE TABLE IF NOT EXISTS user ( userId int(10) NOT NULL AUTO_INCREMENT, userName varchar(64) CHARACTER SET utf8 NOT NULL, password varchar(32) CHARACTER SET utf8 NOT NULL, PRIMARY KEY (userId) ENGINE=InnoDB
4、DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=3 ;- 转存表中的数据 user-INSERT INTO user (userId, userName, password) VALUES(1, admin, 21232f297a57a5a743894a0e4a801fc3),(2, aa, 4124bc0a9335c27f086f24ba207a4912);/*!40101 SET CHARACTER_SET_CLIENT=OLD_CHARACTER_SET_CLIENT */;/*!40101 SET CHARACTER_SET_RESULTS=OLD_CHA
5、RACTER_SET_RESULTS */;/*!40101 SET COLLATION_CONNECTION=OLD_COLLATION_CONNECTION */;工程文件结构修改$yiic的路径为框架文件的路径修改$yii的路径连接数据库,修改main.php中db的内容,如下【views】Yii的views目录下有layouts和 site 两个文件夹layouts中的main.php为页面主要框架,而site的内容显示在 $content自行修改菜单 pageTitle=Yii:app()-name . - Register; $this-breadcrumbs=array( Reg
6、ister, ); ? beginWidget(CActiveForm, array( id=register-form, enableClientValidation=true, clientOptions=array( validateOnSubmit=true, ), ); ? errorSummary($model); ? labelEx($model,userName); ? textField($model,userName); ? error($model,userName); ? labelEx($model,password); ? passwordField($model,
7、password); ? error($model,password); ? endWidget(); ?pageTitle=Yii:app()-name . - Login;$this-breadcrumbs=array( Login,);?LoginbeginWidget(CActiveForm, array( id=login-form, enableClientValidation=true, clientOptions=array( validateOnSubmit=true, ),); ? labelEx($model,userName); ? textField($model,u
8、serName); ? error($model,userName); ? labelEx($model,password); ? passwordField($model,password); ? error($model,password); ? endWidget(); ?【models】 Yii中的Model主要实现的功能为(1)具备属性来存储数据(2)public functions rules() 用来定义验证规则(3)public function attributeLabels() 声明属性对于的标签名称hasErrors() $this-_identity=new UserI
9、dentity($this-userName,$this-password); if(!$this-_identity-authenticate() $this-addError(password,Incorrect username or password.); /* * Logs in the user using the given username and password in the model. * return boolean whether login is successful */ public function login() if($this-_identity=nu
10、ll) $this-_identity=new UserIdentity($this-userName,$this-password); $this-_identity-authenticate(); if($this-_identity-errorCode=UserIdentity:ERROR_NONE) $duration=$this-rememberMe ? 3600*24*30 : 0; / 30 days Yii:app()-user-login($this-_identity,$duration); return true; else return false; 【Controll
11、ers】array( class=CCaptchaAction, backColor=0xFFFFFF, ), page=array( class=CViewAction, ), ); public function actionIndex() $this-render(index); public function actionRegister() $model = new RegisterForm; if(isset($_POSTRegisterForm) $model-attributes=$_POSTRegisterForm; $userName = $model-userName;
12、/cant be written as $userName = trim($model-userName); or $userName = isset($model-userName); $password = md5($model-password); $conn = Yii:app()-db; $msql = select userName from user where userName = $userName ; $command = $conn-createCommand($msql); $num = $command-queryRow(); /返回userName=$userNam
13、e那条记录 if($num) $this-refresh(); else $sql=insert into user(userName,password) values($userName,$password); $command = $conn-createCommand($sql); $dataReader = $command-query(); if($dataReader) $this-redirect(Yii:app()-createUrl(/site/Login); /控制器间跳转 exit(); $this-refresh(); $this-render(register,arr
14、ay(model=$model); public function actionLogin() $model=new LoginForm; if(isset($_POSTLoginForm) $model-attributes = $_POSTLoginForm; $username = $model-userName; $password = md5($model-password); $conn = Yii:app()-db; $sql = select * from user where userName = $username; $command = $conn-createComma
15、nd($sql); $row = $command-queryAll(); /获取所有数据 foreach($row as $k = $v) /获取到的数据为二维数组 $name = $vuserName; $pw = $vpassword; if($name) if($pw = $password) Yii:app()-sessionuser = $name; /记录登录用户的session $this-redirect(Yii:app()-createUrl(/site/content); else echo password is wrong; $this-refresh(); else
16、 echo user is not exist; $this-refresh(); /刷新页面 / display the login form $this-render(login,array(model=$model); public function actionContent() $model = new ContentForm; if(isset($_POSTContentForm) $model-attributes = $_POSTContentForm; $content = $model-content; $conn = Yii:app()-db; $sql = insert
17、 into user(message)values($content); $command = $conn-createCommand($sql); $data = $command-query(); if($data) $this-redirect(Yii:app()-createUrl(/site/index); $this-render(content,array(model=$model); /* * This is the action to handle external exceptions. */ public function actionError() if($error=
18、Yii:app()-errorHandler-error) if(Yii:app()-request-isAjaxRequest) echo $errormessage; else $this-render(error, $error); ?目录下的文件 password demo=demo, admin=admin, ); if(!isset($users$this-username) $this-errorCode=self:ERROR_USERNAME_INVALID; elseif($users$this-username!=$this-password) $this-errorCode=self:ERROR_PASSWORD_INVALID; else $this-errorCode=self:ERROR_NONE; return !$this-errorCode; ?最终效果:数据库数据:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1