Catch session id in codeigniter - php

I wanna to know how to catch session id from device/client.
I have a Login API, the logic is like this. When client login to the apps they are also get an session id. the code is below:
public function user_post()
{
$data = array (
'username' => $this->input->get_post('username'),
'password' => sha1($this->input->get_post('password'))
);
$result = $this->login_m->user_check($data);
if ($result ) {
foreach ($result as $row ) {
$sess_array = array(
'username' => $row->username,
'email' => $row->email,
'uniqueID' => $row->uniqueID,
'userid' => $row->userid,
'session_id' => session_id()
);
$this->session->set_userdata('logged', $sess_array);
$this->response(array('success' => $this->session->userdata('logged') ));
}
} else {
$this->response(array(404 => 'missing parameter'));
}
}
it also has an array init. and the json response would like this below:
{
"success": {
"username": "johndoe123",
"email": "myemail#gmail.com",
"uniqueID": "1q2w3e4r5t6y",
"userid": "97",
"session_id": "1bbfdj07as68bvp3ctrboj8gs7"
}
}
the question is, how i to catch the session id that is store in client apps?
so, when they are trying to get some information like news, or their profile. The client apps send me back the session id.
I presume that session id has array data just like when they are try to login.
so maybe i will get the array, array value will be the condition to get the news or profile.
CMIIW

Try this
$this->session->userdata('session_id');

$session_data=$this->session->userdata('logged');
use this to get all the session data and you can get the particular data by iterating that array
like this $session_data['session_id']

Related

How to change JSON response on JWT Authentication for WP-API?

I have used "JWT Authentication for WP-API" plugin to login and to generate a token.
On the JSON response I have only some data from the wp_users table.
How can I change it to take some others value from wp_usermeta table on response, because I want to know user status level.
You need to use the filters jwt_auth_token_before_sign or jwt_auth_token_before_dispatch.
The first filter receives the token data, and a user object.
In another plugin or in your theme, you'd need to call add_filter.
I'm a bit rusty on my Wordpress, and I do not have a WP instance handy to test this out, but this is the general theory:
Basic example:
add_filter('jwt_auth_token_before_sign', 'add_user_info_jwt', 10, 2);
function add_user_info_jwt($token, $user) {
// fetch whatever information you want from the user, probably using the $user
// object as starting point.
$token['roles'] = implode(',', $user->roles);;
return $token;
}
The $token you receive will have this starting structure and information:
$token = [
'iss' => get_bloginfo( 'url' ),
'iat' => $issuedAt,
'nbf' => $notBefore,
'exp' => $expire,
'data' => [
'user' => [
'id' => $user->data->ID,
]
]
];

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');
`

Doctrine findby to loop all through rows to show all users

I'm very new to PHP and having trouble making an API that's essentially taking a GET request and returning all users stored in a table. This application uses Zend Framework 2 and Doctrine.
Controller:
public function viewAllAction(){
$restService = $this->getServiceLocator()->get("Rest");
$restService->auth();
$business = $restService->getIdentity();
$bizID = $business->getId();
$clientModel = $this->getServiceLocator()->get('clientModel');
$clients = $clientModel->findAllByBusinessID($bizID);
$params['client'] = array(
"name" => $clients->getName(),
"email" => $clients->getEmail(),
"phone" => $clients->getPhone(),
"address" => $clients->getAddress(),
"suburb" => $clients->getSuburb(),
"postcode" => $clients->getPostcode(),
"state" => $clients->getState(),
"country" => $clients->getCountry(),
);
return $restService->send($params);
}
Model:
public function findAllByBusinessID( $business_id ){
$clientRepository = $this->getMapper()->getRepository( self::ENTITY_CLASS );
$clients= $clientRepository->findBy(array("bid" => $business_id));
foreach($clients as $client) {
return $client;
}
}
At the moment I can successfully retrieve data and return it via rest, but only the first set(row) of data. There are more in the table with the same business ID.
How to I return all rows that have the same business ID instead of just the first one? Thank you!
The problem is in your loop in findAllByBusinessID method. return breaks your loop, so only first row is returned. What do you want is probably something like this:
public function findAllByBusinessID( $business_id ){
$clientRepository = $this->getMapper()->getRepository( self::ENTITY_CLASS );
$clients= $clientRepository->findBy(array("bid" => $business_id));
$results = [];
foreach($clients as $client) {
$results['client'][] = [
"name" => $client->getName(),
"email" => $client->getEmail(),
"phone" => $client->getPhone(),
"address" => $client->getAddress(),
"suburb" => $client->getSuburb(),
"postcode" => $client->getPostcode(),
"state" => $client->getState(),
"country" => $client->getCountry(),
];
}
return $results;
}
But you should split this method into 2 separate functions. One function for retrieving data from database, one to format data the way you want.
Another solution would be to use Doctrine's queryBuilder and return data as array set (getArrayResult())

Saving Array to a Session and Retrieve it, CodeIgniter

So $token here is an array of data from the database, I would like to retrieve it in the view. I know that the data is already there by using print_r($this->session->all_userdata()); but I would like to retrieve the values from the $token array and use it.
Controller:
$token['answer']=$this->Qmodel->get_answers();
$data= array(
'username' => $this->input->post('username'),
'is_logged'=> 1,
$token
);
$this->session->set_userdata($data);
You can try with this one,I hope it will help you
$token['answer']=$this->Qmodel->get_answers();
$data= array(
'username' => $this->input->post('username'),
'is_logged'=> 1,
'token' => $token['answer'] // assign $token['answer'] array into token
);
$this->session->set_userdata($data);
get $token['answer'] array data from session:
$token_from_session = $this->session->userdata('token'); //return $token['answer'] array

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

Categories