How to decrease session time in cakephp - cakephp

i have write this code in app/Config/core.php file of cakephp to timeout in 1 minute
Configure::write('Session', array(
'defaults' => 'php',
'timeout' => 1,
'autoRegenerate' => true
));
but this code not work

When you use the 'php' defaults, the time out is actually taken from the session.cookie_lifetime directive in php.ini. Either change the value in the php.ini configuration for your php or change the configuration like this:
Configure::write('Session', array(
'defaults' => 'php',
'autoRegenerate' => true,
'ini' => array('session.cookie_lifetime' => $timeInSeconds)
));

Related

cakephp redis storage besides sessions

I am trying to use redis as my caching engine and I have so far was able to add the add redis in cakephp and it writes my sessions to my redis just fine. This is what I did in the core
$engine = 'Redis';
$prefix = 'myapp_';
Cache::config('session', array(
'engine' => $engine,
'prefix' => $prefix . 'cake_session_',
'duration' => $duration
));
Configure::write('Session', array(
'defaults' => 'cache',
'timeout' => 100,
'start' => true,
'checkAgent' => false,
'handler' => array(
'config' => 'session'
)
));
Now I want to be able to write to redis from my controller some cached queries but I am not able to save that in redis. Instead it saves it to some default (I have no idea where) cache.
This is what I did in my controller
public function index()
{
Cache::write("TestKey","myquery","default");
var_dump(Cache::read("TestKey")); die;
}
The above gives me the value myquery on the browser but when I go to my redis-cli and look for keys * I do not see that key there. So obviously its saving somewhere else. I am not sure how to make it to write to redis. I have tried this Cache::write("TestKey","myquery","Redis"); but that also did not work.
Thanks for help in advance
**EDIT 1 **
I just did some debugging and it looks like its trying to add to file
object(FileEngine)[3]
protected '_File' => null
public 'settings' =>
array (size=10)
'engine' => string 'File' (length=4)
'path' => string '/Library/WebServer/Documents/phoneapp_backend/app/tmp/cache/' (length=60)
'prefix' => string 'cake_' (length=5)
'lock' => boolean true
'serialize' => boolean true
'isWindows' => boolean false
'mask' => int 436
'duration' => int 3600
'probability' => int 100
'groups' =>
array (size=0)
empty
protected '_init' => boolean true
protected '_groupPrefix' => null
How do I change it to write to redis?
Thanks
You need to configure the default cache configuration to use Redis insteado of File. In app/Config/bootstrap.php Change
Cache::config('default', array('engine' => 'File'));
So it reads:
Cache::config('default', array('engine' => 'Redis'));
You may need to add more options to the configuration for specifying the server and port where Redis is running.

Trouble Generating PDF with CakePHP and CakePdf

I am trying to use CakePdf to generate a pdf file within CakePHP. I have a function in the CourseGradesController called viewReport. I want viewReport to allow you to select a student from a select field, and then upon submitting the form, it will generate a PDF with the appropriate data. If I put the data into a table and do not try to make a PDF, the page will display correctly, so I don't think that is the problem. In bootstrap.php, I have
CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
Configure::write('CakePdf', array(
'engine' => 'CakePdf.WkHtmlToPdf',
'options' => array(
'print-media-type' => false,
'outline' => true,
'dpi' => 96
),
'margin' => array(
'bottom' => 15,
'left' => 50,
'right' => 30,
'top' => 45
),
'binary' => '/var/www/cakephp/app/Plugin/CakePdf/Vendor/WkHtmlToPdf',
'orientation' => 'landscape',
'download' => false
));
I have the WkHtmlToPdf folder moved into /var/www/cakephp/app/Plugin/CakePdf/Vendor/WkHtmlToPdf, so the lib and include folders are in that directory.
In the viewReport function of CourseGradesController, I have
function viewReport($id = null)
{
$this->CourseGrade->id = $id;
if (!$this->CourseGrade->exists())
{
throw new NotFoundException(__('Invalid invoice'));
}
$this->pdfConfig = array(
'orientation' => 'portrait',
'filename' => 'Invoice_' . $id
);
$this->set('invoice', $this->CourseGrade->read(null, $id));
...
}
If I navigate to /courseGrades/viewReport, I get the error "Error: The requested address '/cakephp/courseGrades/viewReport' was not found on this server."
If I naviate to /courseGrades/viewReport/1.pdf, then I just see a completely blank screen.
I would strongly recommend the use of this plugin for this kind of work.
https://github.com/ceeram/CakePdf

CakePHP Uploader Plugin only works on one Model

I've installed Miles Johnson's Uploader plugin and set it up with one of my models and got it working perfectly. Very nice.
Then I went and set it up on another model with almost identical code [the only difference is the upload path] and it won't work on the second model. When I submit the form the plugin doesn't seem to notice; I get an SQL error from an attempt to insert the POST file array straight into the DB.
Here is the code. [Other than this the plugin is imported in the bootstrap]
public $actsAs = array(
'Uploader.Attachment' => array(
'photo' => array(
'name' => 'formatFileName',
'uploadDir' => '/uploads/uses/img/',
'dbColumn' => 'photo',
'maxNameLength' => 30,
'overwrite' => true,
'stopSave' => true,
'allowEmpty' => false,
'transforms' => array(
array('method' => 'resize', 'width' => 240, 'dbColumn' => 'photo_thumb'))
)
),
'Uploader.FileValidation' => array(
'fileName' => array(
'extension' => array('gif', 'jpg', 'png', 'jpeg'),
'required' => true
)
)
);
This is on the model that is not uploading and the only difference is the uploadDir.
Does the plugin only work on one model? Any clues? thnx :}
Edit for extra clarity
Here is my view code:
echo $this->Form->create('Use', array('type' => 'file'));
echo $this->Form->input('Use.photo', array('type' => 'file'));
echo $this->Form->input('Use.desc', array('rows' => '3', 'label' => 'Description'));
echo $this->Form->end('Add to Gallery');
And here is my controller code:
public function add() {
if ($this->request->is('post')) {
$this->Use->set('user_id', $this->Auth->user('id'));
if ($this->Use->save($this->request->data)) {
$this->Session->setFlash('Your Use has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your Use.');
}
}
}
The plugin doesn't work in only one model. You can add more uploader into your site.
The model seems to be good, I suggest you to see into your form to see if you have create the form in the right way into your view (is imporant to put into your form: 'type' => 'file'
example:
echo $this->Form->create('Product', array ('class' => 'form', 'type' => 'file'));
echo $this->Form->input('ModelImage.filename', array('type' => 'file'));
echo $this->Form->submit('Add Image', array('id'=>'add_image'));
echo $this->Form->end();
Or the problem is the name Use try to change the name with another
After checking thru the code with Alessandro [thank you :)] I found the problem.
If you look in the View and Controller code you can see that the model is named 'Use'. This was the problem, as Use is a loaded word in PHP and I shouldn't have used it for a model name.
I renamed the model to Outcome and now the Uploader works perfectly.

CakePHP Cache Config always reverts to File (from Memcache or APC)

Firstly both Memcache and APC are installed successfully using PECL (and are verified as working) on my Macbook Pro dev server running PHP 5.3.15
I've just baked a test Cake 2.2.2 app. In Config/core.php I have the following:
/**
* Pick the caching engine to use. If APC is enabled use it.
* If running via cli - apc is disabled by default. ensure it's available and enabled in this case
*
*/
$engine = 'File';
if (extension_loaded('apc') && function_exists('apc_dec') && (php_sapi_name() !== 'cli' || ini_get('apc.enable_cli'))) {
$engine = 'Apc';
}
if (extension_loaded('memcache') && php_sapi_name() !== 'cli') {
$engine = 'Memcache';
}
// In development mode, caches should expire quickly.
$duration = '+999 days';
if (Configure::read('debug') >= 1) {
$duration = '+10 seconds';
}
// Prefix each application on the same server with a different string, to avoid Memcache and APC conflicts.
$prefix = 'mcmyapp_';
/**
* Configure the cache used for general framework caching. Path information,
* object listings, and translation cache files are stored with this configuration.
*/
Cache::config('_cake_core_', array(
'engine' => $engine,
'prefix' => $prefix . 'cake_core_',
'path' => CACHE . 'persistent' . DS,
'serialize' => ($engine === 'File'),
'duration' => $duration
));
/**
* Configure the cache for model and datasource caches. This cache configuration
* is used to store schema descriptions, and table listings in connections.
*/
Cache::config('_cake_model_', array(
'engine' => $engine,
'prefix' => $prefix . 'cake_model_',
'path' => CACHE . 'models' . DS,
'serialize' => ($engine === 'File'),
'duration' => $duration
));
Cache::config('default', array(
'engine' => $engine,
'serialize' => ($engine === 'File'),
'duration'=>$duration,
'prefix'=>$prefix,
));
debug(Cache::settings());
The output of the above final debug() is:
array(
'prefix' => '*****',
'engine' => 'Memcache',
'serialize' => false,
'duration' => (int) 10,
'servers' => array(
(int) 0 => '127.0.0.1'
),
'compress' => false,
'persistent' => true,
'probability' => (int) 100,
'groups' => array()
)
So in core.php it appears that Cake wants to use Memcache for its caching engine.
However, when I visit localhost/myapp/pages/home in my browser, CakePHP's default, freshly-baked welcome page greets me, informing me that The FileEngine is being used for core caching
Anywhere else in my app (other than in core.php) if I debug(Cache::settings()), I get the following:
array(
'prefix' => '*****',
'engine' => 'File',
'serialize' => false,
'duration' => (int) 10,
'servers' => array(
(int) 0 => '127.0.0.1'
),
'compress' => false,
'persistent' => true,
'probability' => (int) 100,
'groups' => array()
)
Basically, CakePHP appears to revert to File for caching, ignoring my configuration in core.php to use Memcache... unless I've done something wrong here??!
I've tried this with Configure::write('debug', 0) and Configure::write('debug', 1).
I should mention that with the above, both APC and Memcache extensions are enabled in php.ini.
Any help would be gratefully received, thanks in advance.
Memcached should be on some port, default one is 11211 (check)... So your config tries to connect to Memcached, fails and reverts back to default (File) cache...
Try:
Cache::config('default', array(
'engine' => 'Memcache',
'duration'=> 3600,
'probability'=> 100,
'servers' => array('127.0.0.1:11211') <<<<==
));

Different session timeouts for admin and users in CAKEPHP 2.0. is it possible?

I am using cakephp 2.0 for developing a website. Site will contain only 2 types of users. Admin and users(customers). now I need to set session time out 10 minutes for customer and 1 hour for admin. is it possible ?. My core.php file contains the line like this,
Configure::write('Session', array(
'defaults' => 'php',
'cookie' => 'xyz',
'timeout' => 10,
'checkAgent' => false,
));
It is more correct you do this condition in the AppController than to change your core.php this magnitude. You can make only this:
Configure::write('Session.timeout', 60);
And if you're using Acl, verify if user is admin using his own Acl method instead strripos.
I added the following lines in core.php file. For quick fix
if(strripos($_SERVER['REDIRECT_URL'],"admin/"))
{
Configure::write('Session', array(
'defaults' => 'php',
'cookie' => 'xyz',
'timeout' => 60,
'checkAgent' => false,
));
}
else
{
Configure::write('Session', array(
'defaults' => 'php',
'cookie' => 'xyz',
'timeout' => 10,
'checkAgent' => false,
));
}

Resources