/    Sign up×
Community /Pin to ProfileBookmark

password encryption in cakephp

Hi,

I am making a user registration and login system in cakephp without using Auth component.

The registration is working fine except the password field.Its not getting encrypted while inserting in the db.

Registration View:

[code=php]<?php
// create the form inputs
echo $form->input(‘fname’, array(‘label’=>’First Name: *’));

echo $form->input(‘lname’, array(‘label’=>’Last Name: *’));

echo $form->input(‘username’, array(‘label’=>’Username: *’));
echo $form->input(‘password’, array(‘label’=>’Password: *’));
echo $form->input(’email’, array(‘label’=>’Email: *’));
?>[/code]

User model :

[code=php]
var $validate = array(
// name field
‘fname’ => array(
// must not be empty
‘rule’ => VALID_NOT_EMPTY,
// error message to display
‘message’ => ‘Please enter First Name’
),
‘lname’ => array(
// must not be empty
‘rule’ => VALID_NOT_EMPTY,
// error message to display
‘message’ => ‘Please enter Last Name’
) ,
‘username’ => array(
‘rule-emp’ => array(
‘rule’ => VALID_NOT_EMPTY,
‘message’ => ‘Please enter username’ ,
‘last’ => true
),

‘isUnique’ => array(
‘rule’ => ‘isUnique’,
‘required’ => true,
‘message’ => ‘Username is already registered’
)
) ,
‘password’ => array(
‘rule-1’ => array(
‘rule’ => VALID_NOT_EMPTY,
‘message’ => ‘Please enter password’ ,
‘last’ => true
),
‘rule-2’ => array(
‘rule’ => array(‘between’, 5, 15),
‘message’ => ‘Pasword should be between 5 and 15 characters’
)
,
‘rule-3’ => array(
‘rule’ => ‘alphaNumeric’,
‘message’ => ‘Pasword should be characters and numerics only’
)
),
’email’ => array(

‘rule-1’ => array(
‘rule’ => VALID_NOT_EMPTY,
‘message’ => ‘Please enter email’ ,
‘last’ => true
),
‘rule-2’ => array(
‘rule’ => ’email’,
‘message’ => ‘Incorrect email id’
),
‘isUnique’ => array(
‘rule’ => ‘isUnique’,
‘required’ => true,
‘message’ => ’email is already registered’
)
)

);[/code]

User controller :

[code=php]function register() {
// set layout file
$this->pageTitle = ‘Video Site registration’;
$this->layout = ‘view’;

// if the form data is not empty
if (!empty($this->data)) {

$this->User->create();

if ($this->User->save($this->data)) {
// set a flash message
$this->Session->setFlash(‘The user has been saved’, ‘flash_good’);

$this->redirect(array(‘action’=>’register’));
} else {
// set a flash message
$this->Session->setFlash(‘The user could not be saved. Please, try again.’, ‘flash_bad’);
}

}

}[/code]

When I am writing this line

$this->data[‘User’][‘password’] = md5($this->data[‘User’][‘password’] ); just after the $this->User->create(); then the validation for password (between 5 and 15) is not working.

Please suggest me what should I do now?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiAug 18.2009 — You need to encrypt the password using the beforeSave method in the User model if you want to apply validation to the unencrypted version. You should also use the core security library to hash the password since it will use the application's salt. I don't know why you don't just use Auth though, it does all of this for you as well as taking care of sessions etc in a secure way.
Copy linkTweet thisAlerts:
@raj_2006authorAug 19.2009 — I have put this following code inside the user model.

[code=php]function beforeSave() {
$this->data['User']['password'] = md5($this->data['User']
['password']);
return true;
} [/code]


Now its work fine....But also I whave to understand how the Auth component works.

Can you please suggest me a good tutorial for user registration/login using auth.
Copy linkTweet thisAlerts:
@MindzaiAug 19.2009 — Read the manual, it's all there:

http://book.cakephp.org/view/172/Authentication

You are also probably going to find that code produces unexpected results since you are not checking that the password value actually exists in the data array. You are also still not using the Security component to hash the password.
×

Success!

Help @raj_2006 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...