php 为什么呼叫会话不起作用?(代码点火器 3)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37458436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:14:52  来源:igfitidea点击:

Why call session not working? (codeigniter 3)

phpmysqlcodeignitersessioncodeigniter-3

提问by moses toh

my config is like this :

我的配置是这样的:

$config['sess_driver'] = 'database';  // select database driver
$config['sess_save_path'] = 'ci_sessions';  // name of the mysql table
$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = FALSE;
$config['sess_time_to_update']  = 300;

My autoload is like this :

我的自动加载是这样的:

$autoload['libraries'] = array('database', 'session', 'form_validation', 'functions');

In controller login.php:

在控制器login.php 中

$this->session->set_userdata('club', 'chelsea');

In controller dashboard.php:

在控制器dashboard.php 中

echo $this->session->userdata('club');

It did not succeed in calling the session club if different controller

如果不同的控制器,它没有成功调用会话俱乐部

But, when I call session club in the same controller(controller login.php), it's working

但是,当我在同一个控制器(控制器login.php)中调用 session club 时,它正在工作

I try $this->library->load('session');in the constructor of Dashboard.php. But it's not success

我尝试$this->library->load('session');Dashboard.php. 但这不是成功

Any solution to solve my problem?

有什么解决方案可以解决我的问题?

Thank you

谢谢

采纳答案by DFriend

What the answer from @Ali suggests will work or load the session library in the constructor of Dashboard.phplike so...

@Ali 建议的答案将在构造函数中工作或加载会话库Dashboard.php...

class Dashboard extends CI_Controller
{
  public function __construct()
  {
    parent::__construct();
    $this->load->library('session');
  }

  //the rest of controller's code
}

Or if you only need sessions in one particular function inside the controller load the library in that function instead.

或者,如果您只需要控制器内一个特定函数中的会话,请改为在该函数中加载库。

The point is that the library has to be loaded somewhere before you can make calls to its functions.

关键是必须先将库加载到某个地方,然后才能调用其函数。

The choice of autoloading or explicitly loading session in a controller might depend on how often your site needs to use sessions. If every controller uses them then chose autoload. If sessions are needed in only a few places then it is a waste of resources to load them where they are not used.

在控制器中选择自动加载或显式加载会话可能取决于您的站点需要使用会话的频率。如果每个控制器都使用它们,则选择自动加载。如果会话只在少数地方需要,那么在不使用它们的地方加载它们是一种资源浪费。

回答by Joyie

I meet the same problem , then I check the code in Session.php and find that :

我遇到了同样的问题,然后我检查了 Session.php 中的代码,发现:

if (isset($_COOKIE[$this->_config['cookie_name']])
        && (
            !is_string($_COOKIE[$this->_config['cookie_name']])
            OR !preg_match('/^[0-9a-f]{40}$/', $_COOKIE[$this->_config['cookie_name']]))
){
    unset($_COOKIE[$this->_config['cookie_name']]);
} 

but the cookie is only 32 length ,so 'ci' create a new cookie everytime.

但是cookie只有32个长度,所以'ci'每次都会创建一个新的cookie。

I change

我改变

  OR !preg_match('/^[0-9a-f]{40}$/', $_COOKIE[$this->_config['cookie_name']])

to

  OR !preg_match('/^[0-9a-f]{32}$/', $_COOKIE[$this->_config['cookie_name']])

then everything is ok !

然后一切正常!

回答by Fadian Adhitya Nugraha

In CodeIgniter 3.x if you just upgraded to PHP version 7.x, go to system/libraries/Session/session.phpat line 281 and replace :

在 CodeIgniter 3.x 中,如果您刚刚升级到 PHP 7.x 版,请转到system/libraries/Session/session.php第 281 行并替换:

ini_set('session.name', $params['cookie_name']); 

by :

经过 :

ini_set('session.id', $params['cookie_name']);

回答by Rayees Pk

For working session, It is important to set encryption key in config file. You can set any keywords as encryption key. Use difficult strings it improve session security. An example given below $config['encryption_key'] = '4n9*^%%$3n^&4v&%7@!90&$$3c3x$^*$m8a456#@tgf%$$c';

对于工作会话,在配置文件中设置加密密钥很重要。您可以将任何关键字设置为加密密钥。使用困难的字符串可以提高会话安全性。下面给出的示例 $config['encryption_key'] = '4n9*^%%$3n^&4v&%7@!90&$$3c3x$^*$m8a456#@tgf%$$c';

回答by user4662441

By default codeigniter 3 uses file drivers, so you should to create a directory like:

默认情况下,codeigniter 3 使用文件驱动程序,因此您应该创建一个目录,例如:

mkdir /<path to your application directory>/sessions/
chmod 0700 /<path to your application directory>/sessions/
chown www-data /<path to your application directory>/sessions/

Codeigniter info

代码点火器信息

Before, you should to update config.php with path directory:

之前,您应该使用路径目录更新 config.php:

$config['sess_save_path'] = 'sessions';

Bonus: If you are using macOSX you should look what it your apache user:

奖励:如果您使用的是 macOSX,您应该查看您的 apache 用户:

$ cat /etc/apache2/httpd.conf
[...]
User _www <-----
Group _www <----
[...]

回答by elddenmedio

I downloaded the latest version of CI 3.0.6, and when i compare the session part in the path application/config/config.php you have

我下载了 CI 3.0.6 的最新版本,当我比较路径 application/config/config.php 中的会话部分时

$config['sess_driver']          = 'database';  // select database driver
$config['sess_save_path']       = 'ci_sessions';  // name of the mysql table
$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = FALSE;
$config['sess_time_to_update']  = 300;

and the original file has (with your info)

并且原始文件具有(带有您的信息)

$config['sess_driver']             = 'database';
$config['sess_cookie_name']        = 'ci_session';
$config['sess_expiration']         = 7200;
$config['sess_save_path']          = 'ci_session';
$config['sess_match_ip']           = FALSE;
$config['sess_time_to_update']     = 300;
$config['sess_regenerate_destroy'] = FALSE;

After that you are right in the autoload file application/config/autoload.php

之后你就在自动加载文件 application/config/autoload.php 中

$autoload['libraries'] = array('database', 'session', 'form_validation', 'functions');

Then in one controller whe make this

然后在一个控制器中制作这个

class Example extends CI_Controlelr{
    public function __construct(){
        parent::__construct();
    }

    public function index(){
        if( $this->session->has_userdata('test_name')){
            $this->session->set_flashdata('exist_userdata', 'The session was seted before');

            redirect(base_url('index.php/example2'), 'refresh');
        }
        else{
            $newdata = array(
                                'test_name' => 'name',
                                'test_id'   => 12
                            );

            $this->session->set_userdata($newdata);

            $this->session->set_flashdata('set_userdata_ok', 'You set your user data');

            redirect(base_url('index.php/example2'), 'refresh');
        }
    }
}

applicationi/controller/Example2.php

applicationi/控制器/Example2.php

class Example2 extends CI_Controller{
    public function __construct(){
        parent::__construct();
    }

    public function index(){
        $this->load->view('example/index');
    }
}

application/views/example/index.php

应用程序/视图/示例/index.php

<div>
    <?php
        if( isset( $this->session->flashdata('set_userdata_ok'))){
            echo $this->session->flashdata('set_userdata_ok');
        }
        if( isset( $this->session->flashdata(exist_userdata))){
            echo $this->session->flashdata('exist_userdata');
        }
    ?>
</div>
<br>
<div>
    <?php
        echo "printing general";
        var_dump($_SESSION);
        echo "<hr>";
        var_dump($this->session->userdata());
        echo "<hr><hr>";
        echo "printing spesific data";
        echo $_SESSION['test_name'];
        echo $this->session->test_name;
        echo "<hr>";
        echo $this->session->userdata('test_name');
    ?>
</div>

Hope this can helpyou debugging the session library, if you have more problems, sen the result in every echo i make in view

希望这可以帮助您调试会话库,如果您有更多问题,请在我查看的每个回声中发送结果

回答by Ali

You need to load session library by default using application/config/autoload.php

默认情况下,您需要使用加载会话库 application/config/autoload.php

https://www.codeigniter.com/user_guide/libraries/sessions.html

https://www.codeigniter.com/user_guide/libraries/sessions.html