书签 分享 收藏 举报 版权申诉 / 16

类型在Yii下建立工程并实现用户注册登陆图文解析.docx

  • 文档编号:5213532
  • 上传时间:2022-12-14
  • 格式:DOCX
  • 页数:16
  • 大小:221.12KB

phpecho$form->labelEx($model,'password');?

>

phpecho$form->passwordField($model,'password');?

>

--渲染密码-->

phpecho$form->error($model,'password');?

>

phpechoCHtml:

:

submitButton('Submit');?

>

php$this->endWidget();?

>

php

/*@var$thisSiteController*/

/*@var$modelLoginForm*/

/*@var$formCActiveForm*/

$this->pageTitle=Yii:

:

app()->name.'-Login';

$this->breadcrumbs=array(

'Login',

);

?

>

Login

php$form=$this->beginWidget('CActiveForm',array(

'id'=>'login-form',

'enableClientValidation'=>true,

'clientOptions'=>array(

'validateOnSubmit'=>true,

),

));?

>

phpecho$form->labelEx($model,'userName');?

>

phpecho$form->textField($model,'userName');?

>

phpecho$form->error($model,'userName');?

>

phpecho$form->labelEx($model,'password');?

>

phpecho$form->passwordField($model,'password');?

>

phpecho$form->error($model,'password');?

>

phpechoCHtml:

:

submitButton('Login');?

>

php$this->endWidget();?

>

--form-->

【models】

Yii中的Model主要实现的功能为

(1)具备属性来存储数据

(2)publicfunctionsrules()用来定义验证规则

(3)publicfunctionattributeLabels()声明属性对于的标签名称

php

classRegisterFormextendsCFormModel{

public$userName;

public$password;

publicfunctionrules(){

returnarray(

array('userName,password','required')

);

}

}

?

>

php

classLoginFormextendsCFormModel

{

public$userName;

public$password;

private$_identity;

/**

*Declaresthevalidationrules.

*Therulesstatethatusernameandpasswordarerequired,

*andpasswordneedstobeauthenticated.

*/

publicfunctionrules()

{

returnarray(

//usernameandpasswordarerequired

array('userName,password','required'),

//passwordneedstobeauthenticated

array('password','authenticate'),

);

}

/**

*Authenticatesthepassword.

*Thisisthe'authenticate'validatorasdeclaredinrules().

*/

publicfunctionauthenticate($attribute,$params)

{

if(!

$this->hasErrors())

{

$this->_identity=newUserIdentity($this->userName,$this->password);

if(!

$this->_identity->authenticate())

$this->addError('password','Incorrectusernameorpassword.');

}

}

/**

*Logsintheuserusingthegivenusernameandpasswordinthemodel.

*@returnbooleanwhetherloginissuccessful

*/

publicfunctionlogin()

{

if($this->_identity===null)

{

$this->_identity=newUserIdentity($this->userName,$this->password);

$this->_identity->authenticate();

}

if($this->_identity->errorCode===UserIdentity:

:

ERROR_NONE)

{

$duration=$this->rememberMe?

3600*24*30:

0;//30days

Yii:

:

app()->user->login($this->_identity,$duration);

returntrue;

}

else

returnfalse;

}

}

【Controllers】

php

classSiteControllerextendsController

{

/**

*Declaresclass-basedactions.

*/

publicfunctionactions()

{

returnarray(

'captcha'=>array(

'class'=>'CCaptchaAction',

'backColor'=>0xFFFFFF,

),

'page'=>array(

'class'=>'CViewAction',

),

);

}

publicfunctionactionIndex(){

$this->render('index');

}

publicfunctionactionRegister(){

$model=newRegisterForm;

if(isset($_POST['RegisterForm'])){

$model->attributes=$_POST['RegisterForm'];

$userName=$model->userName;//can'tbewrittenas"$userName=trim($model->userName);"or"$userName=isset($model->userName);"

$password=md5($model->password);

$conn=Yii:

:

app()->db;

$msql="selectuserNamefrom`user`whereuserName='$userName'";

$command=$conn->createCommand($msql);

$num=$command->queryRow();//返回userName='$userName'那条记录

if($num){

$this->refresh();

}else{

$sql="insertinto`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',array('model'=>$model));

}

publicfunctionactionLogin(){

$model=newLoginForm;

if(isset($_POST['LoginForm'])){

$model->attributes=$_POST['LoginForm'];

$username=$model->userName;

$password=md5($model->password);

$conn=Yii:

:

app()->db;

$sql="select*from`user`whereuserName='$username'";

$command=$conn->createCommand($sql);

$row=$command->queryAll();//获取所有数据

foreach($rowas$k=>$v){//获取到的数据为二维数组

$name=$v['userName'];

$pw=$v['password'];

}

if($name){

if($pw==$password){

Yii:

:

app()->session['user']=$name;//记录登录用户的session

$this->redirect(Yii:

:

app()->createUrl('/site/content'));

}else{

echo"passwordiswrong";

$this->refresh();

}

}else{

echo"userisnotexist";

$this->refresh();//刷新页面

}

}

//displaytheloginform

$this->render('login',array('model'=>$model));

}

publicfunctionactionContent(){

$model=newContentForm;

if(isset($_POST['ContentForm'])){

$model->attributes=$_POST['ContentForm'];

$content=$model->content;

$conn=Yii:

:

app()->db;

$sql="insertinto`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));

}

/**

*Thisistheactiontohandleexternalexceptions.

*/

publicfunctionactionError()

{

if($error=Yii:

:

app()->errorHandler->error)

{

if(Yii:

:

app()->request->isAjaxRequest)

echo$error['message'];

else

$this->render('error',$error);

}

}

}

?

>

目录下的文件

php

classControllerextendsCController

{

public$layout='//layouts/column1';

public$menu=array();

public$breadcrumbs=array();

}

?

>

php

classUserIdentityextendsCUserIdentity

{

publicfunctionauthenticate()

{

$users=array(

//username=>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;

}

}

?

>

最终效果:

数据库数据:

举报
举报
版权申诉
版权申诉
word格式文档无特别注明外均可编辑修改;预览文档经过压缩,下载后原文更清晰! 立即下载
配套讲稿:

如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

特殊限制:

部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

关 键  词:
Yii 建立 工程 实现 用户 注册 登陆 图文 解析
提示  冰豆网所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
关于本文
本文标题:在Yii下建立工程并实现用户注册登陆图文解析.docx
链接地址:https://www.bdocx.com/doc/5213532.html
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2008-2022 冰点文档网站版权所有

经营许可证编号:鄂ICP备2022015515号-1

收起
展开