Cakephp keep session alive for long durations - cakephp

I m building an app called Trackosaur which tracks time on things you do. I m using Cake2+jQuery1.8 for this. The issue I m facing is related to sessions getting timed out. I could adjust the time that a session times out through the php ini. But I need to 'keep alive' a session for really long durations (10+ hours). So I setup a ajax call to a trivial function in my UserController which just uses session_start() in it.
JS
function keepAlive()
{
$.ajax({
type: 'get',
url: '/users/keepalive'
}).done(function(data){});
}
CAKE
public function keepalive()
{
session_start();
}
The ajax call is made every 10 minutes. I m not really sure if this is a good way to keep the session alive. Is there a better way I could do this using something in Cake itself as opposed to using session_start?
Many thanks for your time :)

In your core config file you can change the session timeout value.
In CakePHP 1.3 it's easy. Just find this and change to your value (36000 for 10 hours).
app/config/core.php
/**
* Session time out time (in seconds).
* Actual value depends on 'Security.level' setting.
*/
Configure::write('Session.timeout', '120');
In CakePHP 2, find this line and read the comment block above it for an explanation of how to configure the session time. I have not had to do this myself but I think:
Configure::write('Session', array(
'defaults' => 'php',
'Session.timeout' => 36000
));

Related

JSON post, browser and application different times

Before i start eating my keyboard, maybe someone can help me out here...
Im using Angular Bootstrap to set a time. After posting the time, and checking my console, i see that the selected time is posted. But after checking my log file, i see that the time is posted minus 1 hour. I've set my app => timezone to Europe/Amsterdam (ECT if im right) but still cant find what is going wrong.
Can someone help me please?
Controller:
public function update( EventRequest $request, $eventGuid )
{
Log::info($request->all());
$this->event->updateById($request, $eventGuid);
}
Angular post will be done as: 2016-01-27 03:45:39
Console log: 2016-01-27 03:45:39
Laravel log: 2016-01-27 02:45:39

CakePHP 3: Set cache duration in Controller when writing

I am using CakePHP 3 in my project. I am still learning new things as I go. Today I had a requirement to use CakePHP cache to cache data that I retrieve from the database. Every time I load a page that returns data from the database, takes around 30 to 40 seconds.
So I went ahead and configured cache in my controller, which significantly improved page loading from 30 sec to less then 4 seconds.
Now, what I want to do is set duration of the cache to clear itself after 1 hour to refresh new data that is stored in the database.
This is my code that does the caching:
if (!($custstoredata = Cache::read('custstoredata'))) {
# Code logic
Cache::write('custstoredata', $customers);
$this->set('data',$customers);
} else {
$this->set('data', Cache::read('custstoredata'));
}
After doing some research online, I found that I can use Cache::set to configure duration so I went a ahead and added Cache::set(array('duration' => '+1 hour')); in my if statement, but when I load the page in browser, I get this error:
Error: Call to undefined method Cake\Cache\Cache::set()
I am not sure what is the right way to set caching duration in controller real time when cache is written to a file.
I think I answered my own question.
I added below cache config in app.php file:
'reports_seconds' => [
'className' => 'File',
'path' => CACHE,
'serialize' => true,
'duration' => '+60 seconds',
]
Once I added above code, I modified my if statement with below code that fixed the problem.
if (!($custstoredata = Cache::read('custstoredata'))) {
# Code logic
Cache::write('custstoredata', $customers, $config = 'reports_seconds');
$this->set('data',$customers);
} else {
$this->set('data', Cache::read('custstoredata'));
}

404 after 43 seconds TTFB

I have script which uses simple_html_dom to parse different site data. It looks through my table of users, grabs the various sites needed, and then parses the data and stores them into my db.
The problem is that when I iterate through more than 3 users I get a 404 error. After a lot of debugging (much of which I'm learning as I go) it looks like as soon as my TTFB hits 40 seconds I get a 404 not found error. Anything under that the page returns fine.
I included the following in my php file to extend the time but this problem seems to ignore these statements.
// It may take a whils to crawl a site ...
ini_set("memory_limit", "-1");
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
ini_set('max_input_time', -1); //300 seconds = 5 minutes
set_time_limit(0);
But I've never had this problem before where I get a 404 for a page that exists. I'm somewhat new to simple_html_dom and crawling through different pages but is the problem that the wait time is too long? If so how do I can I fix that? Thanks
So it did not have to do with execution time or any setting I could change with the php script. For anyone having the same issue this was fixed by changing the way simple_html_dom loads the script from:
$html = new simple_html_dom();
$html->load_file($url_link);
To:
$html = #file_get_contents($url_link);
$html = str_get_html($html);
Hope this helps someone else!

CakePHP 1.3 cleares all cached pages after adding new post

I am using CakePHP 1.3 and trying to enable cache for view pages, cache system works fine and caches all pages. But when we add a new post (insert new record to database) or edit an old one (update a record of the table) CakePHP deletes all cached pages, not just the edited page!
app/config/core.php :
Cache::config('default', array('engine' => 'File','duration' => 8640000));
app/controllers/articles_controller.php :
var $helpers = array('Cache');
var $cacheAction = array(
'view' => array('duration' => 8640000),
'latest' => array('duration' => 8640000),
);
How can I tell Cake to delete just the cached version of changed page and not all cached pages?
This it actually pretty hard, so I can't just give you a piece of code to solve this. You need to edit the actual cake files in the lib folder that manage caching. Note: this is super not recommended by the cake people. However the lib/Cake/Cache/Engine/FileEngine.php is the file that has the operations of the file engine. You seem interested in the delete function:
/**
* Delete a key from the cache
*
* #param string $key Identifier for the data
* #return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
*/
public function delete($key) {
if ($this->_setKey($key) === false || !$this->_init) {
return false;
}
$path = $this->_File->getRealPath();
$this->_File = null;
//#codingStandardsIgnoreStart
return #unlink($path);
//#codingStandardsIgnoreEnd
}
Also, instead of editing the core cake files you could add your own file engine and use parted of the cake engine by moving the code and just extending the code there (that's totally cool in open source).
Its also possible that by reading the code used to implement the file caching engine you will find your actual solution. Good Luck.

cakephp Session->write problem in linux

I have problem with cakephp's Session->write method.
If I set a value like $_SESSION['..'] i'm able to read it back. But if I use the write method it's not working.
My problem is same as here: http://www.nabble.com/Session-problem-td16684956.html
The same code was working in windows but it's not working after I moved to linux.
Any permission problem would be the reason? (but i have given rw permission fully for the cake app directory).
code sample: in the link: http://www.nabble.com/Session-problem-td16684956.html
Configure::write('Session.save', 'php');
Configure::write('Session.cookie', 'CAKEPHP');
Configure::write('Session.start', true);
Configure::write('Session.checkAgent', false);
Configure::write('Security.level', 'medium');
cake version: 1.2.3.8166
Some steps to ensure it's not you:
clear the cache in your /app/tmp
check and recheck that your /app/tmp is world-writable recursively (that means drwxrwxrwx for all folders inside)
use Firebug to check your session cookie, maybe something has gone wrong with it
Last but not least, try to move your session persistence to your database (see: Session.save), just to test things out that way, you never know what you'll find.
Hopefully you'll find something if you try all these.
You should also try to use Cache::read and Cache::write
if (($session = Cache::read('session')) === false)
{
$session = 'some values';
Cache::write('session', $session);
}
Firstly, it will try to initialize Cache::read. If it returns false, Cache::write
will take part to store the values in sessions.
Prabu,
While I suspect the Configure::write() call will sometimes correctly set the session information (at least it looks like it might work), the Cake convention (aka the CakeWay) is to use the Session helper. I believe it is included by default in all Cake controllers; if not, you can always declare your controller as such:
class UsersController extends AppController {
...
var $helpers = array( 'Session', ... )
...
}
Then, when you want to write info to the session, just call:
$this->Session->write( 'checkAgent', false );
To read back values, use:
$this->Session->read( 'checkAgent');
For more information on the Session helper, check out the CakeBook # http://book.cakephp.org/view/484/Session

Resources