Category: Routing

Q: In Cakephp 3, how to detect user from another site?

I want to have a method before AppController (beforeFilter) to detect if a user has come to my site via another site. If so, from another site, the method will redirect them to a welcome page.

  public function beforeFilter(Event $event) {
      parent::beforeFilter($event);
      $availableUrls = array(
		‘http://foo.com/news/’,
		‘http://foo.com/users/1‘,
		‘http://foo.com/users/2',
		// … like this …
      ); //is there a better way?
      if (!in_array($this->referer(), $availableUrls)) {
        $this->redirect([‘controller’ =>’landing’, ‘action’ => ‘welcome’]);
      }
  }

Can anyone help? Thanks.

SOLVED detect url domain
2 Answers

Try the following code:

if (parse_url($this->referer(), PHP_URL_HOST) != 'foo.com') {
    //catch-all, if so do something
}
Accepted by AJhamper

This will do the trick:

if ($this->request->domain() != 'foo.com') {
    $this->redirect([‘controller’ =>’landing’, ‘action’ => ‘welcome’]);
}

Created: 6 Sep '16

Last Reply: 6 Sep '16

Replies: 2

Views: 623

Votes: 0

Welcome to Aero Coding!

Aero Coding is a CakePHP-focused Q&A community for professional and enthusiast cake bakers. It's built and run by you as part of the community.


Join Now Tour

Download Cakephp

Start baking your own CakePHP application!


Cakephp All Versions