Feed items

Handling mobile redirects in .htaccess for iPhones/Androids and tablets

Below is code I put into my .htaccess to set an environment variable to change the content/theme for mobile users.

# Mobile
SetEnvIf User-Agent "Mobile" IS_MOBILE
SetEnvIf User-Agent "Googlebot-Mobile" IS_MOBILE
SetEnvIf User-Agent "BlackBerry" IS_MOBILE
SetEnvIf User-Agent "iPod" IS_MOBILE
SetEnvIf User-Agent "iPhone" IS_MOBILE
SetEnvIf User-Agent "NetFront" IS_MOBILE
SetEnvIf User-Agent "Symbian OS" IS_MOBILE
SetEnvIf User-Agent "Windows Phone" IS_MOBILE

Notice that ‘Android’ is not present in the User-Agent check. While testing with a Galaxy Nexus and Nexus 7, I noticed that the easiest way to tell the difference between Android on phones vs. Android on tablets was to check for ‘Mobile’.
From here I can access IS_MOBILE via PHP like so:

if (isset($_SERVER['IS_MOBILE'])) {
include('index-mobile.php');exit;
}

test

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

CakePHP save/saveAll methods not displaying validation errors

Just thought I’d leave a quick note to myself about validation errors not showing up when I use read() after a save() or saveAll(). The key is to use a conditional for the read() or I believe using find() work as well.
Won’t show validation errors:

if (!empty($this->data)) {
  if ($this->User->saveAll($this->data, array('validate'=>'first'))) {
    $this->Session->setFlash('User saved');
  } else {
    $this->Session->setFlash('User not saved.');
  }
}
$this->data = $this->User->read(null, $id);

Ignoring CakePHP’s tmp/ directory with Subversion

I’ve noticed on many occasions that I’ll start a project with a Subversion repo and immediately add/commit all the files included with Cake’s nightly build including the tmp/ directory.  If you’ve ever done this, you’ll know it’s kind of a pain always having those cache/log files showing up when you commit.  So, for my benefit, I’m posting these few lines of code in order for me to refer to later.
From the app/ directory:

svn export tmp tmpBAK
svn rm --force tmp
svn ci -m 'Removing directory "tmp".'
mv tmpBAK tmp
svn propset svn:ignore 'tmp' .
svn ci -m 'Ignoring "tmp" directory.'

Changing the CakePHP Auth component password hash

In moving some legecy sites over to CakePHP, I’ve come across some situations where I need to the Auth component to ‘hash’ the password differently. For example, just a straight md5() of the password.
To accomplish this, you need to empty your Security.salt variable in /app/config/core.php (Not positive what all this will affect):
Configure::write('Security.salt', '');
and change the hash method of the Security class (I did this in app_controller.php):
function beforeFilter(){
   Security::setHash('md5');
}

Send more data with CakePHP’s observeField()

I just ran into a situation today that required me to have access to more than one form field when doing an ajax request within my CakePHP application. Basically, when someone is typing an address, I want to check Google’s Geocode API for a latitude and longitude coordinate.
The important part is the ‘with’ option:
'with'=>'Form.serializeElements($("CommunityForm").getElements())'
My example:
echo $ajax->observeField('CommunityAddress', array('with'=>'Form.serializeElements( $("CommunityForm").getElements() )','url'=>'geocode','update'=>'geocode'"));