How to match new_password and confirm_password fields when they are not in database?
Hi... I would like to know how I would be able to match my fields "new_password" and "confirm_password", they are not stored in database they are just used for matching purpose in Change Password module.
I tried this but it didn't worked:
if($this->data['User']['new_password'] != $this->data['User']['confirm_password'] ) {
$this->Session->setFlash("New password and Confirm password field do not match");
} else {
$this->data['User']['password'] = $this->data['User']['new_password'];
$this->data['User']['id'] = $this->User->id;
if($this->User->save($this->data)) {
$this->Session->setFlash("Password updated");
$this->redirect('/users/login');
}
}
You can try this:
// check for empty value
if( !empty( $this->data['User']['new_password'] ) && !empty( $this->data['User']['confirm_password'] ) ) {
if( $this->data['User']['new_password'] != $this->data['User']['confirm_password']) {
$this->Session->setFlash("New password and Confirm password field do not match");
} else {
$this->data['User']['password'] = $this->data['User']['new_password'];
$this->data['User']['id'] = $this->User->id;
if( $this->User->save($this->data) ) {
$this->Session->setFlash("Password updated");
$this->redirect('/users/login');
} else {
$this->Session->setFlash('Password update fail');
}
}
} else {
$this->Session->setFlash('Enter New password and Confirm password');
}
Related
I'm trying to get the data in the array that came from another function(that function is extracting the data in the csv file) and when i tried calling the two fields from that array it shows an error that it is unidentified variables.
The $this->csv_process(); as shown on the function action() is the function that extracts the data from the csv file and stores it in an array which is successful since I tried checking it on var_dump();
I also named the two fields as $name and $email as shown below:
Function CSV_process()
public function csv_process()
{
/* variables for openning the csv file */
if (!in_array($extension, $allowed_ext)) {
$this->session->set_flashdata("message", "Sorry, CSV file only.");
} else {
if ($filesize > 0) {
$file = fopen($filename, "r");
$toWrite = array();
$error = false;
$col_size = 2;
$skip = 0;
while ($data = fgetcsv($file, 10000, ","))
{
$skip++;
if ($skip == 1) {
continue;
}
$numofcol = count($data);
if ($numofcol != $col_size ) {
$this->session->set_flashdata("message", "Column count exceeded or missing.");
} else {
$name1 = $data[0];
$name = str_replace("'", "''", $name1);
$email1 = $data[1];
$email = str_replace("'", "''", $email1);
$toWrite[] = [
'name' => $name,
'email' => $email
];
}
}
}
}
return $toWrite;
}
Function Action()
function action(){
$toWrite[] = $this->csv_process();
foreach ($toWrite as $arr) {
list($name, $email) = $arr;
//die(var_dump($arr));
$query = $this->db->query("SELECT * FROM import WHERE name ='$name' AND email = '$email'");
if ($query->num_rows() >= 1) {
} else {
if ($name == "" OR $email == "") {
} else {
if ((filter_var($email, FILTER_VALIDATE_EMAIL)) == FALSE ) {
} else {
$this->db->query("INSERT INTO import(name, email, created_date) VALUES('".$name."', '".$email."', '".date("Y-m-d h-i-s")."')");
$this->session->set_flashdata('message', 'SUCCESS YEAY');
redirect('Clean_csv/index');
}
}
}
$query->free_result();
}
}
Listing arrays doesn't seem to work for here, anyone knows how to extract the data array from $arr?
You don't need to extract the values. You can use each $arr in a bound query. It simplifies the syntax for the select query.
For inserting use CodeIgniter's insert() method. Again, the $arr can be used directly by adding the date to it before the insert is attempted.
I think this will work.
function action()
{
$toWrite[] = $this->csv_process();
foreach($toWrite as $arr)
{
$query = $this->db->query("SELECT * FROM import WHERE name=? AND email=?", $arr);
if($query->num_rows() >= 1)
{}
else
{
if($arr['name'] == "" OR $arr['email'] == "")
{}
else
{
if((filter_var($email, FILTER_VALIDATE_EMAIL)) == FALSE)
{}
else
{
$arr['created_date'] = date("Y-m-d h-i-s");
$this->db->insert("import", $arr);
$this->session->set_flashdata('message', 'SUCCESS YEAY');
//??? redirect('Clean_csv/index');
//Are you sure, you may still have more $arr in $toWrite to process - right?
}
}
}
$query->free_result();
}
}
You need to know what a terrible idea it is to repeatedly run database queries inside a loop. Even though you use free_result() it could be a massive drain on server resources. If your csv file has several thousand items you are severely stressing the database and the server.
Yii does not save to db in $register. My aim is to make saving into db and logging a user if he is not there yet or just log in in if he is there.
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if(isset($_POST['LoginForm'])){
$users = User::find()->where("username = '".$_POST['LoginForm']['username']."'")->count();
if ($users == '0'){
$register = new User();
$register->username = 'lak';
$register->save();
}
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
}
}
return $this->render('login', [
'model' => $model,
]);
}
could be you have some fails i validation rules
if this the try using
if ($users == '0'){
$register = new User();
$register->username = 'lak';
$register->save(false);
}
if with $register->save(false); the values is saved then check better for your validation rules ..
I am using the github repository code here: https://github.com/hunzinker/CakePHP-Auth-Forgot-Password
I have used the following function in my UsersController.php. I get the error Undefined index: token on the line that has a comment before it. What should I change?
/**
* Allow user to reset password if $token is valid.
* #return
*/
function reset_password_token($reset_password_token = null) {
if (empty($this->data)) {
$this->data = $this->User->findByResetPasswordToken($reset_password_token);
if (!empty($this->data['User']['reset_password_token']) &&
!empty($this->data['User']['token_created_at']) &&
$this->__validToken($this->data['User']['token_created_at'])
) {
$this->data['User']['id'] = null;
$_SESSION['token'] = $reset_password_token;
} else {
$this->Session->setflash(
'The password reset request has either expired or is invalid.'
);
$this->redirect('/users/login');
}
} else {
//ERROR ON THE NEXT LINE HERE UNDEFINED INDEX: TOKEN
if ($this->data['User']['reset_password_token'] != $_SESSION['token']) {
$this->Session->setflash(
'The password reset request has either expired or is invalid.'
);
$this->redirect('/users/login');
}
$user = $this->User->findByResetPasswordToken(
$this->data['User']['reset_password_token']
);
$this->User->id = $user['User']['id'];
if ($this->User->save($this->data, array('validate' => 'only'))) {
$this->data['User']['reset_password_token'] =
$this->data['User']['token_created_at'] = null;
if ($this->User->save($this->data) &&
$this->__sendPasswordChangedEmail($user['User']['id'])
) {
unset($_SESSION['token']);
$this->Session->setflash(
'Your password was changed successfully. Please login to continue.'
);
$this->redirect('/users/login');
}
}
}
}
You need to be sure that $_SESSION contains this index, so you should update it like this in order to be sure it exists:
By this:
if (!isset($_SESSION['token']) || $this->data['User']['reset_password_token'] != $_SESSION['token']) {
$this->Session->setflash(
'The password reset request has either expired or is invalid.'
);
$this->redirect('/users/login');
}
How to update user information stored in auth session? without logout and login again.
I think this function will do it.. but is it the best-practice?
function update($field, $value){
$this->Session->write($this->Auth->sessionKey . '.' . $field, $value);
}
Yes.
You could grab the current info array, modify it, and then call $this->Auth->login($newUserData);, but this will also renew the session (no user interaction needed, though). Note: Applies to CakePHP 2.0+ only.
I've completed update function to get an array of new values. with keys (field name):
public function update($fields, $values = null) {
if (empty(parent::$_user) && !CakeSession::check(parent::$sessionKey)) {
return false;
}
if (!empty(parent::$_user)) {
$user = parent::$_user;
} else {
$user = CakeSession::read(parent::$sessionKey);
}
if (is_array($fields)) {
if (is_array($values)) {
$data = array_combine($fields, $values);
} else {
$data = $fields;
}
} else {
$data = array($fields => $values);
}
foreach ($data as $field => $value) {
if (isset($user[$field])) {
$user[$field] = $value;
}
}
return $this->login($user);
}
(thanks to tigrang for login function)
I use OpenID Simple Registration on CakePHP like this post but this don't work for me.
my code in controller is here:
function openid() {
$returnTo = 'http://'.$_SERVER['SERVER_NAME'].$this->webroot.'customers/openid';
if (!empty($this->data)) {
try {
$this->Openid->authenticate($this->data['OpenidUrl']['openid'], $returnTo, 'http://'.$_SERVER['SERVER_NAME'].$this->webroot, array('email'), array('nickname'));
} catch (InvalidArgumentException $e) {
$this->setMessage($this->data);
} catch (Exception $e) {
$this->setMessage($e->getMessage());
}
}
elseif (count($_GET) > 1) {
$response = $this->Openid->getResponse($returnTo);
$this->set("test",$_GET);
if ($response->status == Auth_OpenID_CANCEL) {
$this->Session->write('OpenIdStatus','Verification cancelled');
} elseif ($response->status == Auth_OpenID_FAILURE) {
$this->Session->write('OpenIdStatus','OpenID verification failed: '.$response->message);
} elseif ($response->status == Auth_OpenID_SUCCESS) {
$this->Session->write('OpenIdStatus','successfully authenticated!');
$sregResponse = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
$sregContents = $sregResponse->contents();
$this->set("message",$sregContents);
}
}
}
What is my wrong?!
The authenticate method only expects four params in the current version. The following snippet:
$this->Openid->authenticate($this->data['OpenidUrl']['openid'], $returnTo, 'http://'.$_SERVER['SERVER_NAME'].$this->webroot, array('email'), array('nickname'));
has to look like:
$this->Openid->authenticate($this->data['OpenidUrl']['openid'], $returnTo, 'http://'.$_SERVER['SERVER_NAME'].$this->webroot, array('sreg_required' => array('email'), 'sreg_optional' => array('nickname')));