Session_set_save_handler not setting

左心房为你撑大大i 提交于 2019-12-05 16:43:32

Setting the session save handler fails:

session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc")

Because these callbacks you want to register to do not exists:

var_dump(is_callable("sess_open")); # FALSE

That is because your object methods needs to be properly registered as callbacks. An object method callback is written in form of an array with two elements, the first one is the object, the second one a string of the methodname. Example from PHP net that is similar to yours:

$handler = new FileSessionHandler();
session_set_save_handler(
    array($handler, 'open'),
    array($handler, 'close'),
    array($handler, 'read'),
    array($handler, 'write'),
    array($handler, 'destroy'),
    array($handler, 'gc')
);

As you can see, each method is written as a single array with the first element $handler always.

From within the class you can use $this to refer to the same object. But before your fully write your own, check the session_set_save_handler() PHP manual page for infos, examples and user contributed notes. There are different ways how you can organize that for your case.

If you use the function inside the constructor then you need to pass in $this like so:

session_set_save_handler(
    array($this, 'sess_open'),
    array($this, 'sess_close'),
    array($this, 'sess_read'), 
    array($this, 'sess_write'),
    array($this, 'sess_destroy'),
    array($this, 'sess_gc')
);

And then instantiate the class

new SessionClass;

Whenever you are in doubt you can always take a look at the documentation. Be sure to read the comments as well; they are usually very helpful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!