How to get the session data into Codeigniter controller after user logged in? - codeigniter

I want to access session data after the user has logged in. I'm able to access session data in view pages but I want to access in the controller as well to create some database tables with session data like user id, name etc.

You can get session data:
$session = $this->session->userdata('session_name'); //Replace 'session_name' to your session name
print_r($session); //For check
You can details from Click Here

first set session variable in login controller as-
$this->session->set_userdata('userid', $row->iduser);
and use that session variable as-
$this->session->userdata('userid');

Following line will show all session data.
$session_data = $this->session->userdata();
echo "<pre>"; print_r($session_data); exit;

Related

Delete session in laravel

I am using a session_variable as the user_id to get information about the user by querying with the help of the variable. Whenever my user logs in with correct credentials, I create a session variable using:
\Session::put('user_id', $user_id);
And now I have a condition when the user logs out from the website,so how do I remove the session variable from the session using laravel. I tried something like : session()->forget('some_data'); which didn't seem to work for me so what should I do to remove all the session variable I have in the webpage?
You can try with this
Session::flush(); // removes all session data
Session::forget('user_id') // Removes a specific variable

Codeigniter remove specific user's session

In Codeigniter, how do I remove specific user's session?
I have set login session like below. And I want to remove specific MemId's LoggedIn(in other words, session for just one user) assuming there are about 1000 users.
$this->session->set_userdata([
'LoggedIn'=>true,'MemId'=>PrimaryKey
]);
When user logged in you are setting two session variables.One is LoggedIn and another 'MemId' which is the primary key of member.So,first retrieve the logged in user member id from database then unset it.Might work properly.
MemId is the member id of user for which you want to remove session.
$this->session->unset_userdata('MemId');

How to keep session id after login in Laravel 5.1

I been using my session id to identify all the items that are added to a shopping cart temporary items table.
What I didn't know is that once the user login into the system the session id changes so I can't query the temporary items table anymore as there is no products containing the new session id value.
I am using file session driver and I was thinking to use db sessions but I think it will be exactly the same.
Any ideas on how to keep the session id after login?
Instead of using user's session_id you can store the cart items in a separate session key.
Like so -
Session::put('cart.items.0', $item_id);
Now you can access it as -
$cart = Session::get('cart');
Not exactly what you asked for, but to me this seems like a better way.

Multiple versions with single database without duplicating code

I am developing payroll management with codeigniter. I will tell the design briefly.
I have single db. In that db, multiple clients are registered. Till now we provided single login form for clients by prompting username, password and client ID. But customer asking to give unique URL's for each and every client with out duplicating code and with single db.
For example: http://www.example.com/Client1, http://www.example.com/Client2 etc
So that client can enter username and password only. Based on the client code in url, we can validate username and password.
Just i want to know this is possible or not. Any problems will come in future?
Thanx
i got it
you can handle it from routes
in your routes file just call your database
$db =& DB();
$db->select('employe_name');
$query = $db->get( 'tbl_user' );
$result = $query->result();
foreach( $result as $row )
{
$route[ $row->name_prefered ] = ' http://www.example.com/dashboard';
now what acutely happens is when ever http://www.example.com/dashboard link hits it will change to http://www.example.com/employe name

How can I validate current user session id with database stored session ids?

I have found that Code Igniter has already ci_sessions table which stores users session ids in it. I understand the main purpose of that table. Just I want to clarify how I can validate with ci_session table data with my current user login session id?
Basically , I am trying to do User login one place at a time. Multiple login's to be prevented. How can I do this?
You couldn't do that with session ID's since logging in from another location would create a new session ID at that new location.
Store the userID to the session then check it on login.
$this->db->where('userId',$userId);
$result = $this->db->get('ci_session');
if($result->num_rows() > 0)
{
//log out old user, throw error, whatever
} else {
//continue with login
}
By storing the user id in the session data and adding it as a column to the sessions table, you can remove all sessions of that user id on login before the session data is created. This will invalidate any session of the same user. For further explanation on how to do this, see here: https://stackoverflow.com/a/9564433/89211

Resources