CakePHP 2.3 cookies and Django 1.4 password reset

Published: 2013-05-05
Tagged: django

This is more of a note to self than anything else, really. Time is unforgiving and I get tired of looking up the same sites about how to reset my Django password because I forgot it out of disuse. I'm also exploring the world of cookie and javascript, so I wanted to do a little code dump before these snippets get buried in my coils.

CakePHP 2.3 cookies

$this->Cookie->read('data')

Will read a cookie called 'data'. Fairly simple.

$this->Cookie->check('data')

This will check if a cookie exists. Useful.

this->Cookie->name = 'data';
$this->Cookie->time = 300;
$this->Cookie->path = '/';
$this->Cookie->domain = '127.0.0.1';
$this->Cookie->key = 'data';

This is pretty self explanatory. It creates cookie attributes. What eludes me is the fact that I don't remember if the time is in seconds or milliseconds? I'd bet it's the first one. Now, to actually write that cookie:

$this->Cookie->write('data', 'true', false, 3600)

The first attribute is the key to use, the second the value, the third decided whether the cookie is encrypted (using cake's config.php salt), the last value is the expiry. I guess it's in seconds. What a relief.

I've found this method to be more elegant when handling cookies, but using JavaScript appears to be more elastic and it's a bit less server calls. CakePHP's methods would be good for heavier stuff that has to be handled by controllers, where JS seems good for taking care of those cookies with a lesser responsibility in the grand scheme of things.

To keep things complete, here's how I do cookie using JavaScript:

//get the right date format for cookie
//note how getTime() uses milliseconds instead of seconds
var date = new Date();
date.setTime(date.getTime()+3600000);
expires = 'expires=' + date.toGMTString() + ';';
document.cookie = 'CookieName[key]=value;' + expires + 'path=/;';

Django Password Reset

When using Heroku, use the run command with the --app parameter to invoke a manage.py command like so:

heroku run --app appName python manage.py changepassword admin

Gotta look into Heroku's manual and check how to specify an app so as to ignore using --app every single time.

And that's all folks!

Comments

There aren't any comments here.

Add new comment