用户登录时如何将非发布数据发布到会话中

发布于 2024-10-09 13:32:18 字数 1893 浏览 0 评论 0原文

当我的用户登录网站时,会话数据中缺少他们的名字、姓氏和 ID,因为我的会话数据被编码为获取发布数据并提交到我的数据库中的会话表中。

因为用户在我的会话数据中使用电子邮件和密码登录,所以仅显示电子邮件,而没有其他任何内容。

如何使名字、姓氏和 ID 出现在数据库的会话表中?我想知道如何在用户登录时从数据库中获取这些详细信息并将其提供在我的 $u_data 数组中,以便它发布上传登录成功。

这是我的代码:

<?php
class Login_Model extends CI_Model {


    public function checkLogin() {

            $this->db->where('email', $this->input->post('email')); //compare db email to email entered in form
            $this->db->where('password', $this->hashed()); //compare db password to hashed user password
            $query = $this->db->get('users'); //get the above info from 'user' table

            if ($query->num_rows() == 1) { //if number of rows returned is 1

            $u_data = array( //new variable with session data
                'user_id' => $this->db->insert_id(),
                'email' => $this->input->post('email'),
                'first_name' => $this->input->post('first_name'),
                'last_name' => $this->input->post('last_name'),
                'logged_in' => TRUE
            );

            $this->session->set_userdata($u_data); //send data from variable to db session
            return TRUE;

       } else {
            return FALSE;
       }
 }


   public function hashed() { //hashing method

            // sha1 and salt password
            $password = $this->encrypt->sha1($this->input->post('password')); //encrypt user password
            $salt = $this->config->item('encryption_key'); //grab static salt from config file

            $start_hash = sha1($salt . $password);
            $end_hash = sha1($password . $salt);
            $hashed = sha1($start_hash . $password . $end_hash);
            return $hashed;
}

}

alt text

When my users log into the website their first name, last name and ID are missing from the session data because my session data is coded to take post data and submit into the session table in my database.

Because user logs in with email and password in my session data only email appears and nothing else does.

How can I make first name, last name and id appear in my session table in my db? I want to some how grab these details from the database when user is logging in and provide it in my $u_data array so it get's posted upload login success.

Here is my code:

<?php
class Login_Model extends CI_Model {


    public function checkLogin() {

            $this->db->where('email', $this->input->post('email')); //compare db email to email entered in form
            $this->db->where('password', $this->hashed()); //compare db password to hashed user password
            $query = $this->db->get('users'); //get the above info from 'user' table

            if ($query->num_rows() == 1) { //if number of rows returned is 1

            $u_data = array( //new variable with session data
                'user_id' => $this->db->insert_id(),
                'email' => $this->input->post('email'),
                'first_name' => $this->input->post('first_name'),
                'last_name' => $this->input->post('last_name'),
                'logged_in' => TRUE
            );

            $this->session->set_userdata($u_data); //send data from variable to db session
            return TRUE;

       } else {
            return FALSE;
       }
 }


   public function hashed() { //hashing method

            // sha1 and salt password
            $password = $this->encrypt->sha1($this->input->post('password')); //encrypt user password
            $salt = $this->config->item('encryption_key'); //grab static salt from config file

            $start_hash = sha1($salt . $password);
            $end_hash = sha1($password . $salt);
            $hashed = sha1($start_hash . $password . $end_hash);
            return $hashed;
}

}

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

π浅易 2024-10-16 13:32:18

如果您正在跟踪数据库中的会话,则会想到两种解决方案。

首先,您可以从用户表中选择第一个/最后一个并将其插入会话表中。这需要更改您的应用程序。

其次,您可以为您的应用程序设置一个视图,其中会话表自动与适当的用户连接,但这假设您已经拥有会话中哪个用户的一些唯一标识符(是电子邮件地址吗?* )。此解决方案不需要对应用程序代码进行任何更改,但需要对数据库进行更改,这可能是首选方法,具体取决于您的部署要求(也可能不是:))。

* 作为旁注,如果您使用电子邮件地址作为唯一标识符,请注意,在您决定这是否是适合您的解决方案时,有些人会共享电子邮件地址。

If you're tracking sessions in your DB, two solutions come to mind.

First, you could select the first/last from the user table and insert it into the session table. This requires changes to your application.

Second, you could set up a view for your application, in which the session table is automatically joined with the appropriate user, but that assumes you already have some unique identifier for which user it is in the session (was that the email address?*). This solution would not require any changes to the application code, but would require changes to the DB, which may be the preferred method depending upon your deployment requirements (or it may not :) ).

* as a side note, if you're using email addresses for unique identifiers, be aware that some people share email addresses as you decide if this is the right solution for you.

掀纱窥君容 2024-10-16 13:32:18

这就像

session_start();
$_SESSION['userdata'] = $u_data;

在 CheckLogin 方法中 执行以下操作一样简单:会话只是一个常规的 PHP 数组,它会自动为您保留。您可以将任何您想要的东西放入其中,但您必须自己将东西放入其中 - PHP 不会为您做这件事。

评论后续:

陷阱。因此,您只需修改类即可在登录经过身份验证后从数据库中获取该信息。我不知道你的数据库类是如何工作的,但不仅仅是检查是否有匹配的行,而是使用如下查询来获取名字/姓氏:

select firstname, lastname
from users
where email=$email and password=$password

如果你得到一个结果行,你就知道这是一个有效的登录,然后您只需检索名称数据即可。我不知道你的数据库类是如何工作的,但让它做到这一点应该不会太难。

It'd be as simple as doing something like:

session_start();
$_SESSION['userdata'] = $u_data;

within your CheckLogin method. The session is just a regular PHP array that happens to be automatically preserved for you. You can put anything you want into it, but you do have do put things into it yourself - PHP won't do it for you.

comment followup:

gotcha. So, you simply modify you class to fetch that information from the DB once the login's authenticated. I don't know how your DB class works, but instead of merely checking if there's a matching row, fetch the first/last name, using a query something like this:

select firstname, lastname
from users
where email=$email and password=$password

If you get a result row, you know it's a valid login, and then you just retrieve the name data. I have no idea how your db class works, but it shouldn't be too hard to get it to do that.

断桥再见 2024-10-16 13:32:18

当我在 CodeIgniter 中使用身份验证系统时,我已经习惯了通过在构造函数中获取用户数据,在视图中全局包含“用户”对象,并且在我的控制器中全局包含“用户”对象,就像这样

<?php

class My_Controller extends Controller {

    private $the_user;  //global var to store current user data

    function My_Controller() {
        parent::Controller();

        $data->the_user = $this->ion_auth->get_user();       //get user data
        $this->load->vars($data);                  //load into all views as $the_user "$the_user"
        $this->the_user=$data->the_user;         //load into private class variable "$this->the_user"
    }

......默认情况下,$the_user 变量对象在所有视图中都可用,并且 $this->the_user 始终可用于控制器函数。它始终代表当前登录的用户。

我使用 Ion_auth 进行身份验证并获取用户,因此您必须填写该部分。

我实际上只是 构造了一个“如何” 来实现扩展控制器类,以便所有身份验证逻辑自动继承到所有“受保护”控制器。

When I'm working with Auth systems in CodeIgniter I have made it a practice to include the "user" object globally in views, and also globally in my controllers, by fetching the userdata in the constructor, like so...

<?php

class My_Controller extends Controller {

    private $the_user;  //global var to store current user data

    function My_Controller() {
        parent::Controller();

        $data->the_user = $this->ion_auth->get_user();       //get user data
        $this->load->vars($data);                  //load into all views as $the_user "$the_user"
        $this->the_user=$data->the_user;         //load into private class variable "$this->the_user"
    }

At that point $the_user variable object is available in all views by default AND $this->the_user is always available to controller functions. It always represents the user currently logged in.

I am using Ion_auth for authentication and fetching the user, so that piece you would have to fill in.

I actually just constructed a "How-to" to implement extended Controller classes so all the Auth logic is automatically inherited to all "protected" Controllers.

贪恋 2024-10-16 13:32:18

以下为我解决了这个问题。我在这里查看了我的一个旧问题并运用了我的常识。

我对我的代码进行了此编辑。

if ($query->num_rows() == 1) { //if number of rows returned is 1

      $user = $query->result();

                $u_data = array( //new variable with session data
                    'user_id' => $user[0]->id,
                    'email' => $this->input->post('email'),
                    'first_name' => $user[0]->first_name,
                    'last_name' => $user[0]->last_name,
                    'logged_in' => TRUE
                );

The following solved this issue for me. I looked at one of my old questions on here and used my common sense.

I made this edit to my code.

if ($query->num_rows() == 1) { //if number of rows returned is 1

      $user = $query->result();

                $u_data = array( //new variable with session data
                    'user_id' => $user[0]->id,
                    'email' => $this->input->post('email'),
                    'first_name' => $user[0]->first_name,
                    'last_name' => $user[0]->last_name,
                    'logged_in' => TRUE
                );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文