Auto Login

Hey,

I have database where only login name, no password field. I need auto login, no need to submit login.php. When user access my site just check login name, if have permission then go to main page, else go to register page. How can I do this?

Thanks

Take a look at the blog example in the yii distribution and omit the password validation.

Hello

I omit the password validation. I need to login just check the login name automatically and go to the home page if user have no permission then go to register page. Just tell me where i need to change.

Thanks

default is




public $ loginUrl = array ( 'site / login');



sets the value to ‘site/register’ using http://www.yiiframework.com/doc/api/1.0.11/CWebUser#__set-detail

no tested

I don’t want to submit login page. If any user go to my site, database automatically check the username, if he has permission( loginName) then go to main page otherwise redirect to register page. I think I need to change the CWebUser.php page or if you have any idea plz let me know.

in your controller you should check if a user is registered then redirect to the desired page




if(Yii::app()->user->isGuest)

{

    $this->redirect(Yii::app()->user->loginUrl);

}else{

    $this->redirect(array('/main/page'));

}



Your main/page should check if user is guest, if user is guest then they should be redirected to the login page.

Extend CUserIdentity::authenticate() in your UserIdentity class eg:




class MyUserIdentity extends CUserIdentity

{

        public function __construct($username)

        {

               $this->username = $username;

        }


	public function authenticate()

	{

		$record = User::model()->findByAttributes(array('username'=>$this->username));

		if($record===null)

		{

			$this->errorCode=self::ERROR_USERNAME_INVALID;

		}else{

			$this->errorCode=self::ERROR_NONE;

		}

		return !$this->errorCode;

	}

}



After you validate your login form, you should then call your user identity class like $result = new MyUserIdentity(YOUR USERNAME VALUE);

if $result->errorCode === UserIdentity::ERROR_NONE

then log the user in by Yii::app()->user->login($result,LENGTH OF SESSION);

Hello,

I did it. My question is, I don’t want to submit Login form. It will check automatically (if user already in the database) when User browse my site then go to main page otherwise go to register page.

At some point, you need to enter the username to check it against the database i.e login.

$_SERVER[PHP_AUTH_USER] ??

Yes you re right. I m using Global login and I know the variable name $testName[5]. I want to check this $testName5 with my own database. If that Global login user ( $testName[5]) has access then go to main page otherwise redirect to register page.

Hi Neel,

you might be able to create a base controller, overide the beforeAction function with login command. And modify all other controller to extend your base controller.

This is the approach that I used when creating this facebook connect authentication.

But, be warned, I just learn PHP several weeks ago. My solution might not the good one…

hope this help,

~Holly~