I wanted to make a persistent session for the user using a class, but it seems that I can't make it work. Currently, I have this:
<?php
class Session {
private $sessionId = NULL;
public function __construct() {
session_start();
if($this->sessionId === NULL)
$this->sessionId = session_id();
return $this->sessionId;
}
public function getSessionValue($field) {
return $_SESSION[$field];
}
public function setSessionValue($field, $data) {
$_SESSION[$field] = $data;
}
public function removeSessionValue($field) {
unset($_SESSION[$field]);
}
public function destroySession() {
$this->sessionId = NULL;
session_destroy();
}
public function __toString() {
return $this->sessionId;
}
}
?>
But it happens that when I redirect the user to another domanin so he can make a payment, he loses its session when he's back to my website. Currently, I save some data with setSessionValue('paymentid', 1)
method from this class, redirect using javascript and, when it comes back, I have this code to check if he's got a session value:
$session = new Session();
if($session->getSessionValue('paymentid') !== null) {
// Do something here
}
Which returns me an empty paymentid. The strange thing is that sometimes it works, but it doesn't all the time, so... What could be wrong with this?