how to create a simple login function
I'm designing database for Yii web application and I'm not sure I'm doing it right way.
CREATE TABLE IF NOT EXISTS `users` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`level` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`level` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
and
INSERT INTO table users, Example:
INSERT INTO users (username , password, level) VALUES ('sam',md5('sam'),1);
INSERT INTO users (username , password, level) VALUES ('jan',md5('jan'),2);
INSERT INTO users (username , password, level) VALUES ('bud',md5('bud'),2);
You should be taken to the login screen, where you just enter the Gii password (established in the configuration file), and click Enter. Assuming you entered the correct password, you’ll see a splash page and a list of options (as links).
INSERT INTO users (username , password, level) VALUES ('jan',md5('jan'),2);
INSERT INTO users (username , password, level) VALUES ('bud',md5('bud'),2);
You should be taken to the login screen, where you just enter the Gii password (established in the configuration file), and click Enter. Assuming you entered the correct password, you’ll see a splash page and a list of options (as links).
- Enter users as the Table Name.
- Enter User as the Model Class. You’ll notice that the form automatically copies the table name as the Model name.
- Click Preview. You’ll see a table appear at the bottom of the form, indicating the files to be generated (just one in this case).
- Click Generate.
open the model in user.php and add the script like this:
//valadation
protected function afterValidate() {
parent::afterValidate();
//melakukan enkripsi pada passwod yang di input
$this->password = $this->encrypt($this->password);
}
//encrypt
public function encrypt($value){
return md5($value);
}
protected function afterValidate() {
parent::afterValidate();
//melakukan enkripsi pada passwod yang di input
$this->password = $this->encrypt($this->password);
}
//encrypt
public function encrypt($value){
return md5($value);
}
/**
* 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;
/**
* Authenticates a user.
* The example implementation makes sure if the username and password
* are both 'demo'.
* In practical applications, this should be changed to authenticate
* against some persistent user identity storage (e.g. database).
* @return boolean whether authentication succeeds.
*/
public function authenticate()
{
$model = new User;
$user= $model->findByAttributes(array('username'=>$this->username));
if($user===null){
$this->errorCode=self::ERROR_USERNAME_INVALID;
}else{
if($user->password !== $user->encrypt($this->password)){
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}else{
$this->_id = $user->id;
$this->errorCode=self::ERROR_NONE;
}
}
return !$this->errorCode;
}
public function getId() {
return $this->_id;
}
}
Finally, Test:
open your browser, example : http://localhost/cobaweb/index.php?r=site/login/
Finish, congratulations



Tidak ada komentar:
Posting Komentar