Accessing the $_SESSION variable from pthreads - php

I'm having an issue accessing the session variable from functions called from Thread objects using the pthreads library for PHP.
When the function is called from the main thread, no errors occur and everything runs fine.
When run from a Thread object however, I get the following errors:
> PHP Notice: Undefined variable: _SESSION
> PHP Notice: Undefined index: Properties Manager
> PHP Fatal error: Call to a member function getGroupValue() on a non-object
The line numbers specified by the errors all point to this code block:
function connect_mysql_db($database, $write = false) {
$properties = $_SESSION['Properties Manager'];
if(!isset($database) || strlen($database)==0){
throw new Exception("No database specified");
}
// Read appropriate host, port, dbname, user & pass for this database
$host = $properties->getGroupValue($database, DB_HOST);
$port = $properties->getGroupValue($database, DB_PORT);
$db_name = $properties->getGroupValue($database, DB_NAME);
...Removed unnecessary code...
}
A little searching says that I should be able to remedy this issue by putting session_start(); at the top of my file.
After doing this, the other errors are still printed in addition to:
> PHP Notice: A session had already been started - ignoring session_start()
So my main question: Is there something special that I need to do when using pthreads in order to access the super-global session? Or is there something completely different at play here that I am missing?
Edit:
Yes, I have tried global $_SESSION; as well.

After a bit more research, it seems that the session variable is not thread safe and in fact locks entirely until the session is closed. I had to remove references to the session and simply pass the needed information to the function or reinitialize it.
Luckily there isn't a performance downfall of the property manager being created.

Related

Why isn't my mysqli_connect connecting? It says no hostname was specified when one is

I'm writing a simple blogging web app for my portfolio and I've come across this strange problem. I wrote a PHP script to connect to a database and read and write data to a database by RESTful calls.
I've done this before in other programs with no problems, but in this one I get an error. I wrote a simple test page to check that the RESTful calls would work before I started using them in my main app. Instead of working, I got an error back from my PHP script.
The error goes like this:
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): Can't connect to local MySQL server through socket '/No-MySQL-hostname-was-specified' (2) in /home/releesquirrel/storage.electricsquirrel.net/SimpleBlog/php/model_SimpleBlog.php on line 35
The code leading up to line 35 goes like this:
class model_SimpleBlog {
// Properties
// Database Info
private $serverName = "mysql.electricsquirrel.net";
private $userName = "esqrl_client";
private $userPassword = "fakepassword";
private $dbaseName = "esquirrel_simpleblog";
// Methods
public function model_SimpleBlog() {
//
}
// Load the ten latest entries after an offset
public function loadEntries($offset) {
$result = false;
// Connect to the Database Server
$connection = mysqli_connect($serverName, $userName, $userPassword, $dbaseName);
I've changed the password for privacy but that's the code that's throwing the error. I'm completely stumped. I've used code similar to this with no problems before, and I've tried googling the error code with no luck.
Does anybody know why I'm getting this error and what I can do to fix my code?
In PHP, you need to refer to object variables with $this->:
$connection = mysqli_connect($this->serverName, $this->userName, $this->userPassword, $this->dbaseName);
$serverName, for instance, looks for a variable with that name in the scope of the function. Obviously none exists.
See the manual for more information on PHP object properties.

Multiple NuSOAP clients causes "Undefined index: _transient" error

I'm working with 0.9.5 and I'm doing some phpunit tests.
When I execute my second test, that invokes again the webservice, I'm getting this error:
Undefined index: _transient
/var/www/dev_folder/nusoap/nusoap.php:227
/var/www/dev_folder/nusoap/nusoap.php:7293
when
$client = new nusoap_client($this->_config->URL_Path . $webserviceWSDL, true);
is executed by a second time.
I checked nusoap.php and seems something related with globals or something static or singleton... but I don't know what can I do to solve the problem...
$GLOBALS['_transient']['static']['nusoap_base']['globalDebugLevel'] = 9;
Need nusoap client to be unloaded or something like this? Why this global variable is failing?
Thank you.
I had the same problem. The comments seemed to indicate that the global variable was an attempt to emulate a static class variable, so I simply updated the code to actually use a static class variable in the nusoap_base class. That seemed to do the trick.
You can checkout the code here.

Invalid Memcache->connection member variable errors

We are currently using Nginx as our server and memcached for caching mechanism. I was inspecting PHP error logs and lots of PHP Warnings about memcached caught my attention.
PHP Warning: Memcache::get(): Invalid Memcache->connection member variable in {directory}/library/Zend/Cache/Backend/Memcached.php on line 180
At the line it was pointing, there is this piece of code:
$tmp = $this->_memcache->get($id);
I also see many other PHP warnings with the same warning message but different with different function calls of memcache object:
PHP Warning: Memcache::add(): Invalid Memcache->connection member variable in {directory}/library/Zend/Cache/Backend/Memcached.php on line 180
PHP Warning: Memcache::set(): Invalid Memcache->connection member variable in {directory}/library/Zend/Cache/Backend/Memcached.php on line 230
PHP Warning: Memcache::delete(): Invalid Memcache->connection member variable in {directory}/library/Zend/Cache/Backend/Memcached.php on line 323
I did a search through the web but could not find anything that really helped. From time to time, we have some problems with our memcached. Is it possible that this some kind of issue that happens when servers are down because of some problem? I really do not have any idea about what causes these warnings. How can I correct it or at least how can I avoid these warnings?
I found 3 references that might help you
Limit of file system and memcache
Possible memory leaks
Memcache vs Memcached
You need to check key max 250 chars and value max : 1MB
Have you compiled your own php recently? It's possible ther versions are out of sync.
I had the same problem.
when i called memcache object in __destruct to update the state of my object i goot the error.
and here is my solution.:
call object in your class function where you change the state and be sure to send an instance of memcache to this class.
While from the warning message itself, it may not be obvious, but the error may be happening when you're trying to serialize/deserialize memcache connection object itself.
For example:
class a {
private Memcache $mc;
private $name = 'glen';
public function __construct(Memcache $mc) {
$this->mc = $mc;
}
}
$a = new a($mc);
$mc->set('a', $a);
You very likely (as me) ended up here because class having mixed concerns (the object being a model and has as other business logic as well). you can omit the unwanted mc key from serializing using __debugInfo function:
public function __debugInfo() {
return [];
}
https://www.php.net/manual/en/language.oop5.magic.php#object.debuginfo
While writing this note, I'm not able to reproduce with my own example, so there's something else involved, perhaps memory corruption. But removing $mc property solve the problem for me.

Problems with a login php script - passing objects through a session

I have an index.php page that creates a new Login class (login class does all the handling of data,creating session, redirecting, etc)
index.php I create a new login class
require_once('login.class.php');
$login = new Login;
Login constructor looks like this
public function __construct(){
// Start session and open a database connection
session_start();
$this->connectToDB();
}
if the user logs in successfully, I redirect him to securePage.php.
if I do the following on the securePage.php
$test = $_SESSION ['usrData'];
var_export($test->getFirstName());
var_export($test->getLastName());
var_export($test->isAuthorized());
it displays the following error
Fatal error: Call to a member function getFirstName() on a non-object
in /login/securePage.php on line
17
When, however, I put
$login = new Login;
in front of
var_export($test->getFirstName());
var_export($test->getLastName());
var_export($test->isAuthorized());
It works! I dont' know what gives and if I am doing something wrong because even when I put
session_start();
instead of
$login = new Login;
but it still gives me the error
Fatal error: Call to a member function getFirstName() on a non-object
in /login/securePage.php on line
16
Could be a couple of things:
-You have to do a session_start() at the top of any page that uses sessions.
-With a serialized object (an object stored in a session variable), the object class needs to have been included anywhere it is being referenced.
Looks more likely to be that you didn't session_start()?
Also resource variables (database handles, file handles, etc) won't survive the serialization/deserialization that happens with session variables.
I think it has to do with the variable name. If I am not wrong, in your securePage.php, here is the code:
$login = $_SESSION ['usrData'];
var_export($login);
var_export($test->getFirstName());
It should be:
$login = $_SESSION ['usrData'];
var_export($login);
var_export($login->getFirstName());
Hope it helps.
It would be good practice to close the session before issuing the redirect (although I'd expect its unlikely that the session is not being written until after the browser requests the new page).
What does the line 'var_export($login);' within securePage.php generate? (comment out the calls which are causing the fatal error).

Cannot redeclare the function error

function getContactActiveEmails($eid)
{
global $db;
if ($eid) {
$sql = "SELECT email FROM activeEmails WHERE id = $eid";
return $db->GetCol($sql);
}
}
I get the error "Cannot redeclare function getContactActiveEmails"
The line number it gives is the last line of the function - the }
All files are being called with require_once. This is the only place in the entire codebase where getContactActiveEmails is being defined. Why is this?
It is very clear from the error your function is defined twice hence you are getting the error.
I would recommend that check if the function is already defined before declaring it.
if (!function_exists('getContactActiveEmails'))
{
function getContactActiveEmails($eid)
{
global $db;
if ($eid) {
$sql = "SELECT email FROM activeEmails WHERE id = $eid";
return $db->GetCol($sql);
}
}
}
solution by #Shakti Singh will work, but keep in mind that you are loosing control of your code - you do not know where is this function declared and what does it return, so I suggest looking for it
Try case insensitive search, many text editors and IDEs search case-sensitive by default and your function can be as well declared as getcontactactiveemails somewhere.
If still no luck make php say something about this function, using Reflection extension
Example usage of reflection:
if(function_exists('getContactActiveEmails')){
$myfunc = new ReflectionFunction('getContactActiveEmails');
echo 'Function is declared in '.$myfunc->getFileName().
' starting from line '.$myfunc->getStartLine().
' to '.$myfunc->getEndLine();
die;
}
More about Reflection
I'm getting the same issue. I have a standard file called adhoc.inc.php which is imported into almost every php file on my site. With no changes being made to any code over night i started seeing the error
[13-Jul-2013 21:19:22 Australia/Sydney] PHP Fatal error: Cannot redeclare checkloggedin() in /Applications/MAMP/htdocs/mycobber/util/adhoc.inc.php on line 4
Initially I only got it in a few files so i just commented out this import and it worked. Suddenly, again no changes, I was getting this in every file i loaded. I figured this wasn't me so I restarted my MAMP servers (apache and mysql) and then it went away.
Has anyone seen this before?
This error occurs if you have your function defined in a loop, since you're trying to define it in each iteration.

Categories