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

1 comment:

Please feel free to post your comments. If anyone has a good article or good thing to share, just send me that with your name to asadmehmoodstar@gmail.com. and if anyone want so receive updates regarding my blog, he can subscribe to my weekly newsletter on "Subscribe to our mailing list" section.

Thanks