mercredi 21 janvier 2015

How to use same SESSION in different Threads?

I developed a webpage. For some reason I need to use PHP threads. However I run into problem while implementing this issue. The webpage uses Session variables for the database connection. This is stored in $_SESSION['database']. The thread is implemented in the run method.



class SendDataToServer extends Thread{
public $curl;
/**
* Constructor
*/
public function __construct(){
$this->curl = curl_init();
}


public function run(){
if($_SESSION['database']->getidfromtable()>=1){
print "table has more than 1 id";
}
}

}


The thread is called with the following instructions in another file:



$sendtoserver = new SendDataToServer();
$sendtoserver->run();


This method works. But if I switch to the start method for the threads it won't work anymore:



$sendtoserver = new SendDataToServer();
$sendtoserver->start();


Then I receive the following error:


Fatal error: Call to a member function getidfromtable() on a non-object in /opt/lampp/htdocs/project/php/senddatatoserver.php on line 27


Sometimes I additionally receive this error message:


Notice: Undefined variable: _SESSION in /opt/lampp/htdocs/project/php/senddatatoserver.php on line 27


I tried also to include require_once session.php. But this had no effect.


I also tried to generate the database object in the senddatatoserver class. But then I receive the following error:


SQLSTATE[HY000] [14] unable to open database file


How can I use one session in both threads? Or if this is not possible how can I use the database in this session without any error?


Aucun commentaire:

Enregistrer un commentaire