This is a simple function to minimize JS code, it’s very simple and probably fail with more complex cases. But hey, might be useful for someone for something! We would suggest using JSMin::minify() instead.
CakePHP – Simple User Registration
After doing some initial readings on different PHP frameworks, including CakePHP, CodeIgniter, Symphony, Zend. I’m digging deeper into CakePHP with an open eyes for fresh ideas and other developments. To start, CakePHP is slow, quite slow (backed up by Rasmus’s benchmark at froscon). It also has not taken advantage of more object-oriented syntax and methods. That said, there are many neat ideas in CakePHP that we can learn to incorporate into our in-house high performance framework (but with less features and automagic).
Framework Performance
To those who says that “slow frameworks is acceptable or understandable”: good framework scales both in term of performance and functionality. It should be easily extensible from its tiny core. Software that does not use any framework cannot scale to provide more functionality and can offer good performance only at a simple task. Enough rants, let’s get dirty.
First App
This is a simple user registration. Cake does have its way of doing things and you might or might not like it. I’m not a big fan of $validate, $uses, and such. I do hope CakePHP will use more PHP5 and OOP the sooner the better since it’s a good framework.
User Model
/** validate only run through Model->save() or Model->validates() */
function validateConfirmPassword($data) {
if ($this->data[‘User’][‘password’] == AuthComponent::password($this->data[‘User’][‘password_confirm’])) {
return true;
} // fi
return false;
}
/** check username taken or not */
function beforeValidate() {
if (empty($this->id)) { // being created, check for existing username
$vCond = array(‘User.username’=>$this->data[‘User’][‘username’]);
if ($this->find(‘count’,array(‘conditions’=>$vCond))>0) { // taken
$this->invalidate(‘username_taken_error’);
return false;
} // fi
} // fi
return true;
}
}
?>
User Controller
/** comment */
function login() {}
/** comment */
function logout() {
$this->redirect($this->Auth->logout());
}
function register() {
$this->Auth->logout();
if (!empty($this->data)) {
$this->User->create();
if ($this->User->save($this->data)) {
$this->Auth->login($this->data); // autologin
$this->redirect(array(‘action’=>’index’));
} // fi
} // fi
}
}
?>
View – register.ctp
Improve security for apc.php
If you’re running APC with PHP, you have the option to download apc.php to view, monitor the usage/stats. However, the default authentication is very open. Without any credential, anyone can view the running stats and also the cached filenames (a simple search on “APC INFO” will show some site running APC). The login is to view per-directory file caching. So if you want to require login credential on ALL access change this code:
if (!isset($_SERVER[‘PHP_AUTH_USER’]) ||
!isset($_SERVER[‘PHP_AUTH_PW’]) ||
$_SERVER[‘PHP_AUTH_USER’] != ADMIN_USERNAME ||
$_SERVER[‘PHP_AUTH_PW’] != ADMIN_PASSWORD) {
Header(“WWW-Authenticate: Basic realm=\”APC Login\””);
Header(“HTTP/1.0 401 Unauthorized”);
echo <<
Rejected!
Wrong Username or Password!
Continue…