Search code examples
cakephpif-statementcakephp-2.1referer

Cakephp check if referer is from local url


I'm struggeling a litte with my Cakephp 2.1 application.

I want to render a different layout, if the referer is from an external url. Sadly this is not working :

 if($this->referer(null, true))
     {
        $this->layout = 'lightview';
     }
     else 
     {
        $this->layout = 'default';
     }

Any ideas how I can fix this?

Thanks in advance


Solution

  • You got a logical error here:

    $this->referer(null, true) // true as second param: only internal
    

    will only return the lightview layout if

    • there is a referer (not always the case)
    • internal referrer

    So this is probably not what you want.

    You cannot do that this easily. Sometimes there is just no referrer. And even if there is you might still don't know if this person came from somewhere else and just "hid" the referrer. Or then browsed your site (creating internal referrers from here on).

    You would need to store the initial result on first visit in the session to check against later on. But even so your approach is highly inconsistent and very likely breakable...