用户
 找回密码
 入住 CI 中国社区
搜索
查看: 5416|回复: 16
收起左侧

[版本 3.x] CI3.1 Session类取不到值的问题

[复制链接]
发表于 2016-10-7 22:07:07 | 显示全部楼层 |阅读模式
CI初学者一枚。写了一个登录后生成session,检查session的小例子。因为用的是php5.4,所以之前加载session会报错,在论坛里面找到的解决方法是将CI的Session类中的session_set_save_handler($class, TRUE); 代码注释:                // Configuration ...
                $this->_configure($params);

                $class = new $class($this->_config);
                if ($class instanceof SessionHandlerInterface)
                {
                        if (is_php('5.4'))
                        {
                                //session_set_save_handler($class, TRUE);
                        }
                        else
                        {
                                session_set_save_handler(
                                        array($class, 'open'),
                                        array($class, 'close'),
                                        array($class, 'read'),
                                        array($class, 'write'),
                                        array($class, 'destroy'),
                                        array($class, 'gc')
                                );

                                register_shutdown_function('session_write_close');
                        }
                }

修改后不再报错,可是又出现了新的问题。Session里面的值取不到。

这个是登录成功就生成Session的方法:  
function checkLogin() {
        if (!empty($_POST['sub']) && !empty($_POST['uname'])
            && !empty($_POST['upwd'])) {
            //查询返回满足条件下的指定字段
            $this->load->model("testDBModel");
            $username = $_POST['uname'];
            $userpwd  = $_POST['upwd'];

            $arr_wdata = array("uname"=>$username, "upwd"=>$userpwd);
            //查询用户表
            $arr_res = $this->testDBModel->query_table("wenshu_user","uid,uname,upermission",$arr_wdata);
            if (!empty($arr_res))
            {
                //加载session类
                $this->load->library('session');
                //创建session类,生成Session
                $arr_sessiondata = array('uid'=>$arr_res[0]->uid);  
                $this->session->set_userdata($arr_sessiondata);  
                //查看当前session
                echo "current session id is: ".$this->session->userdata('uid');   //页面调用这个方法,此时这里是可以取到值的
            }
            else {
                echo "用户名或密码错误。请重新输入!";
            }
        }
    }

同一个页面,修改URL执行下面的方法时就取不到session的值了:
    function checkSession() {
        $this->load->library('session');
        echo "tesss: ".$this->session->userdata('uid');    //页面打印:  tesss:
        //userdata方法。如果指定的session存在则返回对应的session值
        if ($this->session->userdata('uid')){
            echo "当前用户已登录";
        }else {
            echo "当前用户没有登录";
        }

    }



请论坛里面的大神们帮忙看下是因为什么原因呢? 跪谢各位了!!



 楼主| 发表于 2016-10-8 13:10:34 | 显示全部楼层
Hex 发表于 2016-10-8 12:40
500 错误是 PHP 报错了,要把 display_errors 打开看具体错误才能帮你解决。

CI的错误日志,没有ERR: <?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

INFO - 2016-10-08 12:57:12 --> Config Class Initialized
INFO - 2016-10-08 12:57:12 --> Hooks Class Initialized
DEBUG - 2016-10-08 12:57:12 --> UTF-8 Support Enabled
INFO - 2016-10-08 12:57:12 --> Utf8 Class Initialized
INFO - 2016-10-08 12:57:12 --> URI Class Initialized
INFO - 2016-10-08 12:57:12 --> Router Class Initialized
INFO - 2016-10-08 12:57:12 --> Output Class Initialized
INFO - 2016-10-08 12:57:12 --> Security Class Initialized
DEBUG - 2016-10-08 12:57:12 --> Global POST, GET and COOKIE data sanitized
INFO - 2016-10-08 12:57:12 --> Input Class Initialized
INFO - 2016-10-08 12:57:12 --> Language Class Initialized
INFO - 2016-10-08 12:57:12 --> Loader Class Initialized
INFO - 2016-10-08 12:57:12 --> Controller Class Initialized
INFO - 2016-10-08 12:57:12 --> File loaded: D:\WWW\Zend\workspaces\CI_Test\One\application\views\testloginview.php
INFO - 2016-10-08 12:57:12 --> Final output sent to browser
DEBUG - 2016-10-08 12:57:12 --> Total execution time: 0.4510
INFO - 2016-10-08 12:57:37 --> Config Class Initialized
INFO - 2016-10-08 12:57:37 --> Hooks Class Initialized
DEBUG - 2016-10-08 12:57:37 --> UTF-8 Support Enabled
INFO - 2016-10-08 12:57:37 --> Utf8 Class Initialized
INFO - 2016-10-08 12:57:37 --> URI Class Initialized
INFO - 2016-10-08 12:57:37 --> Router Class Initialized
INFO - 2016-10-08 12:57:37 --> Output Class Initialized
INFO - 2016-10-08 12:57:37 --> Security Class Initialized
DEBUG - 2016-10-08 12:57:37 --> Global POST, GET and COOKIE data sanitized
INFO - 2016-10-08 12:57:37 --> Input Class Initialized
INFO - 2016-10-08 12:57:37 --> Language Class Initialized
INFO - 2016-10-08 12:57:37 --> Loader Class Initialized
INFO - 2016-10-08 12:57:37 --> Controller Class Initialized
INFO - 2016-10-08 12:57:37 --> Model Class Initialized
INFO - 2016-10-08 12:57:37 --> Database Driver Class Initialized
DEBUG - 2016-10-08 12:57:39 --> Session: "sess_save_path" is empty; using "session.save_path" value from php.ini.
INFO - 2016-10-08 12:57:39 --> Session: Class initialized using 'files' driver.
INFO - 2016-10-08 12:57:39 --> Final output sent to browser
DEBUG - 2016-10-08 12:57:39 --> Total execution time: 2.0771
INFO - 2016-10-08 12:57:50 --> Config Class Initialized
INFO - 2016-10-08 12:57:50 --> Hooks Class Initialized
DEBUG - 2016-10-08 12:57:50 --> UTF-8 Support Enabled
INFO - 2016-10-08 12:57:50 --> Utf8 Class Initialized
INFO - 2016-10-08 12:57:50 --> URI Class Initialized
INFO - 2016-10-08 12:57:50 --> Router Class Initialized
INFO - 2016-10-08 12:57:50 --> Output Class Initialized
INFO - 2016-10-08 12:57:50 --> Security Class Initialized
DEBUG - 2016-10-08 12:57:50 --> Global POST, GET and COOKIE data sanitized
INFO - 2016-10-08 12:57:50 --> Input Class Initialized
INFO - 2016-10-08 12:57:50 --> Language Class Initialized
INFO - 2016-10-08 12:57:50 --> Loader Class Initialized
INFO - 2016-10-08 12:57:50 --> Controller Class Initialized
DEBUG - 2016-10-08 12:57:50 --> Session: "sess_save_path" is empty; using "session.save_path" value from php.ini.
INFO - 2016-10-08 12:57:50 --> Session: Class initialized using 'files' driver.
INFO - 2016-10-08 12:57:50 --> Final output sent to browser
DEBUG - 2016-10-08 12:57:50 --> Total execution time: 0.0460
INFO - 2016-10-08 12:58:08 --> Config Class Initialized
INFO - 2016-10-08 12:58:08 --> Hooks Class Initialized
DEBUG - 2016-10-08 12:58:08 --> UTF-8 Support Enabled
INFO - 2016-10-08 12:58:08 --> Utf8 Class Initialized
INFO - 2016-10-08 12:58:08 --> URI Class Initialized
INFO - 2016-10-08 12:58:08 --> Router Class Initialized
INFO - 2016-10-08 12:58:08 --> Output Class Initialized
INFO - 2016-10-08 12:58:08 --> Security Class Initialized
DEBUG - 2016-10-08 12:58:08 --> Global POST, GET and COOKIE data sanitized
INFO - 2016-10-08 12:58:08 --> Input Class Initialized
INFO - 2016-10-08 12:58:08 --> Language Class Initialized
INFO - 2016-10-08 12:58:08 --> Loader Class Initialized
INFO - 2016-10-08 12:58:08 --> Controller Class Initialized
INFO - 2016-10-08 12:58:08 --> File loaded: D:\WWW\Zend\workspaces\CI_Test\One\application\views\testloginview.php
INFO - 2016-10-08 12:58:08 --> Final output sent to browser
DEBUG - 2016-10-08 12:58:08 --> Total execution time: 0.1720
INFO - 2016-10-08 12:58:09 --> Config Class Initialized
INFO - 2016-10-08 12:58:09 --> Hooks Class Initialized
DEBUG - 2016-10-08 12:58:09 --> UTF-8 Support Enabled
INFO - 2016-10-08 12:58:09 --> Utf8 Class Initialized
INFO - 2016-10-08 12:58:09 --> URI Class Initialized
INFO - 2016-10-08 12:58:09 --> Router Class Initialized
INFO - 2016-10-08 12:58:09 --> Output Class Initialized
INFO - 2016-10-08 12:58:09 --> Security Class Initialized
DEBUG - 2016-10-08 12:58:09 --> Global POST, GET and COOKIE data sanitized
INFO - 2016-10-08 12:58:09 --> Input Class Initialized
INFO - 2016-10-08 12:58:09 --> Language Class Initialized
INFO - 2016-10-08 12:58:09 --> Loader Class Initialized
INFO - 2016-10-08 12:58:09 --> Controller Class Initialized
INFO - 2016-10-08 12:58:09 --> File loaded: D:\WWW\Zend\workspaces\CI_Test\One\application\views\testloginview.php
INFO - 2016-10-08 12:58:09 --> Final output sent to browser
DEBUG - 2016-10-08 12:58:09 --> Total execution time: 0.0340
INFO - 2016-10-08 12:58:14 --> Config Class Initialized
INFO - 2016-10-08 12:58:14 --> Hooks Class Initialized
DEBUG - 2016-10-08 12:58:14 --> UTF-8 Support Enabled
INFO - 2016-10-08 12:58:14 --> Utf8 Class Initialized
INFO - 2016-10-08 12:58:14 --> URI Class Initialized
INFO - 2016-10-08 12:58:14 --> Router Class Initialized
INFO - 2016-10-08 12:58:14 --> Output Class Initialized
INFO - 2016-10-08 12:58:14 --> Security Class Initialized
DEBUG - 2016-10-08 12:58:14 --> Global POST, GET and COOKIE data sanitized
INFO - 2016-10-08 12:58:14 --> Input Class Initialized
INFO - 2016-10-08 12:58:14 --> Language Class Initialized
INFO - 2016-10-08 12:58:14 --> Loader Class Initialized
INFO - 2016-10-08 12:58:14 --> Controller Class Initialized
INFO - 2016-10-08 12:58:14 --> Model Class Initialized
INFO - 2016-10-08 12:58:14 --> Database Driver Class Initialized
DEBUG - 2016-10-08 12:58:15 --> Session: "sess_save_path" is empty; using "session.save_path" value from php.ini.


页面的错误信息:
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at  admin@phpStudy.net to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.

 楼主| 发表于 2016-10-8 14:37:04 | 显示全部楼层
Hex 发表于 2016-10-8 14:15
CI 的错误日志看不出来,要 PHP LOG,要具体 PHP 报错信息。

我用的是phpstudy集成环境。修改了php.ini里面的display_errors = On,log_errors = On,error_log = d:\www\logs,重启了Apach。但是在d:\www\logs这个目录下没有生成任何文件。   求教,应该怎么弄?
 楼主| 发表于 2016-10-8 12:43:16 | 显示全部楼层
Hex 发表于 2016-10-8 12:40
500 错误是 PHP 报错了,要把 display_errors 打开看具体错误才能帮你解决。

我看你们之前的帖子也有人遇到同样的问题说是$class有问题,屏蔽这句代码就可以解决。具体错误稍等下哈
发表于 2016-10-8 12:37:44 | 显示全部楼层
session_set_save_handler($class, TRUE);  这个报什么错?你的 CI 版本是 3.x 吗?
建议,不了解就不要随意改 CI 里的源码,一般来说不会是 CI 的 BUG。
 楼主| 发表于 2016-10-8 12:39:24 | 显示全部楼层
Hex 发表于 2016-10-8 12:37
session_set_save_handler($class, TRUE);  这个报什么错?你的 CI 版本是 3.x 吗?

我用的是最新的CI 3.1 ,php是5.4。就是报的500 Internal Server Error错误
发表于 2016-10-8 12:40:11 | 显示全部楼层
annt007 发表于 2016-10-8 12:39
我用的是最新的CI 3.1 ,php是5.4。就是报的500 Internal Server Error错误

500 错误是 PHP 报错了,要把 display_errors 打开看具体错误才能帮你解决。
发表于 2016-10-8 14:15:33 | 显示全部楼层
CI 的错误日志看不出来,要 PHP LOG,要具体 PHP 报错信息。
发表于 2016-10-8 14:41:39 | 显示全部楼层
annt007 发表于 2016-10-8 14:37
我用的是phpstudy集成环境。修改了php.ini里面的display_errors = On,log_errors = On,error_log = d:\ ...

你是用的 IE 浏览器么?
 楼主| 发表于 2016-10-8 14:43:38 | 显示全部楼层
Hex 发表于 2016-10-8 14:41
你是用的 IE 浏览器么?

是的

本版积分规则