I am trying to use a third party script and extract the logged in users userid. I am aware the CodeIgniter uses some sort of encrypted sessions. Can you please suggest how to get the userid. A simple $_SESSION does not seem to work.
I am basically running a separate script and i just want the session details i.e. the userid. But I do not want to modify this script as MVC model. I want to modify it as minimal as possible.
Sorry I am very new to CodeIgniter. Thank you for your time.
Depending if you auto-load the session library or not, we will need to include:
$this->load->library('session');
Then you should be able to use:
$session_id = $this->session->userdata('SessionID');
Does this get you what you need?
Code Igniter session
If you want to set a session variable, for example:
$this->session->set_userdata('some_name', 'some_value');
or use the array. It's quite easy if you read the docs. And the docs in Code Igniter are great!
$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe#some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
Related
How do I pass the session variable userid to a text input? Using Yii::app->session?
loginform model of my application
$session = Yii::$app->session;
$session->set('userid', $this->getUser($this->email, 'id'));
$session->set('firstname', $this->getUser($this->email, 'fname'));
view/ form
<?= $form->field($model, 'name')->textInput(['value' => Yii::$app->session['email']]) ?>
The problem is when you pass the value to the session. Because the value you are passing is an object, but there is a solution and it is simple.
$form->field($model, 'name')->textInput([
'value' => Yii::$app->session->get('userid')->id
]);
You should just pass it as Yii::$app->session->get('userid')->id.
You were calling the email instead of userid. You can read more about sessions it in the documentation itself. Please, let me know if there is anything unclear.
I am not entirely clear why you want to expressly set the session user ID when Yii has a built-in way to retrieve the user ID.
Yii::$app->user->getId();
The above will return the user ID without the need for you to manually set the session user ID.
How to define a dynamic global variable in code-igniter framework.
I am using Geo-Location tracking api, which tracks the country of the user based on user's IP address.
I need to set the value of this global variable dynamically depending on the country code returned from the API.
what is the best way to achieve this ?
In your case, You can store the value in session like this
$newdata = array(
'country' => 'Nepal',
);
$this->session->set_userdata($newdata);
And again retrieve the session value like this
$country = $this->session->userdata('country');
Have a look at their website (see the Config Class), it is written in great detail there.
I have a codeigniter application that I am using the Cart class to make an e-commerce store.
I am also using Codeigniter-Auth (a library) to make accounts on this system. I have noticed when I log out while testing the application, the session data for the shopping cart is also destroyed.
I noticed in the library this is how it logs the users in:
$data = array(
'id' => $row->id,
'name' => $row->name,
'email' => $row->email,
'loggedin' => TRUE
);
$this->CI->session->set_userdata($data);
And this is how it logs the user out:
return $this->CI->session->sess_destroy();
How do I tell codeigniter to only destroy the user session and not every session in the system?
Using:
$data = array(
'id' => $row->id,
'name' => $row->name,
'email' => $row->email,
'loggedin' => TRUE
);
$this->session->set_userdata($data); // instead of $this->CI->session->set_userdata($data);
You are actually setting 4 session variables! To unset a specific one, use:
$this->session->unset_userdata('loggedin');
I believe you can just use $this to refer to CodeIgniter's instance. You must also assign a "name" to your session data (variable) to be able to specifically unset it.
$this->session->set_userdata('sample_userlog',$data);
$this->session->unset_userdata('sample_userlog');
Hope this helps.
The documentation states to clear the current session use
$this->session->sess_destroy();
Note: This function should be the last one called, and even flash variables will no longer be available. If you only want some items destroyed and not all, use unset_userdata().
For example $this->CI->session->unset_userdata($data).
I have read about authentication in Lithium manual, still I have some questions about it.
After Auth::check ('default', $this->request), it will return an array of user data if succeed. I have finished this part according to the manual.
if I want to save some of this array into session (not all of them), how to do it?
Are those data in session encrypted? If not, how could I manipulate it, I want to encrypt it for security concern.
Thanks.
This should give you something to go on regarding encrypting session data - http://nitschinger.at/Session-Encryption-with-Lithium.
As far as telling Auth::check() which fields to save to the session:
Auth::config(array(
'default' => array(
'session' => array(
'persist' => array('username', 'email')
)
)
));
This is in the latest master, and there is more explanation at the top of security\Auth.php.
here are the parts that i think are relevant:
EDIT: when i use regular $_SESSION - evrything works...
function A saves the session data, than calls a view, that returns the data the user added to it to function B.
both function A and function B are on the same controller.
im using 2.0.2 version.. so when it also asked me to enter encryption key in the config file.
the problem is, that the session don't save the data when i move between the pages..
i also noticed that it changes the session_id varible...
do you know what am i doing wrong?
another important information - i return a value threw the URL to function B.
controller welcome: functionA
...
$data = array(
'username' => $myusername,
'is_logged_in' => true,
'permissions' =>$permissions
);
$this->session->set_userdata($data);
...
...
$this->load->view('login_success',$dataV);
controller welcome: functionB
$user=$this->session->userdata('username');
echo "<br> IN LINK REFERENCE: $user";
do you know what am i doing wrong?
Not sure, but looks like you haven't initialized your Session library. Can you please check if you have initialized Session library correctly? Please check $autoload['libraries'] = array(); in config/autoload directory.