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 and 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 AppControllers;
use AppModelsPassword_recovery_model;
use AppValidation;
$validation = ConfigServices::validation();
class Password_recovery extends BaseController
{
public function __construct()
{
$this->session = ConfigServices::session();
// session_destroy();as
}
public function index(){
return view('password_recovery');
}
public function get_values_from_email(){
$session = session();
$uri = new CodeIgniterHTTPURI(current_url());
if(!empty($uri->getSegment(4)) && !empty($uri->getSegment(5))){
$email_code = $uri->getSegment(4);
$email = $uri->getSegment(5);
$this->session->set('email_code', $email_code);
$this->session->set('email',$email);
return view('change_password');
}
}
public function change_password(){
var_dump($this->session->get());
$request = ConfigServices::request();
$userModel = new Password_recovery_model();
$password_changed = false;
$password =$request->getJsonVar('password');
$validation_code =$request->getJsonVar('validation_code');
$validation = ConfigServices::validation();
$validation->setRules(
[
"password" =>'required|max_length[255]|min_length[12]|callback_to_verify_password_c|callback_to_verify_password_l|callback_to_verify_password_s|callback_to_verify_password_n'
],
[
'password' => [
'required' =>"Contraseña es requerida",
'max_length' =>"Contraseña no puede ser mayor de 255 caracteres",
'min_length' =>"Contraseña tiene que ser de 12 carácteres o mayor",
'callback_to_verify_password_c'=>"Contraseña tiene que tener una letra mayúscula",
'callback_to_verify_password_l'=>"Contraseña tiene que tener una letra minúscula",
'callback_to_verify_password_s'=>"Contraseña tiene que tener uno de los siguientes simbolos #$%^&()_+[{}]|:;<>,.?",
'callback_to_verify_password_n'=>"Contraseña tiene que tener un número del 0 al 9"
]
]
);
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);
}
}else{
if(!empty($this->session->get('email'))){
echo $email = $this->session->get('email');
}
var_dump($this->session->get());
if(!empty($email)){
$data = array("message"=> "Su contraseña ha sido cambiada exitosamente");
return $this->response->setJSON($data);
}else{
if(isset($_SESSION['email'])){
$email = $_SESSION['email'];
echo $_SESSION['email_code'].$_SESSION['email'] ;
}
$data = array("message"=> "No email variable");
return $this->response->setJSON($data);
}
}
view("change_password");
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.");
}
//return $this->response->setJSON($data);
}
}
I am expecting the $this->session->get(’email’) session variable to have a value.