不在页面之间携带的codeigniter会话

时间:2014-09-13 00:25:29

标签: php codeigniter session cookies

有人在这里熟悉CodeIgniter会话吗? 我有一个功能登录脚本。成功登录后,将创建会话。用户被重定向到他们的主页。新页面应显示存储在会话变量中的用户名,但根本不显示任何内容。

登录脚本中的代码:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

    /**
     * Index Page for this controller.
     */
    public function index() {
        $this->load->library('session');
        $BB_DATA = array();
        if(isset($_POST['login'])) {
            // SELECT username, password FROM Bacon or bacon-like product. <-- Fuck you #nobanchancom
            if(!empty($_POST['username']) && !empty($_POST['password'])) {
                $q = sprintf('SELECT user_pk, username, pwhash, salt FROM tbl_user WHERE `username`=%s LIMIT 1;', $this->db->escape($_POST['username']));
                $records = $this->db->query($q);
                $username = $_POST['username']; // i added this
                if($records->num_rows() == 1) {
                    $record = $records->row();
                    $test_hash = hash_password($record->salt . $_POST['password']);
                    if($record->pwhash === $test_hash) {
                        $new_salt = generate_salt();
                        $new_hash = hash_password($new_salt . $_POST['password']);
                        $q = sprintf('UPDATE tbl_user SET `pwhash`="%s", `salt`="%s" WHERE `user_pk`="%s" LIMIT 1;', $new_hash, $new_salt, $record->user_pk);
                        if($updates = $this->db->query($q)) {
                            $BB_DATA['messages']['information'][] = "Login successful.";

                            //add custom data to session
                            $this->session->set_userdata('username', "$username");

                            //$this->session->set_userdata('some_name', 'some_value');
                            // redirect to /users/home/
                            redirect(site_url() . 'users/home/'); exit();
                        } else {
                            $BB_DATA['messages']['critical'][] = "You have logged in successfully, but an error has occurred during login.";
                        }
                    } else {
                        $BB_DATA['messages']['critical'][] = "That username/password does not match.";
                    }
                } else {
                    $BB_DATA['messages']['warning'][] = "That username/password does not exist.";
                }
            } else {
                $BB_DATA['messages']['warning'][] = "You must enter a username/password.";
            }
        }
        if(isset($_POST['register'])) {
            // Redirect to /register/
            redirect(site_url() . 'register/');
        }
        $this->load->view('login', $BB_DATA);
    }
}

/* End of file login.php */
/* Location: ./application/controllers/login.php */

然后是用户登录页面的代码:

<?php
class Users extends CI_Controller {

    public function home()
    {
        $this->session->userdata('username');
        require_once(APPPATH . 'views/header.inc.php');//header
            include(APPPATH . 'views/breadcrumbs.inc.php');//top breadcrumbs

            echo ' <h1>Welcome'.$username.',</h1>';
            //$username = $this->session->userdata('username');
            echo $this->session->all_userdata();
            //echo $username;
            include(APPPATH . 'views/breadcrumbs.inc.php');//bottom breadcrumbs
            require_once(APPPATH . 'views/footer.inc.php');//footer
    }



}

?>

在登录脚本中,我调用codeigniters session library-

  

$这 - &GT;负载&GT;库( '会话');

稍后,假设一切正常,我将$ username变量添加到会话数据中。用户被重定向,并从那里

  

$这 - &GT;会话而&GT;用户数据( '用户名');   应该重新建立变量。它不是。

建议?

注意:因此,我使用print_r(array_values($this->session->all_userdata()));来打印数组中的所有值。它包含:

  

数组([0] =&gt; 4c46f32b249ed55e8439152d63365fed [1] =&gt; 75.134.164.245   [2] =&gt; Mozilla / 5.0(Windows NT; Win64; x64; rv:26.0)Gecko / 20100101   Firefox / 26.0 Waterfox / 26.0 [3] =&gt; 1410568024 [4] =&gt; [5] =&gt; ottomatic)

所以我很好奇,为什么把它添加为[5]而不是'用户名'?

1 个答案:

答案 0 :(得分:0)

感谢来自@ xd6_的正确方向的一些提示,我看到了发生的事情。

我误读了文档 - 您需要在每个页面上加载会话,除非它位于/config/autoload.php数组中。

一旦完成,我就可以按名称

访问数组元素
  

$ username = $ this-&gt; session-&gt; userdata('username');