php 在 Codeigniter 中在哪里设置会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29289421/
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 00:20:21  来源:igfitidea点击:

Where to set session in Codeigniter?

phpcodeigniter

提问by Vegan Sv

I'm reading the Codeigniter 2.2 tutorial and I am not clear on how to use sessions for logging in.

我正在阅读 Codeigniter 2.2 教程,但不清楚如何使用会话进行登录。

Say I have a login.php, which checks user data with the database. Then if its ok then I should set the session in a controller?

假设我有一个 login.php,它用数据库检查用户数据。然后,如果没问题,那么我应该在控制器中设置会话吗?

  $this->load->library('session');

And then in say admin.php page I should check if session exists by? :

然后在 admin.php 页面中,我应该检查会话是否存在?:

 $this->session->user_data('item'); ??

Or how do I check if the person is logged in?

或者我如何检查该人是否已登录?

Thank you

谢谢

回答by justanotherprogrammer

Based on the docs, to do anything custom within session you need to load the session library. If you plan to use session throughout your application, I would recommend autoloadingthe library. You do this from within config/autoload.php.

根据文档,要在会话中执行任何自定义操作,您需要加载会话库。如果您打算在整个应用程序中使用 session,我建议您自动加载库。您可以在 config/autoload.php 中执行此操作。

$autoload['libraries'] = array('session');

$autoload['libraries'] = array('session');

Then you won't have to use $this->load->library('session');on every page.

这样您就不必$this->load->library('session');在每个页面上都使用。

After the library is loaded, set your custom information, maybe based off some information from your database. So in your case, this would be in login.php:

加载库后,设置自定义信息,可能基于数据库中的一些信息。所以在你的情况下,这将在 login.php 中:

$this->session->set_userdata('userId', 'myId');where userIdwould be the name of the session variable, and myIdwould be the value.

$this->session->set_userdata('userId', 'myId');whereuserId是会话变量的名称,myId是值。

Then, on subsequent pages (admin.php), you could check that the value is there.

然后,在后续页面 (admin.php) 上,您可以检查该值是否存在。

if($this->session->userdata('userId') == '') { //take them back to signin }

if($this->session->userdata('userId') == '') { //take them back to signin }

回答by VVLeon

To set user session

设置用户会话

$the_session = array("key1" => "value1", "key2" => "value2");
$this -> session -> set_userdata($the_session);

To read user session

读取用户会话

$foo = $this -> session -> userdata('key1');

You need $this->load->library('session');every time prior you use CI session functions. Or you can set it up in autoload.php $autoload['libraries'] = array('session');

您需要$this->load->library('session');每次使用 CI 会话功能之前。或者您可以在 autoload.php 中进行设置$autoload['libraries'] = array('session');

回答by Siddharth Shukla

  1. load session library

    $this->load->library('session');
    
  2. set session

    $_SESSION['email'] = $data['email'];
    
  3. unset session

    $this->session->unset_userdata($_SESSION['email']); // $this->session->sess_destroy();
    
  1. 加载会话库

    $this->load->library('session');
    
  2. 设置会话

    $_SESSION['email'] = $data['email'];
    
  3. 未设置会话

    $this->session->unset_userdata($_SESSION['email']); // $this->session->sess_destroy();
    

回答by Siddharth Shukla

$this->load->library('session');
$this->session->userdata("email")