Rectangle 27 1

php How to post non post data into session when user logs in?


<?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 actually just constructed a "How-to" to implement extended Controller classes so all the Auth logic is automatically inherited to all "protected" Controllers.

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

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...

Note
Rectangle 27 1

php How to post non post data into session when user logs in?


* 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.

@Psychonetics: then you may either need to refactor your sessions table or make a second sql query.

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

I was thinking of something along those lines. I would like to use the ID of the user. So when the email address is recognised it then get's the id from the row the email address is in then grabs all the associated data such as first_name, last_name and puts this into the session table. Kind of complicated though as my sessions table has 1 column for all the session data.

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

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 :) ).

Note
Rectangle 27 1

php How to post non post data into session when user logs in?


select firstname, lastname
from users
where email=$email and password=$password
session_start();
$_SESSION['userdata'] = $u_data;

I may have it wrong, but I think OP is tracking sessions in a DB table - possibly to support multiple front ends, such as for load balancing. Then again, I may have misread it and s/he really is looking for your answer :)

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.

It'd be as simple as doing something like:

When user registers and is automatically logged on, first_name, last_name etc are put into my sessions table in the db. This is because the post data is available from when user fills out the registration form. With login now the only fields available are email and password so the only post data I can grab to put into my sessions table would be email. So that's where I became stuck.

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:

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.

Note
Rectangle 27 0

php How to post non post data into session when user logs in?


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
                );
Note