Giter Site home page Giter Site logo

Predis/PHP namespace issue about predis HOT 12 CLOSED

predis avatar predis commented on June 10, 2024
Predis/PHP namespace issue

from predis.

Comments (12)

nrk avatar nrk commented on June 10, 2024

Hi and sorry for the late response.
I don't think this is an issue related to Predis per se, but could you create a gist with a code snippet so that I can test your issue in my environment?

Thanks,
Daniele

from predis.

 avatar commented on June 10, 2024

Hi Daniele,

Thanks for replying.

This code works fine:

require "Predis.php";

$single_server = array(
'host'     => '127.0.0.1',
'port'     => 6379,
'database' => 15
);

$test = array( 'some' => 'value' );

var_dump( $test );

$redisConnection = new \Predis\Client($single_server);
$redisConnection->set('test', serialize( $test) );

var_dump( unserialize( $redisConnection->get('test' ) ) );

whereas this code (adapted and simplified from https://github.com/ivanstojic/redis-session-php/blob/master/redis-session.php) does not work

require "Predis.php";

$redisServer = array(
'host'     => '127.0.0.1',
'port'     => 6379,
'database' => 15
);

$redisTargetPrefix = "session:php:";

session_set_save_handler(
'redis_session_open',
'redis_session_close',
'redis_session_read',
'redis_session_write',
'redis_session_destroy',
'redis_session_gc');

function redis_session_read($id) {
  global $redisServer, $redisTargetPrefix;

  $redisConnection = new \Predis\Client($redisServer);
  return base64_decode($redisConnection->get($redisTargetPrefix . $id));
}

function redis_session_write($id, $data) {
  global $unpackItems, $redisServer, $redisTargetPrefix;

  $redisConnection = new \Predis\Client($redisServer);
  $ttl = ini_get("session.gc_maxlifetime");

}

function redis_session_destroy($id) {
  global $redisServer, $redisTargetPrefix;

  $redisConnection = new \Predis\Client($redisServer);
  $redisConnection->del($redisTargetPrefix . $id);
}

function redis_session_open($path, $name) {}
function redis_session_close() {}
function redis_session_gc($age) {}

session_start();

$_SESSION['animal']   = 'cat';

I get the following line in Apache's error log:

PHP Fatal error:  Class 'Predis\\Client' not found in /var/www/xslt/testPredis3.php on line 31

from predis.

nrk avatar nrk commented on June 10, 2024

Hi jerje,

no luck so far with trying to reproduce this issue. I've tested your script on ArchLinux with PHP 5.3.4 and MacOS X with PHP 5.3.3 (both using the suhosin patch installed as a module) but I couldn't get any error out of it and the session handler works as expected.

Do you have any update on your side?

from predis.

 avatar commented on June 10, 2024

Hi Daniele,

Apologies for the delay in replying.

I upgraded Ubuntu and php -v is returning

PHP 5.3.3-1ubuntu9.1 with Suhosin-Patch (cli) (built: Oct 15 2010 14:17:04)

but I am still getting the same error. I had a look around to see if anyone had
reported similar bugs but couldn't find anything.

I'll try it on some other machines to see if I can narrow it down.

from predis.

nrk avatar nrk commented on June 10, 2024

Don't worry, no problem.
In the next few days I will try on an Ubuntu virtual machine with Apache to replicate the same environment, and see if I can get the same error out of it.

from predis.

rquigley avatar rquigley commented on June 10, 2024

I can confirm the same error on CentOS 5.5 with self-compiled PHP 5.3.5 and suhosin patch using the above test code. I don't get the error using MacPorts PHP 5.3.5 with suhosin on my dev machine.

I believe the error occurs because session_write_close is not called when your script ends, but after all of your objects have been destroyed. You can fix (temporarily) the issue by calling session_write_close() at the end of your script.

More here: http://us.php.net/session_set_save_handler

from predis.

nrk avatar nrk commented on June 10, 2024

@rquigley thanks, I will try also with CentOS and the same configuration.

from predis.

Seldaek avatar Seldaek commented on June 10, 2024

As an alternative you can try to add session_write_close() into the __destruct() method of the redis client class for example.

from predis.

nrk avatar nrk commented on June 10, 2024

@Seldaek: maybe a more clean solution could consist in adding a shutdown callback in the script that contains the code for the session handler:

function session_shutdown() {
    session_write_close();
}

register_shutdown_function('session_shutdown');

@Jerje: you might want to try the above mentioned tricks, but can you also try to alias \Predis\Client to see if the problem can be somewhat related to namespacing issues? It's highly unlikely, but still...

use \Predis\Client as Predis_Client;

Unfortunately I still hadn't the time to test this under CentOS :-(

from predis.

nrk avatar nrk commented on June 10, 2024

Any news? I wasn't able to reproduce it and I'm pretty sure this is a bug in certain older versions of PHP that has been since resolved.

I will leave this issue open for a few days, then I will close it if no further feedback is provided.

from predis.

 avatar commented on June 10, 2024

Hey,

Sorry again for the delay in replying.

I retried this and as nrk pointed out it seems to be a bug in older versions of PHP.

I just checked on Ubuntu and using PHP 5.3.2-1ubuntu4.7 it seems to work just
fine.

Thanks for all the feedback.

from predis.

nrk avatar nrk commented on June 10, 2024

Thanks for the follow up, jerje.
It's reassuring to know that it was a bug of PHP after all.

from predis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.