How to edit a session after it's created - php

I'm using Phalcon PHP and I want to add another item to my session after it's created. I have this :
private function _registerSession($user, $account) {
$this->session->set('auth', array(
'user_id' => $user->id,
'username' => $user->name
)); }
In another controller I want to edit this session for example :
$auth = $this->session->get('auth');
$auth->add('account_id', '10');
And this session will content the 3 variables as :
$this->session->set('auth', array(
'user_id' => $user->id,
'username' => $user->name,
'account_id' => 10
)); }
But I don't know how I can dot that.

Yo need to do it in following manner:-
$auth = $this->session->get('auth'); // get auth array from Session
$auth['account_id']= '10'; // add new index value pair to it
$this->session->set('auth',$auth); // reassign it to auth index of Session

This should work:
$auth = $this->session->get("auth");
$this->session->set("auth", array_merge($auth, array('account_id'=>'10')));

i think you can use like this :-
$auth = $this->session->get('auth');
$auth['account_id']= 10;
$this->session->set('auth',$auth);

private function _registerSession($user, $account) {
$this->session->set_userdata('auth', array(
'user_id' => $user->id,
'username' => $user->name
)); }
// You should use the following code to set one more parameter in sesssion:
$this->session->set_userdata('auth', array(
'user_id' => $this->session_userdata('user_id'),
'username' => $this->session_userdata('username'),
'account_id' => 10
));

Phalcon's session code is simply a wrapper around $_SESSION. The simplest solution is to avoid using Phalcon functions:
$_SESSION['auth']->add('account_id',10);

Related

CodeIgnigter - How to store and access user login session

I have a controller that is used to create a user login session with the following code:
public function account(){
$data = array();
if($this->session->userdata('isUserLoggedIn')){
$data['user'] = $this->user->getRows(array('id'=>$this->session->userdata('userId')));
//Create session
$newdata = array(
'username' => $user['name'],
'email' => $user['email'],
);
$this->session->set_userdata($newdata);
//load the view
$this->load->view('templates/header', $data);
$this->load->view('pages/home', $data);
$this->load->view('templates/footer', $data);
//redirect('pages/view/');
}else{
redirect('users/login');
}
}
Problem occurs when I try to check from this or any other of my controllers the session data and it always returns false. Not sure what it is that Im doing wrong. If I pass this
$data['user']
to a View as data, Im able to access the name and email, but I cant access it on the controller
If I understand correctly I believe your problem is here.
$newdata = array(
'username' => $user['name'],
'email' => $user['email'],
);
The variable $user has not been set in the controller, at least not in the code you show. I think this is what you need
$newdata = array(
'username' => $data['user']['name'],
'email' => $data['user']['email'],
);
A suggestion, not related to your problem, regarding getting session data. With CI >= v3.0.0 you can obtain an item of session data like this.
$this->session->isUserLoggedIn;
The code behind this method is smaller and you type less to get the same return that $this->session->userdata('isUserLoggedIn') provides.
userdata() is a legacy method kept only for backwards compatibility with older applications.
First, you need store session data
`
$sedata = array(
'user' => 1,
'username' => $username,
'userid' => $dbrow->id,
'mobile_phone' => $dbrow->mobile_phone,
'success' => 'Successfully loggedin!',
'logged_in' => TRUE
);
$this->session->set_userdata($sedata);
**Now you get session data like :**
$this->session->userdata('user');
$this->session->userdata('userid');
`

How to use updateOrCreate proprely

Here is my code :
$user = \App\User::where('id', $uid)->firstOrFail();
$user->token()->updateOrCreate(['user_id' => $uid, 'type' => $type], [
'auth' => $auth_token,
'refresh' => $refresh_token,
'type' => $type
]);
I've got two models User and Token with an 'one to one' relationship.
On the first line, I try to catch a User into the database, then I update my model with the updateOrCreate() method.
However, as you can read it, I must use a selector 'user_id' => $uid before to successfully update my model. I think Eloquent should be able to manage it in a different way without making two requests.
You don't have to use the user_id => $uid on that query. It's injected based on the relationship already. You can rewrite that to:
$token = \App\User::where('id', $uid)->firstOrFail()->token()->updateOrCreate(['type' => $type], [
'auth' => $auth_token,
'refresh' => $refresh_token,
'type' => $type
]);

How to retrieve a particular session value in codeigniter?

I would set a session value in codeigniter
$sess_array = array(
'id' => $row->Login_Id,
'username' => $row->Login_Name,
'postid'=> $row->Fk_Post_Id,
);
$CI->session->set_userdata('logged_in', $sess_array);
Then how to retrieve a particular (say - id) session value . I tried these
$createdby=$this->session->userdata('id');
$createdby=$this->session->userdata($logged_in['id']);
but fails.
Use the array syntax for this. Like,
$this->session->userdata['logged_in']['id'];
set it like this
$sess_array = array(
'id' => $row->Login_Id,
'username' => $row->Login_Name,
'postid' => $row->Fk_Post_Id,
);
$CI->session->set_userdata($sess_array);
To retrieve
$createdby = $this->session->userdata('id');
$createdby = $this->session->userdata(logged_in['id']);
or
$createdby = echo $_SESSION[logged_in['id']];
$sess_array = array(
'id' => $row->Login_Id,
'username' => $row->Login_Name,
'postid'=> $row->Fk_Post_Id,
);
$CI->session->set_userdata('logged_in', $sess_array);
then
$logged_in = $this->session->userdata('logged_in');
$createdby = $logged_in['id'];
Session write process. session content could be dynamic value
$logged_in = [
'id'=> 1,
'name'=> 'test'
];
Particular Session retrieve process.
$this->session->userdata['logged_in']['id'];
$pdata = array(
'id' => $row->Login_Id,
'username' => $row->Login_Name,
); // pass ur data in the array
$this->session->set_userdata('session_data', $pdata); //Sets the session
$pdata = $this->session->userdata('session_data'); //Retrive ur session
$pdata['id'] will give u the corresponding id
In newer version, you can simply use
$this->session->username
$this->session->id
$this->session->postid

Session destroy in codeigniter

I am using Code igniter for my application. I have three controllers and three models in may application homepage,dashboard and settings.I have started the session in homepage model and wanted destroy it in dashboard controller. here is my code
foreach($query->result(as $row) {
$info = array(
'loginid' => $row->loginid,
'firstname' => $row->firstname,
'emailid' => $row->emailid,
'logged_in' => TRUE
);
}
$this->session->set_userdata($info);
but my session array is not getting displyed in dashboard controller.
How I should destroy the session?
Try this:
$this->session->sess_destroy();
You destroy the session with:
$this->session->sess_destroy();
as per the codeigniter session documentation.
That said, I'm not sure how it's related to the code you posted and I'm pretty sure it won't work.
foreach($query->result(as $row) Wrong
Please use
foreach($query->result() as $row)
{
$info = array(
'loginid' => $row->loginid ,
'emailid' => row->emailid,
);
}
To display in dashboard controller. Try this
$this->session->set_userdata($info);
Then you can destroy session by
$this->session->sess_destroy();
$this->session->set_userdata([
'logged_in' => TRUE
'loginid' => $row->loginid,
'firstname' => $row->firstname,
'emailid' => $row->emailid,
]);
And in your conntroller to unset like this
$this->session->set_userdata([
'logged_in' => false,
'loginid' => null
]);

Use the user.module API to create user in Drupal 7

I am trying to use user_save() in Drupal 7 to add new accounts to the system.
The function is defined as user_save($account, $edit = array(), $category = 'account').
According to the documentation, the variable $account is a user object. How do I put together a user object that the function can process?
Here is a comment that may help you out a little regarding the user object: User Object. Another way to see what the user object holds is to do:
<?php
global $user;
print_r($user);
?>
Here is a very basic example of creating a new user with user_save():
<?php
$edit = array(
'name' => 'New User',
'pass' => 'password',
'mail' => 'myemail#example.com',
'status' => 1,
'language' => 'en',
'init' => 'myemail#example.com',
'roles' => array(2 => 'authenticated user'),
);
user_save(NULL, $edit);
?>

Categories