Cakephp session is destroyed after redirect - php

I'm currently on a MAMP install on my local machine testing a facebook application. I suspect it is probably my MAMP configuration.
In cake php in /user/login I set a session using
$this->Session->write('Facebook.last_name',$user_profile['last_name']);
$this->redirect('/users/add');
then in /users/add I try and access all session informtaion
print_r ($this->Session->read());
However the only session information that is returned is the following and not what I set.
Array ( [Config] => Array ( [userAgent] => 87a1f39ea78f3ab90174ff791710e6dc [time] => 1345915176 [countdown] => 10 ))

It may be that your session is not even being saved.
What do you get when you write to the session then immediately debug it?
$this->Session->write('Facebook.last_name',$user_profile['last_name']);
debug($this->Session->read());
exit;
Check your core.php file for where your session variable is being saved as well.
I think it is arround line 136 in the config/core.php

You might forget initializing Session component in your controller file.
Declare Session Component as follows in AppController, it will resolve issue for all the controllers. Or you can initialize it in your controller where you want to use Session
var $components=array('Auth','Session');

Related

can't use session at codeIgniter

I have a session in my form controller the session is create when the user save
the session affect the form page so my problem is :
if I loaded the page without clicking on save a undefined index[session index] error appears
so I created the session at the home page
but the problem is if I loaded the form page without visiting the home page at first the undefined index error appear
I tried to call the method session_start() but I got this error
A PHP Error was encountered
Severity: Notice Message: A session had already been started -
ignoring session_start() Filename: views/Form.php Line Number: 5
I strongly recommend using the default CI session library. You can autoload the session in autoload.php. Your session will start automatically, there is no need to call session_start().
Then replace $_SESSION['save']="true" with $this->session->set_userdata('save', 'true');.
I think you need to check if session['save'] is true or not, so beforehand you must declare by default that session['save'] is "false" in the default controller which is specified in the routes.php, and also make sure the session['save'] index is already defined in your form page controller, check like this:
if ($this->session->userdata('save')) {
// do something when exist
} else {
$this->session->set_userdata('save', 'false');
}
I am not sure why you are trying to implement your own session management within CI as one of the powerful tools CI offers is a flexible, simple yet powerful session management system.
Even so, surely a simple solution to your conundrum is to simply check if the session variable is set or not in your form, and if it is not set then set it to a value of 0. In your controller, you can deal with that, i.e. check if the value is 0, and if so, start a session and assign the session variable, or throw the user out, or do whatever you need you app to do when the session is not found, or is set to 0 indicating a session has not been activated.
Your session will start automatically, there is no need to call session_start() in every controller. codeigniter session library takes care of it.
First you have to load session library. application/config/autoload.php
$this->load->library("session");
if you have loaded already no need of loading again.
To set data in session
$this->session->set_userdata("KEY","VALUE");
To get data from session
$this->session->userdata("KEY");

codeigniter unsetting session works unexpectedly

I submit form to controller/complete action, set
$this->session->set_userdata('success', 3);
and then redirect to index action with redirect('controller', 'refresh');.
In my view I get
$success = $this->session->userdata('success');
do some work and then
$this->session->set_userdata('success', 0);
And it works fine, but when I reload page (it is an index action), I still get in $success 3, not 0. What am I missing?
I have seen many problems with codeigniter DB Session, and thus refuse to use it, to include the session not actually properly updating.
If you are interested I created a PHP Session based class that acts as a replacement, it benefits from backward compatability, but also a much easier way of using it.
Check out my Gist: https://gist.github.com/chazmead/1688becbcf11f897e962
To install you will need to replace the CI Session config in application/config/config.php to:
$config['session'] = (object)array(
'UID' => 'MY_SESSION_KEY',
'sess_expiration' => 7200,
'match_ip' => False,
'match_user_agent' => False
);
Then install the file to application/models/session.php
then instead of loading the CI session, just load this session model.
Using this is very easy, just assign variables to the session and it saves automatically, it also locks and unlocks the session so that async requests don't get locked up (which is a problem with PHP sessions)
For full backward compatability you may need to use $this->session = &$this->Session after loading the new model, otherwise you will have to make sure your calling session using Session (Uppercase S) as this is how CI models work. or install as a library instead..
The codeigniter by default managing session in COOKIE, more info
why the cookie value is not updated at once when i submit the form?
CI also provides setting to store session data in database table, if you store session in table this would work fine.
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

How to access php session files

I have a Yii application with cookie based login enabled.
So , every time a user connects to the application a session file gets created on the server
(/var/lib/php5) related to the PHPSESSID.
One of the Session variables is the timeout (timestamp).
I want to create a php script which access all of these Session files , opens them , checks for the timeout , and if it is timed-out ,make some changes to a specific table in the database.
First problem , from within my php script I'cant open session files in /var/lib/php5.
Permission denied.(AFAIK from my php script I try to open those files as Apache User , though I need to be superuser , how could that be done...on the fly ? -without changing file permissions)
Second problem.
Even if I try from my php script to open the superglobal $_SESSION for a specific session id
I still get the permission denied message.
$sSessId='la05bdm63rdmjevg4hfrcf17u6';
session_id($sSessId);
session_start();
print_r($_SESSION);
Any suggestions on how a php script can access those session files ?
Many thnx in advance.
First you do not need to invoke session_start() because by default its true, to disable in the config/main.php
'session' => array (
'autoStart' => false,// by default is true
),
The best way to read session in Yii is by using the build in functions Yii::app()->session which is similar to the super global array $_SESSION.
Yii::app()->session['var'] = 'value';
echo Yii::app()->session['var'];
To unset a session variable
unset(Yii::app()->session['var']);
To remove a session variable
Yii::app()->session->remove('var')
to change the path to your session variable
'session' => array (
'sessionName' => 'Site Access',
'cookieMode' => 'only',
'savePath' => '/path/to/new/directory',
),
Hope this will help

CakePHP keeps logging me out

Recently i have made three Cake Apps and all three share this problem. The config is mostly stock and i use this as the session options.
Configure::write('Session', array(
'defaults' => 'php',
'cookie' => 'test'
));
After lots of googling everyone just suggests that the security level is too high, but i have never changed this value, it's:
Configure::write('Security.level', 'medium');
Edit: I have also tried with low security and no change.
I am only using basic auth to check if the user is logged in or not.
After logging in the cookie is set to expire three hours later and the expire date doesn't update until I log in again, is this normal?
I cant seem to replicate the problem at all, sometimes I will log in and the very next click will log me out again and other times it will last a while.
I am using Chrome on Windows 7 and there is no AJAX on the website.
Any ideas? Thanks.
Are you using Ajax. Is the problem only happening in IE?
IE uses a different Browser Agent string for Ajax calls to the browser itself. For extra security, Cake checks the browser agent and, in the case of IE, thinks another browser is trying to hijack the session as the agent is different.
You can disable this check with:
Configure::write('Session.checkAgent', false);
After running into the same problem I've found that this was caused by the Session.cookieTimeout value. Although the php session was still valid, the expiration date on the session cookie does not get refreshed.
This is now my session config
Configure::write('Session', array(
'defaults' => 'php',
'timeout' => 30, // The session will timeout after 30 minutes of inactivity
'cookieTimeout' => 1440, // The session cookie will live for at most 24 hours, this does not effect session timeouts
'checkAgent' => false,
'autoRegenerate' => true, // causes the session expiration time to reset on each page load
));
the problem is with sessions:
First check ur 'phpinfo();'
check if the sessions are file based.
if yes, go through the process.
create a new script file(php) which contains only this code:<?php var_dump(session_save_path());?>
run it if you get null or empty string then go for this process:
first create a directory in your root folder name it 'xyz' or whatever u want.
make it writable i.e. chmod 777.
go to the script where you start sessions and before starting the sessions change your session_save_path to the newly created directory. i.e.: session_save_path('pathToxyz');
and then you r done.
if in case the sessions are set as memory: no configuration is required. they just use system memory. in that case you would never have got in to this problem.
You are not the only one having issues with CakePHP sessions on Chrome browser.
Pixelastic fellow coder suggests the following fix, quote :
Just create file named session_custom.php in app/config/, drop the following lines in it:
// Killing this config that was causing so much trouble with Chrome
ini_set('session.referer_check', '');
// No session id in url
ini_set('session.use_trans_sid', 0);
// Using custom cookie name instead of PHPSESSID
ini_set('session.name', Configure::read('Session.cookie'));
// Cookie like time, depending on security level
ini_set('session.cookie_lifetime', $this->cookieLifeTime);
// Cookie path
ini_set('session.cookie_path', $this->path);
Then set Configure::write('Session.save', 'session_custom'); in your core.php file.

JMeter cookie manager corrupts the cookie value

I'm trying to do load testing using JMeter 2.5.1. The application is written in PHP, and uses the standard cookie-based session management with a named session. Currently, the test plan is a very simple 2 HTTP request and 1 Cookie Manager within 1 Thread Group. The Cookie Manager's cookie policy is set to compatibility as suggested by the tutorials. However, the session still gets lost on each request.
On the first page call, the session is initialized. I printed the following info before and after the session_start call:
before session start:
session_name() = 'PHPSESSID'
session_id() = ''
$_COOKIE = array (
)
after session start:
session_name() = 'sasExtSMSESSID'
session_id() = 'ihl8svsbl76au7h1ccn3c0ci61'
$_COOKIE = array (
)
On the second page call, the session is supposed to have already been set. But it seems that the cookie that JMeter's cookie manager is corrupted:
before session start:
session_name() = 'PHPSESSID'
session_id() = ''
$_COOKIE = array (
'sasExtSMSESSID' => 'ihl8svsbl76au7h1ccn3c0ci61, sasExtSMSESSID=ihl8svsbl76au7h1ccn3c0ci61',
)
after session start:
session_name() = 'sasExtSMSESSID'
session_id() = '2ro2bkd3t3liq76h7lqn603gm7'
$_COOKIE = array (
'sasExtSMSESSID' => 'ihl8svsbl76au7h1ccn3c0ci61, sasExtSMSESSID=ihl8svsbl76au7h1ccn3c0ci61',
)
So it seems that the cookie array is corrupted somehow, and as the session id is invalid a new one is generated. Beyond calling session_name() and session_start(), there is nothing special in the application that deals with cookies. Accessing the pages using browsers also works. So I guess I didn't configure the JMeter correctly. Any idea what could have caused this? Any help is greatly appreciated.
I'm not sure if this is really an answer, since I still have no idea what is happening, but I guess this is a solution that others may benefit from.
Changing the Cookie Manager's policy from compatibility to default lets me to sidestep this issue, since the corruption apparently happens to another part of the cookie variable:
session_name() = 'sasExtSMSESSID'
session_id() = 'gknq98q7fpecjciti3da9l6mj7'
$_COOKIE = array (
'$Version' => '0',
'sasExtSMSESSID' => 'gknq98q7fpecjciti3da9l6mj7',
'$Path' => '/, sasExtSMSESSID=gknq98q7fpecjciti3da9l6mj7',
)
So far having that $Path corrupted has produced no adverse effect.
Would still appreciate anyone explaining to me exactly what is happening here.

Categories