Showing posts with label CakePHP. Show all posts
Showing posts with label CakePHP. Show all posts

Monday, July 1, 2013

Forcing some urls to HTTPS and some on HTTP using cakephp

Hi guys, today I was trying to do this task and did a lot of R&D. Tried some httaccess rules, tried cakephp routers and most specially which did my half work ( forcing some urls to https ) is security component of cakephp. But non of these components and techniques worked well like I wanted to do it. Then I wrote a simple function and it worked well.

Here is I am gonna show you how to redirect some urls like checkout, login, register on HTTPS and vice virsa. Here is that function,

/*
    redicting some urls to https
*/
   function securityrules()
   {
       $arrparams = $this->params;
       if( $_SERVER["HTTPS"] == "on" )
       {
          if( $arrparams['action'] != "login" && $arrparams['action'] != "register" && $arrparams['action'] != "reset" )
          {
             $this->redirect('http://' . env('SERVER_NAME') . $this->here);
          }
       }
       else
       {
          if( $arrparams['action'] == "login" || $arrparams['action'] == "register" || $arrparams['action'] == "reset" )
          {
             $this->redirect('https://' . env('SERVER_NAME') . $this->here);
          }
        }
    }

And for using it place this function in AppController.php and call it at the start of beforeFilter() function And enjoy. Don't waste your time on other stupid techniques like i wasted my whole day :( Thanks