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.
Related
Example of my code:
class SiteController extends Controller {
/**
* This is the default 'index' action that is invoked
* when an action is not explicitly requested by users.
*/
public function actionIndex() {
$_SESSION['test'] = 'testdata';
var_dump($_SESSION); exit;
}
Example on the second request:
class SiteController extends Controller {
/**
* This is the default 'index' action that is invoked
* when an action is not explicitly requested by users.
*/
public function actionIndex() {
var_dump($_SESSION);exit;
}
I have a project in yii. Project not mine - I'm just trying to fix errors.
My problem is:
first var_dump() shows that $_SESSION variable HAS the "test" index with "testdata". On the second request though I get an empty array of $_SESSION variable. Meaning, that every request session gets cleaned up. I've checked - the session ID stays the same. I've also checked this projects config - i can't find any references to extending SESSION component and changing it's behaviors. Also, when logging in yii DOES save states into SESSION, but the login fails because of SESSION being cleaned after redirect. That is to say COOKIE BASED authentication works which just proves the root of the problem.
Would very much appreciate help.
UPDATE
I've narrowed it down. This is the code for FRONT YII FRONT CONTROLLER(index.php):
<?php
#session_start(); // this line is at cwebuser.php at init() method and it is called at every request. should work properly.
var_dump($_SESSION);
$_SESSION['test'] = 'asdasd';
var_dump($_SESSION);exit;
It still prints empty $_SESSION on the second REQUEST. So the problem is probably not with the framework.
You can set session details in your /protected/config/main.php (this is the default unless you have changed it in index.html)
'session' => array(
'autostart' => true,
'timeout' => 1440, // time in seconds
),
Read about Session on CHttpSession
My problem was that I accessed my website via server ip : 123.123.123.123/site/index and this has conflicts with accessing and saving the session. Though I don't know the details. If someone has knowledge on this stuff I will gladly accept his(her) answer as the right one.
There is a file called controller.php under protected/components/controller.php which will be called before any action get called .. u can check that file and see... is there done any logout function calledthere...
//It clears all the sesstion data... or any php way
Yii::app()->user->logout();
Yes if u are in moule then u can also check ModuleName.php under module directopry ... if there is any session clearing script...
which clears the session... And yes this is not the right way of using session in Yii yes it is PHP but YII .... u can use folowing sytax dfor sessions..
//to set a session variable mnamed test
Yii::app()->user->setState('test',$test);
//to get a session variable named tets
Yii::app()->user->getState('test');
Here I go what am doing is I am using
Yii::app()->SESSION['userid']
with no
Yii::app()->session->open();
at login
Yii::app()->session->destroy();
at logout
I wanna know if dont do the open and destroy session is it worthy . Does Yii do it internally.
One more strange thing I dont know whats happening. In the same browser for a session I can login for multiple users .. this should not happen so.Is it that i am not using the open and destroy session methods .
public function actionLogout()
{
Yii::app()->user->logout();
Yii::app()->session->clear();
$this->redirect(Yii::app()->controller->module->returnLogoutUrl);
}
Please let me know how do i figure this out
For creating yii session
Yii::app()->session['userid'] = "value";
You can get value like this
$sleep = Yii::app()->session['userid'];
And unset session like
unset(Yii::app()->session['userid']); # Remove the session
In case of user signs out , you have to remove all the session.
Yii::app()->session->clear();
After this, you need to remove actual data from server
Yii::app()->session->destroy();
Don't clear session, only logout:
Yii::app()->user->logout(false);
In YII, the session is handled by 'CHttpSession' class - http://www.yiiframework.com/doc/api/1.1/CHttpSession
Should you use the method 'open()' Yii::app()->session->open(); depends on your configuration. If in the main.php, you have set the
'session' => array (
'autoStart' => true,
), then the Session will be started automatically by YII itself.
You can refer the source code for the method 'init()' here - https://github.com/yiisoft/yii/blob/1.1.16/framework/web/CHttpSession.php#L83
Regarding your question about using the methods 'close()' or 'destroy()', the method 'close()' only unsets the keys of Session but 'destroy' removes the whole session data
Once you craeted session it will allow you in same browser multiple time, i mean for same url it will allow you to login, you can just do it rename your session variable with different name and check that particuller variable to login with that.
Session is a Web application component that can be accessed via Yii::$app->session.
To start the session, call open(); To complete and send out session data, call close(); To destroy the session, call destroy().
Session can be used like an array to set and get session data. For example,
$session = new Session;
$session->open();
$value1 = $session['name1']; // get session variable 'name1'
$value2 = $session['name2']; // get session variable 'name2'
foreach ($session as $name => $value) // traverse all session variables
$session['name3'] = $value3; // set session variable 'name3'
On every page I see that new session is generated with null userdata
On model constructor
$this->config->set_item('sess_table_name', 'xx_sessions');
Because I want to store this session in another table because the other session table is being used for another login activity
Login function
function login($username,$password)
{
$this->db->where('login',$username);
$this->db->where('pass',$password);
$q=$this->db->get('prof');
// print $this->db->last_query();
if($this->db->count_all_results())
{
$arr=$q->row();
// creating the session
$this->session->set_userdata('login',$arr->id);
$this->session->set_userdata('prof',$arr->profile_id);
// print_r( $arr);
}
else
return FALSE;
}
This login function is on a model. After login and generating the session the page redirects to another page, on that page I see the session builds without any problem but when I move to another page the session losses along with the userdata.
I use the following function to check session data
function print_session()
{
print_r( $this->session->all_userdata());
}
Where I'm wrong ? Tank_auth library and ion_auth library works fine .. I had already used the
Put the session library name into the autoloader configuration, in application/config/autoload.php:
$autoload['libraries'] = array('session');
Then it's available automatically in each controller and everywhere in your application and you get your session data from anywhere:
$session_id = $this->session->userdata('session_id');
And if you don't want to auto load session library then you have to initialize the Session class manually in your controller constructor, use the $this->load->library function:
$this->load->library('session');
For details have a look here:http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
Edit /application/config/config.php and set cookie domain variable
$config['cookie_domain'] = ".yourdomain.com";
It will work!
.yourdomain.com makes the cookie available throughout the domain and its sub-domains.
I have met same problem, and i have searched lots of pages.
I figured out that changing sess_cookie_name solves the problem(new sessions generating issue)
$config['sess_cookie_name'] = 'somenewname'
Experts,
i have a little Problem with my sessions. I want to save my login data into the session like this:
checklogin Controller
$user = $this->user_model->user($email, $password);
$user["logged_in"] = TRUE;
var_dump($this->session->set_userdata($user)); // return NULL? is this correct?
var_dump($this->session->all_userdata()); //return the correct data
Now the Session is saved up to a redirect.
Other Controller
var_dump($this->session->all_userdata()); // return session_id,… and a empty user_data array
I think I have the same Problem with the Shopping Cart Class.
Can anyone help?
I dont know why, but i think my story will help someone who facing the same problem.
My problem is just because I forgot to downgrade my Wamp php version, its php 7.1. Looks like my CI not support php 7.1 or higher. So just a few click to downgrade my php, my problem solved.
I added session_start() in the main index.php file. Worked like a charm afterwards.
try this..
$user = $this->user_model->user($email, $password);
$user["logged_in"] = TRUE;
$this->session->set_userdata('user',$user); //set session of users with a name user.
to get the session value u can do..
print_r($this->session->userdata('user')); // prints the user session array..
read more about sessions in CI
The set_userdata() function does not have a return value, so it is correct to display null on var_dump().
In order to save custom data to user's session you should check the manual here
The parameter it accepts must be an array.
hmm try add to ci->session and then get data
//assign the CodeIgniter object to a variable
function __construct() {
parent::__construct ();
$this->ci = & get_instance ();
}
//add data to ci session
function test(){
$this->ci->session->set_userdata($user)
}
in the other controller assign the CodeIgniter object to a variable
function __construct() {
parent::__construct ();
$this->ci = & get_instance ();
}
and get the ci user data
$this->ci->session->all_userdata()
I got a similar issue when I want to rerun an old website that was built formerly using CI 3.0.6 on a server running PHP 7.3.
I have set a session in one controller.
$this->session->set_userdata('logged_in', TRUE);
Then, the other controllers that are accessed after that cannot retrieve the information while a new record in the session table in the database is stored.
empty($_SESSION['logged_in']); // true
empty($this->session->logged_in); // true
The solution that works for me is as follows.
Set a higher size for the id column in the CI session table. For example, our session table name is ci_sessions.
ALTER TABLE ci_sessions CHANGE id id varchar(128) NOT NULL;
Replace the current CI system directory with newer version. In my case, it works with the system directory from CI 3.1.10.
In summary, a newer CI version is required to be able to interact well with newer PHP (7.1+). It has a longer id value for the stored session record. Rather than downgrading your PHP version, it is better to upgrade the CI version.
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);