I am trying to use Zend_Session_Namespace to help me with the task of changing a user's password. To this purpose, I need to store some values of their GET request when they first visit the reset page. I'd like to do that with Zend_Session_Namespace. Then, I want to update the user's password in their post request (from the same page) using the values I saved in Zend_Session_Namespace. The problem is the values saved in Zend_Session_Namespace are null when I access them. However, I can still do the job with the typical $_SESSION
variable. Does anyone know where I'm wrong? Am I missing some tip about using Zend_Session_Namespace? Do I need to use Zend_Registry or something to that effect?
My code for the action is as follows:
/**
* This handles password change
*/
public function changepasswordAction() {
$passwordForm = new Advancedsms_Form_ChangePassword();
$this->view->form = $passwordForm;
//If it is NOT post, the user has to provide recovery code and phone number
if(!$this->getRequest()->isPost()) {
$phone = filter_input(INPUT_GET, 'phone', FILTER_SANITIZE_URL);
$recovery = filter_input(INPUT_GET, 'recovery', FILTER_SANITIZE_URL);
$this->session= new Zend_Session_Namespace('RecoverySession');
$this->session->phone= $phone;
$this->session->recoveryCode = $recovery;
print_r($this->session);
$_SESSION['phone'] = $phone;
$_SESSION['recovery'] = $recovery;
}
if($this->getRequest()->isPost()) {
$params = $this->getRequest()->getParams();
print_r($params);
echo 'phone: ' . $this->session->phone .PHP_EOL;
echo 'recovery: ' . $this->session->recoveryCode . PHP_EOL;
echo $_SESSION['phone'] . ',' . $_SESSION['recovery'];
}
}
You need to move this line:
$this->session= new Zend_Session_Namespace('RecoverySession');
to before the if statements. At the moment your echo 'phone: ' . $this->session->phone
line will do nothing because $this->session
is not set on POST requests.
You should always write PHP code with the PHP error level at its most verbose, which would show warnings for issues like this.