为什么在Codeigniter我的会话丢失在其他页面?

时间:2017-11-11 15:48:04

标签: codeigniter session redirect

这是我第一次使用Codeigniter 3.x.在登录验证后,我在Login_model.php中设置了会话数据:

myBook.markDamaged('missing back cover');
在重定向之前

我可以通过以下方式访问我的会话数据:

  $data_session = array(
            'PersonId' => $PersonId,
            'PersonEmail' => trim($PersonEmail),
            'PersonName' => trim($PersonName),
            'PersonFamily' => trim($PersonFamily),
            'Login' => true);
        $this->session->set_userdata($data_session);

但重定向后我的会话数据是这样的:

print_r($this->session->userdata()); 



 Array([__ci_last_regenerate] => 1510383844   [PersonId] => 129[PersonEmail]=> h@q.com  [PersonName] => PersonName   [PersonFamily] => PersonFamily   [Login] => 1)

我在config.php中的设置是:

Array( [__ci_last_regenerate] => 1510384212)

我在这方面已经审查了很多事情。但我还是没有取得好成绩。

2 个答案:

答案 0 :(得分:0)

使用时

$config['sess_driver'] = 'files';

然后$config['sess_save_path']必须设置为存储会话文件的文件夹的绝对路径。

使用此文件夹结构

webroot/
   application/
   sessions/
   system/
   ...etc/

然后

`$config['sess_save_path'] = FCPATH."sessions/";`

将完美运作。

必须正确设置文件夹写入权限。

<强> 修改

您应该尝试设置$config['cookie_domain'] = '';(空字符串),如果这不起作用,请尝试$config['cookie_domain'] = '.your-domain.com';

以下控制器允许您测试会话是否正常工作。在controllers文件夹中创建名为Test_sessions.php的文件。 使用此代码。

<?php
/**
 * This class is useful for testing the configuration of the CodeIgniter "session" library.
 * It works only for CodeIgniter versions >= 3.0.0
 * 
 * If "session" is properly configured then each time you ask a browser for this page
 * the display will alternate between "No session data" and the value of $this->sess_message.
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Test_sessions extends CI_Controller
{
    /**
     * @var string So you can define your own message to be displayed.
     */
    public $sess_message;

    function __construct()
    {
        parent::__construct();
        // The next line can be commented out if 'session' is autoloaded - or not, doesn't matter.
        $this->load->library('session');  
        $this->sess_message = "We got session data!";
    }

    public function index()
    {
        echo date("F j, Y, g:i:s a")."<br>"; //so you can tell the page actually does refresh
        if($this->session->testing)
        {
            echo $this->session->testing;
            unset($_SESSION['testing']);
        }
        else
        {
            echo "No session data";
            $_SESSION['testing'] = $this->sess_message;
        }
    }

}

浏览至http://yoursite.com/test_sessions。如果正确配置了会话,则每次刷新页面时,它将在两个消息之间交替。

答案 1 :(得分:0)

我通过将配置更改为:

解决了我的问题
$config['sess_driver'] = 'files';$config['sess_cookie_name'] = 'ci_session';$config['sess_expiration'] = 86400;$config['sess_save_path'] = FCPATH.'sessions/';$config['sess_match_ip'] = FALSE;$config['sess_time_to_update'] = 3600;$config['sess_regenerate_destroy'] = TRUE;$config['cookie_prefix']    = '';$config['cookie_domain']   = '';$config['cookie_path']     = '/';$config['cookie_secure']  = FALSE;$config['cookie_httponly']  = FALSE;