Reputation: 31
PHP session are not working properly. Values in the session are first stored in get_values_from_email()
. But I cannot see the values of the var_dump($this->session->get())
inside the function change_password()
printed on the screen. If I perform the var_dump($this->session->get())
at the beginning of the script then I am able to retrieve session the values.
At the end of the function change_password()
the session $this->session->get('email')
is always empty at the end of the function, but outputs the a value if I echo the session at the beginning of the function. I am a newbie here and I would like to know what I am doing wrong with sessions. Thanks!
<?php
namespace App\Controllers;
use App\Models\Password_recovery_model;
use App\Validation;
class Password_recovery extends BaseController
{
public function __construct()
{
$this->session = \Config\Services::session();
}
public function index(){
return view('password_recovery');
}
public function get_values_from_email(){
// var_dump($this->session->get());
$uri = new \CodeIgniter\HTTP\URI(current_url());
if(!empty($uri->getSegment(4)) && !empty($uri->getSegment(5))){
$email_code = $uri->getSegment(4);
$email = $uri->getSegment(5);
//stored session values
$this->session->set('email_code', $email_code);
$this->session->set('email',$email);
return view('change_password');
}
}
public function change_password(){
$request = \Config\Services::request();
$userModel = new Password_recovery_model();
$password_changed = false;
$password =$request->getJsonVar('password');
$validation_code =$request->getJsonVar('validation_code');
$validation = \Config\Services::validation();
if(!$validation->withRequest($this->request)->run()){
$val_arr_p=$validation->getErrors();
if(isset($val_arr_p['password'])&& !empty($val_arr_p['password'])){
$data = array("error"=> $val_arr_p['password']);
return $this->response->setJSON($data);
}
// var_dump($this->session->get());
}else{
if(!empty($this->session->get('email'))){
$email = $this->session->get('email');
}
if(!empty($email)){
// $password_changed = $userModel->change_password($validation_code,$email,$password);
$data = array("message"=> "Su contraseña ha sido cambiada exitosamente");
return $this->response->setJSON($data);
}else{
// $email = $session->get('email');
if(isset($_SESSION['email'])){
$email = $_SESSION['email'];
echo $_SESSION['email_code'].$_SESSION['email'] ;
}
$data = array("message"=> "No email variable222");
return $this->response->setJSON($data);
}
}
//confirmed email
if($password_changed){
$data = array("message"=> "Su correo electrónico ha sido enviado exitosamente. Puede demorar hasta 5 minuton en llegar.");
}else{
$data = array("message"=> "Hubo un error porfavor trate de nuevo.");
}
}
public function send_email_to_user_to_change_password(){
$request = \Config\Services::request();
$email =$request->getJsonVar('email');
$userModel = new Password_recovery_model();
if(!empty($email)){
//generates validation code
$email_validation_code = $userModel->email_validation_code();
//sends code to email
$send_e = $userModel->send_email_to_change_password($email);
if($send_e){
$data = array("message"=> "Su correo electrónico ha sido enviado exitosamente para recuperar su contraseña. El mismo puede tardar hasta 5 minutos en llegar. ");
return $this->response->setJSON($data);
}
else{
$data = array("message"=> "Hubo un problema. Trate nuevamente");
return $this->response->setJSON($data);
}
}
}
public function pass_generator(){
$userModel = new Password_recovery_model();
$pgen = $userModel->random_str();
$data = array("gen_p"=> $pgen);
header('Content-Type: application/json');
$this->response->setHeader('Cache-Control', 'no-cache')->appendHeader('Cache-Control', 'must-revalidate');
return $this->response->setJSON($data);
}
}
?>
I am expecting the $this->session->get('email')
session variable to have a value.
Upvotes: -1
Views: 412
Reputation: 1
Declare a variable above the constructor to use the service, but this service already exists by default in the BaseController
class Password_recovery extends BaseController
{
public $session;
public function __construct()
{
$this->session = \Config\Services::session();
}
}
Upvotes: 0