Session started but not found in Cakephp - php

I have an application that I work in that is experiencing a problem where randomly a blank page loads.
I've found that the beforeFilter function fires but it never goes into the action of the controller that is being called at that time. I have also found that the session when this happens can not be found. session_status() returns PHP_SESSION_NONE. When the page is reloaded php can magically find the session again and the page loads normally.
Any help is appreciated. If you need/would like more information just let me know.
EDIT:
AppController beforeFilter
public function beforeFilter() {
parent::beforeFilter();
// Log all access to applictaion
$this->AccessLog->logPageAccess($this->request, $this->Session);
// Read the app's desired datetime display and set as view variable for TimeHelper use.
if(Configure::check('Datetime.dateDisplayFormat')) {
$timeFormat = Configure::read('Datetime.dateDisplayFormat');
$this->set(compact('timeFormat'));
}
}
Session Auth.User when session found
array(
'password' => '*****',
'id' => '44',
'role_id' => '5',
'username' => 'user',
'password_token' => null,
'email' => 'user#example.com',
'email_verified' => true,
'email_token' => null,
'email_token_expires' => null,
'active' => true,
'is_login_locked' => false,
'last_login' => '2015-02-04 16:41:47',
'last_action' => null,
'created' => '2014-07-07 12:45:46',
'modified' => '2015-02-04 16:41:47',
'created_by' => '19',
'modified_by' => '44',
'deleted' => false,
'deleted_date' => null,
'account_locked' => false,
'lu_theme_id' => '4',
'first_name' => 'Joe',
'last_name' => 'Bloggs',
'Role' => array(
'id' => '1',
'name' => 'User',
'is_admin' => false
)
)
$_SESSION when not found
array()

Are you running an array of servers under a load balancer? I've seen session weirdness in that type of environment before now.

Related

user_save doesn't do anything?

So I'm trying to make a form that creates a user and everything works... except the part that actually saves the user to the database...
The thing about it is that the user_save function isn't even returning a false or null value. It is just stopping the script altogether. I even tried echoing some values after it and they are inaccessible.
After hours of echoing out values, I have narrowed it down to the execute() method of the query object.
Here is the code:
$account = array(
'name' => $user_email,
'pass' => $user_password,
'mail' => $uer_email,
'theme' => '',
'signature' => '',
'signature_format' => NULL,
'created' => 0,
'access' => 0,
'login' => 0,
'status' => 1,
'timezone' => NULL,
'language' => '',
'picture' => 0,
'init' => 'email address',
'data' => NULL
);
if (user_save(NULL, $account)) {
user_authenticate($user_email, $user_password);
} else {
echo "Something looks broken.";
}
I have checked the Apache / PHP / MySQL logs and there are absolutely no notable errors. I thought that there might be a problem with how my data is set up in the array. Before this, I defined only the fields that HAD to be defined but it made no difference.
What do you think is wrong?
The line:
'init' => 'email address',
should be:
'init' => $uer_email,
The following code is all you need to create a new user, try starting from this and then adding the other values that you need.
$account = array(
'name' => $user_email,
'pass' => $user_password,
'mail' => $user_email,
'status' => 1,
'init' => $user_email
);
user_save(null, $account);

Session cookie not being written in CakePHP 2

Last weekend I was trying to troubleshoot a bug on a website where the Session was not being preserved in IE - today I went to do further work on the site on my laptop, and I could no longer log in -invariably I have done something incredibly stupid.
I'm using xampp on a Windows laptop, and working on localhost, and this occurs in all browsers. I am not very experienced with troubleshooting these kinds of problems - I have been able to ascertain the following:
The user is able to login (Auth->login() successfully logs the user in), the issue is the Session is gone when they are redirected
I can see the Sessions being written in my /tmp/ dir containing (what looks to be) the correct data
I can create my own stupid cookies and their values persist
No other cookies exist for the site
So, it would appear to me that the Session cookie is not being set, but I have run out of ideas as to why this might be occurring. I haven't changed any cookie related browser settings (outside of enabling cookies in IE), and I have double checked my Chrome cookie settings. I have also, as I mentioned, written some junk cookies in AppController, and I can see them created, and their data persists.
If I call $_SESSION after login(), everything looks great, but if I print $_SESSION before logging in, it's empty.
I am quite sure I have managed to do something retarded, but I have run out of ideas as to what it might be. I have restored my /app/core.php to be the Cake defaults:
Configure::write('Session', array(
'defaults' => 'php'
));
My login() function looks essentially as follows:
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again.'));
}
Auth settings in AppController:
class AppController extends Controller {
public $components = array(
'Session',
'Cookie',
'Acl',
'Email',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email', 'password' => 'password')
)),
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
),
);
And example output from printing $this->Auth->user(), $_SESSION before the redirect in login():
\app\Controller\UsersController.php (line 203)
array(
'id' => '10',
'name' => 'super',
'is_active' => '1',
'email' => 'super#test.com',
'group_id' => '3',
'address' => '3',
'phone' => 'xxxxx',
'category' => 'P',
'communication_in' => 'E',
'created' => '2014-11-29 16:27:19',
'modified' => '2014-11-29 16:27:19',
'Group' => array(
'id' => '3',
'name' => 'Administrators',
'created' => '2014-11-16 21:01:35',
'modified' => '2014-11-16 21:01:35'
)
)
\app\Controller\UsersController.php (line 204)
array(
'Config' => array(
'userAgent' => '4af162a3a94462226b6e93c6806203aa',
'time' => (int) 1417317929,
'countdown' => (int) 10,
'language' => 'eng'
),
'Auth' => array(
'User' => array(
'id' => '10',
'name' => 'super',
'is_active' => '1',
'email' => 'super#test.com',
'group_id' => '3',
'address' => '3',
'phone' => 'xxxx',
'category' => 'P',
'communication_in' => 'E',
'created' => '2014-11-29 16:27:19',
'modified' => '2014-11-29 16:27:19',
'Group' => array(
'id' => '3',
'name' => 'Administrators',
'created' => '2014-11-16 21:01:35',
'modified' => '2014-11-16 21:01:35'
)
)
)
)
Last created session file:
Config|a:4:{s:9:"userAgent";s:32:"4af162a3a94462226b6e93c6806203aa";s:4:"time";i:1417317929;s:9:"countdown";i:10;s:8:"language";s:3:"eng";}Auth|a:1:{s:4:"User";a:12:{s:2:"id";s:2:"10";s:4:"name";s:5:"super";s:9:"is_active";s:1:"1";s:5:"email";s:14:"super#test.com";s:8:"group_id";s:1:"3";s:7:"address";s:1:"3";s:5:"phone";s:10:"xxxxx";s:8:"category";s:1:"P";s:16:"communication_in";s:1:"E";s:7:"created";s:19:"2014-11-29 16:27:19";s:8:"modified";s:19:"2014-11-29 16:27:19";s:5:"Group";a:4:{s:2:"id";s:1:"3";s:4:"name";s:14:"Administrators";s:7:"created";s:19:"2014-11-16 21:01:35";s:8:"modified";s:19:"2014-11-16 21:01:35";}}}
Facepalm of the day:
Many hours later, I finally thought to check phpinfo(), and of course, the session.cookie-domain was set to the remote site. I suppose at some point last week I edited the wrong PHP ini file.

Drupal 7 returning theme() to page

I am developing a module and I need to call different .tpl.php layouts from the theme directory per page.So what I currently have is:
return theme('adverts_index', array('adverts' => $advertsThemeArray));
That is what I return in one of the page callback functions. Now this works perfectly without a problem but in another page callback function within the module I attempt the same thing with a different theme hook but it does not call the layout specified by the theme hook.
$r = theme('adverts_view_advert', array(
'advert' => array(
'id' => $advert->aid,
'seller' => $advertiser,
'more_from_user' => array(),
'year' => $advert->year,
'condition' => $advert->condition,
'region' => $advert->region,
'city' => $advert->city,
'content' => $advert->description,
'bids' => $allBidsArray,
),
)
);
return $r;
The page is not complete blank, the main page.tpl.php which is used on every page which contains the header and what not does still display. Just the template I am trying to call to its contents is not displaying.
This is my hook_theme:
function adverts_theme() {
$theme = array();
$theme['adverts_index'] = array(
'template' => 'adverts-index',
'variables' => array(
'advert' => array(
'title' => null,
'formatted_price' => null,
'image' => null,
'permalink' => null,
'formatted_date' => null,
'description' => null,
),
),
);
$theme['adverts_view_advert'] = array(
'template' => 'adverts-single',
'variables' => array(
'advert' => array(
'id' => null,
'seller' => null,
'more_from_user' => null,
'year' => null,
'condition' => null,
'region' => null,
'city' => null,
'content' => null,
'bids' => null,
),
),
);
return $theme;
}
Any help would be highly appreciated!
arguments changed to variables in Drupal 7, and since either variables or render element is required the registry won't be picking up your theme.
Make the two changes in the hook_theme() implementation, clear the caches, and you should be good to go.

Can CakePHP generate my database tables from the _schema attribute on the Model?

A common task for me when I'm writing CakePHP applications is to type out an SQL file and write it into the database before running bake to generate some scaffolding. This is one of the very few gripes I have with CakePHP - doing this ties me into MySQL, and I'm wondering if there's a better way to do it through code. As an example, in some frameworks I can define the columns that my model uses along with the datatype and such, and then run a command through an admin interface to "build" the database based on what what's presented in the code. It will do this on whatever database is sitting behind the framework.
Is there a way for CakePHP 2.x can do something like this? I want to write out the database schema in my Model code and run a command like bake to automatically generate the tables and columns that I need. After diving into the cookbook docs, the _schema attribute seems to do what I want to do:
class Post{
public $_schema = array(
'title' => array('type'=>'text'),
'description' => array('type'=>'text'),
'author' => array('type'=>'text')
);
}
but there are no examples explaining what I would I do from there. Does the _schema attribute serve a different purpose? Any help would be appreciated!
not from your $_schema array itself. but creating and using a schema file schema.php in /APP/Config/Schema.
you can then run the bake command "cake schema create" which will then "Drop and create tables based on the schema file."
I might then look sth like this:
class YourSchema extends CakeSchema {
public $addresses = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'length' => 10, 'key' => 'primary'),
'contact_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => 10),
'type' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => 2),
'status' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => 2),
'email' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 50, 'collate' => 'utf8_unicode_ci', 'comment' => 'redundant', 'charset' => 'utf8'),
'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_unicode_ci', 'engine' => 'MyISAM')
)
// more tables...
}

Kohana - Session Database

I'm attempting to use the session database with Kohana 3.
I have setup the mysql database as described here: http://kerkness.ca/wiki/doku.php?id=sessions_and_cookies
I am setting session variables like so:
Session::instance('database')->set('uid', $user_id);
However when I go to fetch the data back later on a different page or refresh it returns NULL.
Session::instance('database')->get('uid', NULL);
But if I put them right next to each other it works fine... i.e.
Session::instance('database')->set('uid', $user_id);
Session::instance('database')->get('uid', NULL);
Any ideas as to why this is happening?
I have also setup session.php in my config folder which looks like this:
<?php
return array(
'cookie' => array(
'name' => 'cookie',
'encrypted' => TRUE,
'lifetime' => 43200,
),
'native' => array(
'name' => 'session',
'encrypted' => TRUE,
'lifetime' => 43200,
),
'database' => array(
'group' => 'default',
'table' => 'sessions',
),
);
?>
Cheers,
Thomas.
Update the session.php file to look like this:
<?php
return array(
'cookie' => array(
'name' => 'session_cookie',
'encrypted' => TRUE,
'lifetime' => 43200,
),
'native' => array(
'name' => 'session_native',
'encrypted' => TRUE,
'lifetime' => 43200,
),
'database' => array(
'name' => 'session_database',
'group' => 'default',
'table' => 'sessions',
),
);
?>
Problem was by default the sessions are called 'session'.
So I renamed each adapter and it has fixed the issue.

Categories