2015-07-14 10:20:11 5615次浏览 3条回答 0 悬赏 5 金钱

views 里面代码

<!DOCTYPE html>
<html>
    <head>
	<title>前台登录</title>
	<link href="<?php echo (Yii::app()->request->baseUrl);?>/assets/index/css/jquery.mobile-1.4.5.min.css" type="text/css" rel="stylesheet">
         <script src="<?php echo Yii::app()->request->baseUrl;?>/assets/index/js/jquery-2.1.4.min.js"></script>
         <script src="<?php echo Yii::app()->request->baseUrl;?>/assets/index/js/jquery.mobile-1.4.5.min.js"></script>
		 <script src="<?php echo Yii::app()->request->baseUrl;?>/assets/index/js/main.js"></script>
	<style>
		.image{
			width:60px;
			height:60px;
		}
		#header{
		}
		.errorMessage {
			color: red;
		}
	</style>
    </head>
    <body>

	<div data-role="page">
	    <div data-role="content">
		<h1>前台登录APP</h1>
		<br><br><br>
		    <?php  $form = $this->beginWidget('CActiveForm'); ?>
		    <?php echo $form->textField($loginForm,'nickname',array('id' =>'user','placeholder'=>'用户名/邮箱')); ?>
		    <?php echo $form->error($loginForm,'nickname');?>

		    <?php echo $form->passwordField($loginForm,'password',array('id' =>'pwd','placeholder'=>'请输入密码'));  ?> 
		    <?php echo $form->error($loginForm,'password');?>

		    <?php echo $form->textField($loginForm,'captcha',array('id' =>'verify','placeholder'=>'请输入验证码'));?>
		    <?php echo $form->error($loginForm,'captcha');?>

		    <?php $this->widget('CCaptcha',array(
    				'showRefreshButton'=>false,
    				'clickableImage' => true,
    				'imageOptions' => array('alt'=>'点击换图',
    			        'title' => '点击换图',
    			        'style' => 'cursor:pointer')
    				     ));?>
    				<br/>
		    <input type="submit" value="登录"> 
		    <?php $this->endWidget(); ?>  
	    </div>
	</div>
	<script>
		
	</script>
    </body>

models里面LoginForm.php文件代码

<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */

class LoginForm extends CFormModel
{
	public $nickname;
	public $password;
	public $rememberMe;
	public $captcha;
	private $_identity;
	
	/**
	 * 声明验证规则。  
	 * 规则,需要用户名和密码,
	 * 和密码需要身份验证。
	 */
	public function rules()
	{
		return array(
			// nickname and password are required
			array('nickname', 'required','message'=>'用户名不能为空'),
			array('password', 'required','message' => '密码不能为空'),
			// rememberMe needs to be a boolean
			array('rememberMe', 'boolean'),
			// password needs to be authenticated
			array('password', 'authenticate'),
			array('captcha','captcha','message'=>'验证码错误'),
		);
	}

	/**
	 * 记住我
	 */
	public function attributeLabels()
	{
		return array(
			'rememberMe'=>'Remember me next time', 
		);
	}

	/**
	 * 判断用户名密码
	 */
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())
		{
			$this->_identity=new UserIdentity($this->nickname,$this->password);
			if(!$this->_identity->authenticate())
				$this->addError('password','用户名或密码错误');
		}
	}

	/**
	*记住登录
	*/
	public function login()
	{
		if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($this->nickname,$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;
	}
}

controllers文件里面 LoginController.php 代码

<?php
class LoginController extends Controller
{
	public function actionIndex()
	{
		$loginForm = new LoginForm();
		if(isset($_POST['LoginForm'])){
			$loginForm->attributes=$_POST['LoginForm'];
			if($loginForm->validate()&&$loginForm->login()){
				$this->redirect(array('main/main'));
			}
		}
		$this->render('index',array('loginForm' =>$loginForm));
	}
	public function actions(){
	    return array(
	    	'captcha' => array(
	    		'class' => 'system.web.widgets.captcha.CCaptchaAction',
	    		'height' => 40,
	    		'width' => 80,
	    		'minLength' => 4,
	    		'maxLength' => 4,
	    		//'testLimit' => 999,
	    		),
	    	);
	}
	public function actionOut(){
		Yii::app()->user->logout();
		$this->redirect(array('login/index'));
	}


	public function actionRegister(){
	$registerModel = new Register();
	if(isset($_POST['Register'])){
		//收集信息,保存信息,页面重定向
			$registerModel->attributes=$_POST['Register'];
			if($registerModel->validate()){
				$registerModel->Password=md5($registerModel->Password);
			    if($registerModel->save()){
			    	$this->redirect(array('Check'));
			    }
			}
		}
		//调用添加的视图 
		$this->render('register',array('registerModel' => $registerModel));
	}

}

最佳答案

  • sevenyearsold 发布于 2015-07-14 11:56 举报

    用户验证的逻辑在protected\components\UserIdentity.php的authenticate()方法下,我估计是你这个方法没改或者写错了。你去检查一下。

    3 条回复
    回复于 2015-07-14 11:59 回复

    大神看下,里面的代码哪里不对

    public function authenticate()
    {
        $userinfo = User::model()->find('UserName=:name',array(':name'=>$this->username));
        if($userinfo == NULL){
            $this->errorCode=self::ERROR_USERNAME_INVALID;
            return false;
        }
        if($userinfo->Password != md5($this->password)){
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
            return false;
        }
        $this->errorCode=self::ERROR_NONE;
        return true;
    }
    
    回复于 2015-07-14 13:47 回复

    这么看你的代码是没错的,你试试把当前站的cookie清掉,然后登陆一下看能不能把cookie给加上,如果能看到cookie,那正面的登陆是没问题呢的,是你的main控制器的main方法是空白的

    回复于 2015-07-14 14:32 回复

    大神可以加我QQ指导一下么,84002341 感谢

    觉得很赞
  • 回答于 2015-07-14 11:24 举报

    你好歹return一下呀。。

    2 条回复
    回复于 2015-07-14 11:37 回复

    sorry 没注意这是yii1,是不是这里有问题$this->redirect(array('main/main'));

    回复于 2015-07-14 11:57 回复

    这个是验证完了之后跳转的网页,应该不是这里

  • 回答于 2015-07-14 11:29 举报

    贴出maincontroller看看

    4 条回复
    回复于 2015-07-14 11:58 回复

    要怎么贴出。。我是新手

    回复于 2015-07-14 12:27 回复

    这个$this->redirect(array('main/main'))是跳转到MainController.php的actionMain的,所以你把MainController.php代码给大伙儿看看

    回复于 2015-07-14 12:47 回复


    MainController.php 里面的代码

    class MainController extends Controller 
    {
        public function actionIndex(){
            $this->renderPartial('main');
        }
        public function actionPersonal(){
            $this->renderPartial('personal');
        }
        public function actionSubscribe(){
            $this->renderPartial('subscribe');
        }
    }
    
    回复于 2015-07-14 14:13 回复
    public function actionMain(){
       echo 'here';
    }
    

    新增这个方法,看看那有没有输出

您需要登录后才可以回答。登录 | 立即注册
zhaishao
助理

zhaishao

注册时间:2015-07-13
最后登录:2023-04-21
在线时长:7小时38分
  • 粉丝0
  • 金钱115
  • 威望0
  • 积分185

热门问题