Created
September 27, 2012 02:35
-
-
Save jeffwinesett/3791860 to your computer and use it in GitHub Desktop.
Yii Web Dev Book, Chapter 12, section: Adding a login message log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Displays the login page | |
*/ | |
public function actionLogin() | |
{ | |
Yii::trace("The actionLogin() method is being requested", "application.controllers.SiteController"); | |
//if they are already logged in, redirect them to the home page | |
if(!Yii::app()->user->isGuest) | |
{ | |
$this->redirect(Yii::app()->homeUrl); | |
} | |
$model=new LoginForm; | |
// if it is ajax validation request | |
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') | |
{ | |
echo CActiveForm::validate($model); | |
Yii::app()->end(); | |
} | |
// collect user input data | |
if(isset($_POST['LoginForm'])) | |
{ | |
$model->attributes=$_POST['LoginForm']; | |
// validate user input and redirect to the previous page if valid | |
if($model->validate() && $model->login()) | |
{ | |
Yii::log("Successful login of user: " . Yii::app()->user->id, "info", "application.controllers.SiteController"); | |
$this->redirect(Yii::app()->user->returnUrl); | |
} | |
else | |
{ | |
Yii::log("Failed login attempt", "warning", "application.controllers.SiteController"); | |
} | |
} | |
// display the login form | |
$this->render('login',array('model'=>$model)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment