My requirement is to save the data/value into session which i am passing it to controller via url.
ex: www.pulseofpublic.com/mobile/user_profile/user_forms/1 ( 1 is the user id)
class user_login extends CI_Controller {
public function user_forms($user_id) {
$this->session->set_userdata('user_id',$user_id);
$user_id = $this->session->userdata('user_id');
}
}
After running this code and open the session table in database value for user_id is "images" instead of 1.
If i write "echo "xyz";" before setting data into session using set_userdata. It is user_id=1.
class user_login extends CI_Controller {
public function user_forms($user_id) {
echo "xyz";
$this->session->set_userdata('user_id',$user_id);
$user_id = $this->session->userdata('user_id');
}
}
Why is this happening, Can some please help. I tried lot.
Please note :: session table is created in database and session library also loaded.
what is happening with out echo...
You have to do 1 thing that before making 'user_id' a session element, just clear it or remove it from session as
$this->session->unset_userdata('user_id');
May be 'user_id' is already in session array as set by you.
Also just print all session data as
print_r($this->session->all_userdata());
I hope it will help you.
Related
In my application, I am trying to save user information in a cache variable and access it in another method. I can access it inside the same method. But when I try to access out of that method, it returns null. I need to know how to achieve this, please someone help me out here with an example as I am new to the technology.
Inside a method:
Cache::set('user', $user);
Accessing it in another method:
$user = Cache::get('user');
And I want to clear it once accessing it.
Instead of using Cache for storing variable.
You can declare variable in class as following
class test{
public $user = []; //declare your function here.
function abc(){
$this->user = $user; //Assign value to variable
}
function xyz(){
$user = $this->user;
print_r($user);
}
}
With Codeigniter session.
I have created the ci_session table.
In this table I save session datas in the blob datatype.
$sess_data= array(
'logged_in' => TRUE
);
$this->session->set_userdata($sess_data);
For count all online users, I need to get logged_in==1 value in blob datatype.
For example, user 1 is logged in with Chrome web browser. User2 is logged in with Mozilla. In the database there are 2 sessions with the different session id.
How can i get this all logged_in==1 values in this ci_session table?
This is my model function.
Please help me the fill where place in this function?
public function count_online_users(){
$this->db->select('*');
$this->db->from('ci_session');
$this->db->where(...........);
$query = $this->db->get()->num_rows();
return $query;
}
Check this this method you have to follow
First you have to update logged_in = 1 in your users table as your defined name
after you get number of users are logged_in
// Update user in to call this function
$data = array(
'logged_in' => '1'
);
public function update_user($user_id, $data)
{
$this->db->where('id',$user_id);
$this->db->update('users',$data);
}
// After you get logged in user in total value
public function count_online_users()
{
$this->db->select('*');
$this->db->from('users');
$this->db->where('logged_in','1');
$query = $this->db->get()->num_rows();
return $query;
}
You can use javascript and asynchron request for update field. When you close the window, the handler of window.onunload is called
var unloadHandler = function(e){
//here ajax request to close session
};
window.unload = unloadHandler;
To solve the problem of redirection, php side you can use a counter
class someController extends Controller {
public function methodWithRedirection(){
$this->session->set_userdata('isRedirection', 1);
//here you can redirect
}
}
class homeController extends Controller{
public function closeConnection(){
$this->load->library('session');
if($this->session->userdata('isRedirection')!== 1){
//destroy session
}
}
}
Top of my controller i have added:
use Auth;
function in my controller
public function details($id)
{
if(Auth::check())
{
$user = Auth::user();
$product = Product::find($id);
$cart->product_id = $product->id;
$cart->category_id = $product->category_id;
$cart->user_id = $user->id;
dd($cart->user_id); //check if its storing the value
}
else {
return redirect()->route('login');
}
}
when I run this i get error:
Creating default object from empty value
If I remove the $user->id line the error goes.
I tried adding constructor also but still got same error
public function __construct() {
$this->middleware('auth');
}
dd is showing other details after it checks if user is logged in.
Is there a way to get user id of logged in user from users table that is created by default.
Thanks!
The issue is you’re calling $cart->product_id, but the $cart variable isn’t defined as far as I can see. PHP doesn’t know what to do, so because you try and assign a property to it, PHP assumes you want $cart to be a class, hence the “Creating default object from empty value” message.
Other than that, you code could be improved by using middleware to authenticate your users, and relations on your Eloquent models so you’re not manually assigning relation IDs.
Try like this,
You forgot to initials cart object
$cart = new Cart;
$cart->product_id = $product->id;
$cart->category_id = $product->category_id;
$cart->user_id = $user->id;
dd($cart->user_id); //check if its storing the value
Hi i have this data passed in the url
http://www.test.com/dashboard/john-doe
i want to get the page when i enter http://www.test.com/dashboard/john-doe
john doe is a name from the database i retrieve when the users can login.
so dashboard is a controller then how will i able to get the page if i passed a data john-doe next to dashboard controller? can someone help me figured this thing out? Any help is muchly appreicated this is my controller below
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('users_model', 'um');
$sessionData = $this->session->userdata('loggedIn');
$this->data['id'] = $sessionData['userID'];
if(!$this->session->userdata('loggedIn')){
redirect('login');
}
}
public function index($slug){
echo $slug; exit;
$this->data['title'] = 'Dashboard | test';
$this->data['menu'] = 'dashboard';
//get CustomerInfo
$this->data['getCustomerInfo'] = $this->um->getCustomerInfo($this->data['id']);
$this->template_lib->set_view('templateLogIn', 'dashboard', $this->data,'',$this->data);
}
}
The index function is loaded by default if the second segment of the URI is empty. If you have parameters you need to define a route:
$route['dashboard/(:any)'] = "dashboard/index/$1";
I'm using cakephp 2.3.0. I searched in the manual for quite awhile and searched the internet, but I haven't found the answer. Here is my high level scenario:
** Basically, I'm just trying to add a variable and a value to the options array, so that when cakephp builds the SQL insert into statement, it has this required column and value. **
The user logs in to a web page with username and password
Within the controller, I use the model to query my user table to get the id # of the user
I put the ID # into a session variable
Next, the user is logged in and then fills out a web form and submits
In my controller, I attempt to add to the model's options array with the value of that id stored in the session variable (this is where my trouble lies)....see details below
In my UsersController, I have this code snippet:
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$condition = array("User.username" => $this->request->data['User']['username']);
$this->Session->write('userId', $this->User->find('first',
array (
'conditions' => array ($condition),
'fields' => array ('User.id'))));
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
In my ActivitiesController, I have this:
class ActivitiesController extends AppController {
var $components = array('Session');
public function createActivity() {
if ($this->request->is('post')) {
$this->Activity->create();
$user = $this->Session->read('userId');
//debug($user);
debug($user['User']['id']);
$ownerID = ($user['User']['id']);
$this->Activity->set(array('owner_id' => $ownerID));
if ($this->Activity->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'home'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
My debug will print a value of 5, which is what I would expect to see. But, what I get is a database error, which outputs on the webpage the SQL INSERT INTO code and it tells me that my owner_id cannot be NULL. Which is correct, since that is how I setup this up in the DB table.
So, I'm getting a NULL when trying to put the value of the ID from the session variable into my options array. I would still consider myself more of a beginner to cakephp. Any help would be appreciated. Thanks.
You are trying to do what the framework already does.
The login function doesn't need more then this:
public function login() {
if ($this->request->is('post')) {
if($this->Auth->login()){
$this->Session->setFlash('Your logged in now.');
$this->redirect( array('controller' => 'activities', 'action' => 'createActivity'));
}
else{
$this->Session->setFlash('Username or password is incorrect', 'default', array(), 'auth');
}
}
}
And in your activity controller
public function createActivity() {
if ($this->request->is('post')) {
$this->request->data['Activity']['user_id'] = $this->Auth->user('id');
$this->Activity->create();
if ($this->Activity->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'home'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
AppController should contain
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array(
'Auth',
'Session',
);
}
It is also recommended to follow Cake's conventions, if you link Activity to User, set the foreign key name to user_id and not owner_id
Model associations
Don't use $_SESSION to store user information. The Auth component is already handling everything. You can get the user id in your controller by calling $this->Auth->user('id'); I think using that variable might solve your problem.
See the API docs here: http://api.cakephp.org/2.3/class-AuthComponent.html#method-AuthComponentuser