Search code examples
cakephpreferer

getting referer from auth in cakePHP


I have a link on the main page that is only accessible if they are logged in. However, if this link is clicked, I want to show a custom error message on the login page (a custom 'Message.auth').

i.e. I want (pseudo code)

if (referer == '/users/reserve'){
    Message.auth = 'Please log in to reserve tickets';
}
else {
    Message.auth = 'Please log in to access that page';
}

Where would I put this bit of code?


Solution

  • Provided you have auth flash messages being output in the login view, this should work:

    // login action of users_controller.ctp
    if ($this->Session->check('Auth.redirect')
     && $this->Session->read('Auth.redirect') == '/users/reserve') {
      $this->Session->write('Message.auth', 'Please log in to reserve tickets');
    }