I am playing around with sessions to authenticate users and run functions. I am using sessions between two servers. I describe the process of what I am doing below.
1) With Server 1
pass username and password to the main Server 2
.
public function get_session_auth($username,$password){
$resultArray = array(
'result' => 'succeeded',
'resultText' => null
);
$request = new HTTP_Request2($this->baseUrl. 'index.php/login/get_session_id_via_login?username='.$username.'&password='.$password, HTTP_Request2::METHOD_GET);
if (ENVIRONMENT === 'development') {
$request->setConfig(array('ssl_verify_peer' => false));
}
//$request->setAdapter('curl');
try {
$response = $request->send();
if (200 == $response->getStatus()) {
$resultArray = json_decode($response->getBody(), true);
} else {
$resultArray['result'] = 'failed';
$resultArray['resultText'] = 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
// Here we should write data to log file.
$this->getLogger()->error($resultArray['resultText']);
}
} catch (HTTP_Request2_Exception $e) {
$resultArray['result'] = 'failed';
$resultArray['resultText'] = 'Error: ' . $e->getMessage();
// Here we should write data to log file.
$this->getLogger()->error($resultArray['resultText']);
}
$resultArray['operation_name'] = 'get_session_auth';
return $resultArray;
}
2) I login the user with username
and password
in Sever 2
and start a session and store information such as ( id, user_id, role = (Admin, Batch) )
in the $_SESSION
.
3) I get the session_id
and pass it back to Sever 2
$session_id = session_id();
return $session_id;
4) Now I call some functions in Server 2
using GET
from Server 1
and I also pass in the session_id
as a variable in the url
public function dosomething($session_id)
{
$resultArray = array(
'result' => 'succeeded',
'resultText' => null
);
$request = new HTTP_Request2($this->baseUrl. 'index.php/payment/dosomething/true?session_id='.$session_id, HTTP_Request2::METHOD_GET);
if (ENVIRONMENT === 'development') {
$request->setConfig(array('ssl_verify_peer' => false));
}
//$request->setAdapter('curl');
try {
$response = $request->send();
if (200 == $response->getStatus()) {
$resultArray = json_decode($response->getBody(), true);
} else {
$resultArray['result'] = 'failed';
$resultArray['resultText'] = 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
// Here we should write data to log file.
$this->getLogger()->error($resultArray['resultText']);
}
} catch (HTTP_Request2_Exception $e) {
$resultArray['result'] = 'failed';
$resultArray['resultText'] = 'Error: ' . $e->getMessage();
// Here we should write data to log file.
$this->getLogger()->error($resultArray['resultText']);
}
$resultArray['operation_name'] = 'dosomething';
return $resultArray;
}
5) I fetch the session_id
from the URL in Server 2
and then try to use
session_id( 'session_id' );
session_start();
6) The problem is that, I am expecting that the $_SESSION
variable will having the previously stored (id, user_id, role=(Admin,Batch)
but the $_SESSION` variable is empty even if the session_id is the same one where I stored those information before.
So how can I retrieve the information I stored in the first $_SESSION
?