CakePHP 3 step registration - php

I am trying to create a 3 step registration page withh cakePHP.
First step is OK, it inserts the data in db but the second step is not working. Can someone help please?
Here is my controller code:
<?php
App::uses('Controller', 'Controller');
class UsersController extends AppController {
public $name = 'Users';
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('signup');
}
public function login(){
if($this->request->is('post')){
if($this->Auth->login()){
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Invalid username or password!', 'default', array('class' => 'errormsg'));
}
}
}
public function logout(){
$this->redirect($this->Auth->logout());
}
// SIGN UP ACTIONS
public function signup($step = null){
// SET STEP VARIABLE
$this->set('s', $step);
// STEP 1 - REGISTER
if(empty($step)){
if ($this->request->is('post')) {
if ($this->User->save($this->request->data)) {
$this->Session->write('regUser', $this->request->data['User']['email']);
// $this->Session->setFlash('The user has been saved, please enter other information!',
// 'default', array('class' => 'okormsg'));
$regUser = $this->User->find( 'first', array('conditions' => array(
'User.username' => $this->Session->read('regUser'))));
$id = $regUser['User']['id'];
$this->Session->write('regUserId', $id);
$this->redirect(array('personal-info'));
} else {
$this->Session->setFlash( 'There was an error, please check the fields bellow!',
'default', array('class' => 'errormsg'));
}
}
// STEP 2 - PERSONAL INFORMATION
} elseif($step == 'personal-info') {
if ($this->request->is('post')) {
$id = $this->Session->read('regUserId');
$this->User->id = $id;
if ($this->User->save($this->request->data)) {
$this->Session->setFlash('The user has been saved');
$this->redirect(array('complete'));
} else {
$this->Session->setFlash('User could not be saved. Please, try again.');
}
}
// STEP 3 - COMPLETE REGISTRATION
} elseif($step == 'complete') {
}
}
}
?>
Here is the Model:
<?php
class User extends AppModel {
public $validate = array (
'name'=>array(
'Please enter your name!'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter your name!'
)
),
'surname'=>array(
'Please enter your surname!'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter your surname!'
)
),
'email'=>array(
'Valid email'=>array(
'rule'=>array('email'),
'message'=>'Please enter a valid Email address!'
),
'Already exists'=>array(
'rule'=>'isUnique',
'message'=>'This Email is already registered in our database!'
)
),
'password'=>array(
'Not empty'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter your password!'
),
'Match password'=>array(
'rule'=>'matchPasswords',
'message'=>'Your passwords do not match!'
)
),
'password_confirm'=>array(
'Not empty'=>array(
'rule'=>'notEmpty',
'message'=>'Please confirm your password!'
)
),
'terms'=>array(
'Agree'=>array(
'rule'=>'notEmpty',
'required'=>true,
'message'=>'You must agree to Terms and conditions!'
)
)
);
public function matchPasswords($data){
if ($data['password'] == $this->data['User']['password_confirm']){
return true;
} else {
$this->invalidate('password_confirm', 'Your passwords do not match!');
return false;
}
}
public function beforeSave(){
if(!empty($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
if (empty($this->data['User']['username'])) {
$this->data['User']['username'] = $this->data['User']['email'];
}
return true;
}
}
?>
And here is the view for signup.ctp
<!-- SIGN UP STEPS -->
<!-- SIGN UP - 1 (REGISTER) -->
<?php if(empty($s)): ?>
<div id="register" class="container padded">
<div id="register">
<div class="paginate active">Register</div>
<div class="paginate">Personal Information</div>
<div class="paginate">Complete Registration</div>
<h1>User Registration</h1>
<p>Welcome to user registration! You can sign up yourself and start improving your business. User registration is FREE and once you register yourself you can select apropiate business package for your company and start advertising.<br />
<span style="color:#900">All field are required!</span></p>
<?php
echo $this->Form->create();
echo $this->Form->input('name');
echo $this->Form->input('surname');
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->input('password_confirm', array('label'=>'Password Confirmation','type'=>'password'));
echo $this->Form->input('terms', array('label'=>false, 'type'=>'checkbox'));
?> I accept Terms Of Use <?
echo $this->Form->end('Continue Registration');
?>
</div>
</div>
<!-- SIGN UP - 2 (PERSONAL INFO) -->
<?php elseif($s == 'personal-info'): ?>
<div id="register" class="container padded">
<div id="register">
<div class="paginate">Register</div>
<div class="paginate active">Personal Information</div>
<div class="paginate">Complete Registration</div>
<h1>User Registration</h1>
<p>Welcome to user registration! You can sign up yourself and start improving your business. User registration is FREE and once you register yourself you can select apropiate business package for your company and start advertising.<br />
<span style="color:#900">All field are required!</span></p>
<?php
echo $this->Form->create();
echo $this->Form->input('phone');
echo $this->Form->input('address');
echo $this->Form->input('city');
echo $this->Form->input('ptt', array('label'=>'Postal Code'));
echo $this->Form->input('state');
echo $this->Form->input('country');
echo $this->Form->end('Complete Registration');
?>
</div>
</div>
<!-- SIGN UP - 3 (COMPLETE) -->
<?php elseif($s == 'complete'): ?>
<div id="register" class="container padded">
<div id="register">
<div class="paginate">Register</div>
<div class="paginate">Personal Information</div>
<div class="paginate active">Complete Registration</div>
<h1>User Registration</h1>
<p>Welcome to user registration! You can sign up yourself and start improving your business. User registration is FREE and once you register yourself you can select apropiate business package for your company and start advertising.<br />
<span style="color:#900">All field are required!</span></p>
</div>
</div>
<? else: ?>
<div id="register" class="container padded">
<div id="register">
Unknown page!
</div>
</div>
<? endif; ?>
So as I said on first step it's all OK, it saves the user in DB but on second step I would like to put more information and when I submit it displays the Session flash message that User can not be saved, so that means that second user information is not saved in DB.
Please help!

Found solution. There was an error in validating. On second step it tried to validate Terms of Use checkbox. So here is the complete code of 3 STEP REGISTRATION:
USER CONTROLLER:
<?php
App::uses('Controller', 'Controller');
class UsersController extends AppController {
public $name = 'Users';
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('signup');
}
public function login(){
if($this->request->is('post')){
if($this->Auth->login()){
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Invalid username or password!', 'default', array('class' => 'errormsg'));
}
}
}
public function logout(){
$this->redirect($this->Auth->logout());
}
// SIGN UP ACTIONS
public function signup($step = null){
// SET STEP VARIABLE
$this->set('s', $step);
// STEP 1 - REGISTER
if(!$step){
$this->set('r', 1);
if ($this->request->is('post')) {
if ($this->User->save($this->request->data)) {
$this->Session->write('regUser', $this->request->data['User']['email']);
// $this->Session->setFlash('The user has been saved, please enter other information!',
// 'default', array('class' => 'okormsg'));
$regUser = $this->User->find( 'first', array('conditions' => array(
'User.username' => $this->Session->read('regUser'))));
$id = $regUser['User']['id'];
$this->Session->write('regUserId', $id);
$this->redirect(array('personal-info'));
} else {
$this->Session->setFlash( 'There was an error, please check the fields bellow!',
'default', array('class' => 'errormsg'));
}
}
// STEP 2 - PERSONAL INFORMATION
} elseif($step == 'personal-info') {
$id = $this->Session->read('regUserId');
$this->User->id = $id;
if ($this->request->is('post')) {
if ($this->User->save($this->request->data, array('validate' => false))) {
$this->redirect(array('complete'));
} else {
$this->Session->setFlash( 'User could not be saved. Please, try again.',
'default', array('class' => 'errormsg'));
}
}
// STEP 3 - COMPLETE REGISTRATION
} elseif($step == 'complete') {
// Send email function
}
}
}
USER MODEL:
<?php
class User extends AppModel {
public $validate = array (
'name'=>array(
'Please enter your name!'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter your name!'
)
),
'surname'=>array(
'Please enter your surname!'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter your surname!'
)
),
'email'=>array(
'Valid email'=>array(
'rule'=>array('email'),
'message'=>'Please enter a valid Email address!'
),
'Already exists'=>array(
'rule'=>'isUnique',
'message'=>'This Email is already registered in our database!'
)
),
'password'=>array(
'Not empty'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter your password!'
),
'Match password'=>array(
'rule'=>'matchPasswords',
'message'=>'Your passwords do not match!'
)
),
'password_confirm'=>array(
'Not empty'=>array(
'rule'=>'notEmpty',
'message'=>'Please confirm your password!'
)
),
'terms'=>array(
'Agree'=>array(
'rule'=>'notEmpty',
'required'=>true,
'message'=>'You must agree to Terms and conditions!'
)
)
);
public function matchPasswords($data){
if ($data['password'] == $this->data['User']['password_confirm']){
return true;
} else {
$this->invalidate('password_confirm', 'Your passwords do not match!');
return false;
}
}
public function beforeSave(){
if(!empty($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
if(!empty($this->data['User']['email'])){
if (empty($this->data['User']['username'])) {
$this->data['User']['username'] = $this->data['User']['email'];
}
}
}
}
?>
VIEW '/Users/signup.ctp'
<!-- SIGN UP STEPS -->
<!-- SIGN UP - 1 (REGISTER) -->
<?php if(empty($s)): ?>
<div id="register" class="container padded">
<div id="register">
<div class="paginate active">Register</div>
<div class="paginate">Personal Information</div>
<div class="paginate">Complete Registration</div>
<h1>User Registration</h1>
<p>Welcome to user registration! You can sign up yourself and start improving your business. User registration is FREE and once you register yourself you can select apropiate business package for your company and start advertising.<br />
<span style="color:#900">All field are required!</span></p>
<?php
echo $this->Form->create();
echo $this->Form->input('name');
echo $this->Form->input('surname');
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->input('password_confirm', array('label'=>'Password Confirmation','type'=>'password'));
echo $this->Form->input('terms', array('label'=>false, 'type'=>'checkbox'));
?> I accept Terms Of Use <?
echo $this->Form->end('Continue Registration');
?>
</div>
</div>
<!-- SIGN UP - 2 (PERSONAL INFO) -->
<?php elseif($s == 'personal-info'): ?>
<div id="register" class="container padded">
<div id="register">
<div class="paginate">Register</div>
<div class="paginate active">Personal Information</div>
<div class="paginate">Complete Registration</div>
<h1>User Registration</h1>
<p>This personal information are not required.<br />
<?php
echo $this->Form->create();
echo $this->Form->input('phone');
echo $this->Form->input('address');
echo $this->Form->input('city');
echo $this->Form->input('ptt', array('label'=>'Postal Code'));
echo $this->Form->input('state');
echo $this->Form->input('country');
echo $this->Form->end('Complete Registration');
?>
</div>
</div>
<!-- SIGN UP - 3 (COMPLETE) -->
<?php elseif($s == 'complete'): ?>
<div id="register" class="container padded">
<div id="register">
<div class="paginate">Register</div>
<div class="paginate">Personal Information</div>
<div class="paginate active">Complete Registration</div>
<h1>Congratulation!</h1>
<p>Your account has been created and <strong>Email</strong> has been send to your inbox. Please check you inbox and verify address by clicking on the link provided in mail.</p>
</div>
</div>
<? else: ?>
<div id="register" class="container padded">
<div id="register">
Unknown page!
</div>
</div>
<? endif; ?>
If someone wants to make registration with more steps, the rest is just the same!

You should consider splitting the steps into individual actions and views to make things simpler and easier to read/debug.
Step 1
Since you will create the User account at this point, you should automatically log in the user once saved and validated. Then you don't have to persist the information in the Session, which is bad in my experience.
An email can be sent to the user at this point from Model afterSave()
Step 2
I would move this to an action called "profile" which allows a user to update their additional profile information. Since they are already logged in you can easily find() the User and save their profile. This can be reused in the future too.
Step 3
This is just the "thank you" page from what I can see. You can redirect to the PagesController and render a simple thankyou.ctp rather than using the User Controller action which does nothing for the view.
The login can still happen even though their email address is not verified. You would only allow access to certain parts of the website until they have clicked the link sent to them in an email.

try checking $step as below.
if(!$step){
empty is used to check if array is empty or not.

Related

how to insert a form post data into database in yii 1 framework

this is my form in view
<div class="form">
<?php echo CHtml::errorSummary($model); ?>
<div class="row">
<?php echo CHtml::activeLabel($model,'Your Name'); ?>
<?php echo CHtml::activeTextField($model,'regname') ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'Your Password'); ?>
<?php echo CHtml::activePasswordField($model,'regpass') ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'Email Address'); ?>
<?php echo CHtml::activePasswordField($model,'regemail') ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'Contact'); ?>
<?php echo CHtml::activePasswordField($model,'regcontact') ?>
</div>
<div class="row submit">
<?php echo CHtml::submitButton('Login'); ?>
</div>
please help me to save this data from my controller to model
this is my model
//-- set Table
public function tableName(){
return 'user';
}
public function attributeLabels()
{
return array(
'username'=>'Your username for the game',
'password'=>'Your password for the game',
'email'=>'Needed in the event of password resets',
);
}
and this is my controller
public function actionRegister2()
{
$model=new RegisterForm;
if(isset($_POST['RegisterForm']))
{
echo 'done';
}
$this->render('register2',array(
'model'=>$model,
));
i an new in yii framework so didn't get a good tutorial for it, please suggest me how i insert the data into database by submitting a form
you can add this code in your controller:
public function actionRegister2()
{
$model=new RegisterForm;
if(isset($_POST['RegisterForm']))
{
$model->attributes = $_POST['RegisterForm'];
if ($model->save()) {
Yii::app()->user->setFlash('success', 'You have successfully added.');
$this->redirect(array('index'));
}
// or if(!$model->save()){ print_r($model->getErrors())}
}
$this->render('register2',array(
'model'=>$model,
));
}

Yii login from index and ajax login captcha error

I would like a user to be able to login to my app at site/index.php with an ajax request when the user provides a correct username and password the first time. If username or password is wrong the first time, it shows a captcha then if all inputs are correct don't show captcha error.
This my code:
loginForm
<?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 $username;
public $password;
public $rememberMe;
public $verifyCode;
private $_identity;
/**
* Declares the validation rules.
* The rules state that username and password are required,
* and password needs to be authenticated.
*/
public function rules()
{
return array(
array('username, password', 'required'),
array('rememberMe', 'boolean'),
array('password', 'authenticate'),
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(), 'on' => 'captchaNeed'),
);
}
/**
* Authenticates the password.
* This is the 'authenticate' validator as declared in rules().
*/
public function authenticate($attribute,$params)
{
if (!$this->hasErrors()) {
$this->_identity = new UserIdentity($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 === null) {
$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;
}
}
site/index
/**
* This is the default 'index' action that is invoked
* when ad action is not explicitly requested by users.
*/
public function actionIndex()
{
// set default layout
$this->layout = 'main';
$loginModel = new LoginForm;
if (Yii::app()->request->isAjaxRequest) {
if (isset(Yii::app()->session['captchaNeed'])) {
LoginModel->scenario = 'captchaNeed';
}
if (isset($_POST['LoginForm'])) {
$loginModel->attributes = $_POST['LoginForm'];
if ($loginModel->validate() && $loginModel->login()) {
echo '<script>window.location= "' . Yii::app()->createUrl('site/dashboard') . '"</script>';
unset(Yii::app()->session['captchaNeed']);
Yii::app()->end();
} else {
Yii::app()->session['captchaNeed'] = true;
$loginModel->scenario = 'captchaNeed';
}
$this->renderPartial('_login', array('model' => $loginModel));
}
}
if (!Yii::app()->request->isAjaxRequest) {
$this->render('index', array('loginModel' => $loginModel));
}
}
siteController actions
public function actions()
{
return array(
'captcha' => array(
'class' => 'CCaptchaAction',
'minLenght' => 4,
'maxLenght' => 4,
'testLimit' => 1,
'backColor' => 0x7D287E,
'foreColor' => 0xFFFFFF,
'height' => 24,
'width' => 100,
'offset' => 1,
),
);
}
view\site\index
<section class="col-xs-8" id="index-login">
<?php if (Yii::app()->user->isGuest) : ?>
<div id="login">
<?php $this->renderPartial('_login', array('model' => $loginModel)) ;?>
</div>
<?php echo CHtml::ajaxSubmitButton(
'Login',
Yii::app()->createUrl('site/index'),
array(
'type' => 'post',
'update' => '#login',
'data' => 'js:$("#login-form").serialize();
),
array(
'id'=>uniqid(),
'class'=>'btn btn-danger',
)
); ?>
<?php endif; ?>
</section>
view\site_login
<?php
/* #var $this SiteController */
/* #var $model LoginForm */
/* #var $form CActiveForm */
$this->pageTitle =Yii::app()->name . ' - Login';
$this->breadcrumbs = array('Login',);
?>
<h1>Login</h1>
<p>Please fill out the following form with your login credentials:</p>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
)
);?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username'); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
<p class="hint">Hint: You may login with <kbd>demo</kbd>/<kbd>demo</kbd> or <kbd>admin</kbd>/<kbd>admin</kbd>.
</p>
</div>
<?php if (CCaptcha::checkRequirements() && $model->scenario == 'captchaNeed'): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
</div>
<div class="hint">Please enter the letters as they are shown in the image above.
<br/>Letters are not case-sensitive.
</div>
<?php echo $form->error($model,'verifyCode'); ?>
</div>
<?php endif; ?>
<div class="row rememberMe">
<?php echo $form->checkBox($model,'rememberMe'); ?>
<?php echo $form->label($model,'rememberMe'); ?>
<?php echo $form->error($model,'rememberMe'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Login'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->

cakephp saving data into database

i am new in cake php.I want to insert form data into database.But could not inserted.please help
controller:
officetypesController.php
<?php
class OfficetypesController extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->Officetype->create();
if ($this->Officetype->save($this->request->data)) {
$this->Session->setFlash(__('The data has been saved'));
}
$this->Session->setFlash(
__('The data could not be saved. Please, try again.')
);
}
}
}
?>
view
add.ctp
<div class="users form">
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php echo $this->Form->input('name');
//echo $this->Form->input('password');
echo $this->Form->input('under', array(
'options' => array('1' => 'HO', '2' => 'RO')
));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
model
officetype.php
<?php
class Officetype extends AppModel {
}
database:
table name :officetypes ,
fields name :id,name,under
when i click submit button than showing the message "The data could not be saved. Please, try again."
public function add() {
if ($this->request->is('post')) {
$this->Officetype->create();
if ($this->Officetype->save($this->request->data)) {
$this->Session->setFlash(__('The data has been saved'));
} else {
$this->Session->setFlash(__('The data could not be saved. Please, try again.'));
}
}
}
Check the database to see if the records are added.
Why is the form related to users if you want to add office types?
<div class="office_type form">
<?php echo $this->Form->create('Officetype'); ?>
<fieldset>
<legend><?php echo __('Add Office type'); ?></legend>
<?php echo $this->Form->input('name');
echo $this->Form->input('under', array(
'options' => array('1' => 'HO', '2' => 'RO')
));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>

CakePHP Find Condition Breaking Submit

When using the conditions paramater of find, the user id doesn't submit along with the other fields.
public function add() {
if ($this->request->is('post')) {
$this->Thing->create();
if ($this->Thing->save($this->request->data)) {
$this->Session->setFlash(__('The Thing has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Thing could not be saved. Please, try again.'));
}
}
$users = $this->Thing->User->find('list', array(
'fields' => array('User.username'),
'conditions' => array('User.ownedby' => '3')
));
$this->set(compact('users'));}
Here is my view:
<div class="things form">
<?php echo $this->Form->create('Thing'); ?>
<fieldset>
<legend><?php echo __('Add Thing'); ?></legend>
<?php
echo $this->Form->input('user_id');
?>
<div id="user-info">
</div>
</fieldset>
<?php echo $this->Form->end(__('Submit'
)); ?>
</div>
However, when I omit the condition parameter, it submits just fine. Any ideas? In case you were wondering, I'm using the conditions parameter to sort the list of users that results in the select dropdown this creates.
Thanks.

Getting Last login time

In my application I want that when a user will login he/she can see his/her last login time just like when we do login in user module.So for doing like that that I just followed this link. So I made my UserIdentity code like this
<?php
/**
* UserIdentity represents the data needed to identity a user.
* It contains the authentication method that checks if the provided
* data can identity the user.
*/
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$user = User::model()->findByAttributes(array('username'=>$this->username));
if($user===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($user->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$user->id;
$this->setState('lastLoginTime', $user->lastLoginTime);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
$id=Yii::app()->user->id;
$lastLoginTime=Yii::app()->user->lastLoginTime;
}
}
And to show the last login time and user name I changed my view >> column2.php file like this
<?php $this->beginContent('//layouts/main'); ?>
<div class="container">
<div class="span-19">
<div id="content">
<?php echo $content; ?>
</div><!-- content -->
</div>
<div class="span-5 last">
<div id="sidebar">
<?php if(Yii::app()->user->id):?>
<span class="admin-message">Hello, <span><?php echo yii::app()->user->name;?> </span></span>
<?php echo Yii::app()->user->lastLoginTime;?>
<?php endif;?>
<?php
$this->beginWidget('zii.widgets.CPortlet', array(
'title'=>'Operations',
));
$this->widget('zii.widgets.CMenu', array(
'items'=>$this->menu,
'htmlOptions'=>array('class'=>'operations'),
));
$this->endWidget();
?>
<?php
if(Yii::app()->getModule('user')->isAdmin()):
$this->beginWidget('zii.widgets.CPortlet',array(
'title' => 'Admin Operations',
));
$this->widget('zii.widgets.CMenu', array(
'items'=>array(
array("label"=> "Create User", "url"=>array('/user/admin/create')),
array("label"=> "List User", "url"=> array('/user')),
array("label"=>"Manage Profile","url"=>array('/user/profile')),
array("label"=>"Manage Profile Fields","url"=>array('/user/profileField/admin')),
),
'htmlOptions'=>array('class'=>'operations'),
));
$this->endWidget(); ?>
<? endif;
?>
</div><!-- sidebar -->
</div>
</div>
<?php $this->endContent(); ?>
It is showing the username after login but when I want to check the last login time it is showing error like:
Property "CWebUser.lastLoginTime" is not defined. Can some one guide me how to do this?
Use
<?php echo Yii::app()->user->getState('lastLoginTime');?>
Don't know why not working.
I think it should work if it doesn't you can use session.
Yii::app()->session['lastLoginTime'] = $user->lastLoginTime;
echo Yii::app()->session['lastLoginTime'];

Categories