Skip to content

Instantly share code, notes, and snippets.

@wouterds
Last active December 10, 2015 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wouterds/4393797 to your computer and use it in GitHub Desktop.
Save wouterds/4393797 to your computer and use it in GitHub Desktop.
Shurl CakePHP AuthorsController
<?php
App::uses('AppController', 'Controller');
/**
* Authors Controller
*
* @property Author $Author
*/
class AuthorsController extends AppController {
public static function getId() {
// Generate Author or update user agent current one
return AuthorsController::generate();
}
public static function generate() {
$author = new Author();
$ip = $_SERVER['REMOTE_ADDR'];
$ua = $_SERVER['HTTP_USER_AGENT'];
$authorId = $author->find(
'first',
array(
'conditions' => array(
'Author.ip' => $ip
)
)
);
if(count($authorId) > 0) {
$authorId = $authorId['Author']['id'];
$author->id = $authorId;
$data = array(
'Author' => array(
'ua' => $ua
)
);
$author->save($data);
}
else {
$data = array(
'Author' => array(
'ip' => $ip,
'ua' => $ua
)
);
$return = $author->save($data);
$authorId = $return['Author']['id'];
}
return $authorId;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment