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

上传人:b****5 文档编号:8403143 上传时间:2023-05-11 格式:DOCX 页数:16 大小:221.12KB
下载 相关 举报
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第1页
第1页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第2页
第2页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第3页
第3页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第4页
第4页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第5页
第5页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第6页
第6页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第7页
第7页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第8页
第8页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第9页
第9页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第10页
第10页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第11页
第11页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第12页
第12页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第13页
第13页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第14页
第14页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第15页
第15页 / 共16页
在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

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

《在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx》由会员分享,可在线阅读,更多相关《在Yii下建立工程并实现用户注册登陆图文解析Word下载.docx(16页珍藏版)》请在冰点文库上搜索。

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

40101SETNAMESutf8*/;

--数据库:

`sys`

----------------------------------------------------------

--表的结构`user`

CREATETABLEIFNOTEXISTS`user`(

`userId`int(10)NOTNULLAUTO_INCREMENT,

`userName`varchar(64)CHARACTERSETutf8NOTNULL,

`password`varchar(32)CHARACTERSETutf8NOTNULL,

PRIMARYKEY(`userId`)

)ENGINE=InnoDBDEFAULTCHARSET=utf8mb4AUTO_INCREMENT=3;

--转存表中的数据`user`

INSERTINTO`user`(`userId`,`userName`,`password`)VALUES

(1,'

admin'

'

21232f297a57a5a743894a0e4a801fc3'

),

(2,'

aa'

4124bc0a9335c27f086f24ba207a4912'

);

40101SETCHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT*/;

40101SETCHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS*/;

40101SETCOLLATION_CONNECTION=@OLD_COLLATION_CONNECTION*/;

工程文件结构

<

protected/views/yiic.php>

修改$yiic的路径为框架文件的路径

protected/index.php>

修改$yii的路径

protected/config/main.php>

连接数据库,修改main.php中’db’的内容,如下

【views】

Yii的views目录下有layouts和site两个文件夹

protected/views/layouts/main.php>

layouts中的main.php为页面主要框架,而site的内容显示在$content

自行修改菜单

protected/site>

protected/views/site/Register.php>

<

?

php

$this->

pageTitle=Yii:

:

app()->

name.'

-Register'

breadcrumbs=array(

'

Register'

);

?

>

divclass="

form"

php

$form=$this->

beginWidget('

CActiveForm'

array(

id'

=>

'

register-form'

enableClientValidation'

true,

clientOptions'

array(

validateOnSubmit'

),

));

echo$form->

errorSummary($model);

row"

phpecho$form->

labelEx($model,'

userName'

textField($model,'

error($model,'

/div>

password'

passwordField($model,'

!

--渲染密码-->

rowbuttons"

phpechoCHtml:

submitButton('

Submit'

php$this->

endWidget();

protected/views/site/Login.php>

/*@var$thisSiteController*/

/*@var$modelLoginForm*/

/*@var$formCActiveForm*/

$this->

-Login'

Login'

h1>

Login<

/h1>

php$form=$this->

login-form'

'

));

<

--form-->

【models】

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

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

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

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

protected/models/RegisterForm.php>

classRegisterFormextendsCFormModel{

public$userName;

public$password;

publicfunctionrules(){

returnarray(

array('

userName,password'

'

required'

}

protected/models/LoginForm.php>

classLoginFormextendsCFormModel

{

private$_identity;

/**

*Declaresthevalidationrules.

*Therulesstatethatusernameandpasswordarerequired,

*andpasswordneedstobeauthenticated.

*/

publicfunctionrules()

{

returnarray(

//usernameandpasswordarerequired

array('

userName,password'

//passwordneedstobeauthenticated

authenticate'

);

*Authenticatesthepassword.

*Thisisthe'

validatorasdeclaredinrules().

publicfunctionauthenticate($attribute,$params)

if(!

hasErrors())

{

$this->

_identity=newUserIdentity($this->

userName,$this->

password);

if(!

_identity->

authenticate())

$this->

addError('

Incorrectusernameorpassword.'

}

*Logsintheuserusingthegivenusernameandpasswordinthemodel.

*@returnbooleanwhetherloginissuccessful

publicfunctionlogin()

if($this->

_identity===null)

authenticate();

errorCode===UserIdentity:

ERROR_NONE)

$duration=$this->

rememberMe?

3600*24*30:

0;

//30days

Yii:

user->

login($this->

_identity,$duration);

returntrue;

else

returnfalse;

}

【Controllers】

protected/controllers/SiteController.php>

classSiteControllerextendsController

/**

*Declaresclass-basedactions.

publicfunctionactions()

'

captcha'

'

class'

CCaptchaAction'

backColor'

0xFFFFFF,

),

page'

CViewAction'

publicfunctionactionIndex(){

render('

index'

publicfunctionactionRegister(){

$model=newRegisterForm;

if(isset($_POST['

RegisterForm'

])){

$model->

attributes=$_POST['

];

$userName=$model->

userName;

//can'

tbewrittenas"

$userName=trim($model->

userName);

"

or"

$userName=isset($model->

$password=md5($model->

$conn=Yii:

db;

$msql="

selectuserNamefrom`user`whereuserName='

$userName'

"

$command=$conn->

createCommand($msql);

$num=$command->

queryRow();

//返回userName='

那条记录

if($num){

refresh();

}else{

$sql="

insertinto`user`(userName,password)values('

$password'

)"

createCommand($sql);

$dataReader=$command->

query();

if($dataReader){

redirect(Yii:

createUrl('

/site/Login'

//控制器间跳转

exit();

register'

array('

model'

$model));

publicfunctionactionLogin(){

$model=newLoginForm;

LoginForm'

attributes=$_POST['

$username=$model->

$sql="

select*from`user`whereuserName='

$username'

$row=$command->

queryAll();

//获取所有数据

foreach($rowas$k=>

$v){//获取到的数据为二维数组

$name=$v['

$pw=$v['

if($name){

if($pw==$password){

Yii:

session['

user'

]=$name;

//记录登录用户的session

/site/content'

echo"

passwordiswrong"

}else{

userisnotexist"

//刷新页面

//displaytheloginform

$this->

login'

publicfunctionactionContent(){

$model=newContentForm;

ContentForm'

$content=$model->

content;

insertinto`user`(message)values('

$content'

$data=$command->

if($data){

/site/index'

content'

*Thisistheactiontohandleexternalexceptions.

publicfunctionactionError()

if($error=Yii:

errorHandler->

error)

if(Yii:

request->

isAjaxRequest)

echo$error['

message'

else

error'

$error);

protected/components>

目录下的文件

Controller.php>

classControllerextendsCController

public$layout='

//layouts/column1'

public$menu=array();

public$breadcrumbs=array();

UserIdentity.php>

classUserIdentityextendsCUserIdentity

publicfunctionauthenticate()

$users=array(

//username=>

password

demo'

isset($users[$this->

username]))

errorCode=self:

ERROR_USERNAME_INVALID;

elseif($users[$this->

username]!

==$this->

password)

ERROR_PASSWORD_INVALID;

ERROR_NONE;

return!

errorCode;

最终效果:

数据库数据:

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 职业教育 > 职业技术培训

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2