Best Way To Work Between Https And Http

Hi

I’ve been using this link to show some pages, like login / register through https. Which works fine, my problem arises when I want to go back to another page that doesn’t necessarily need https as i’m redirected to the https version (Guessing as i’m using relative links). What is the best way to overcome this? Just .htaccess the pages you want to be redirected the https?

Jonny

You can do a redirect to https or http in your base controller class. Add there init() method with body something like below:




public function init() {

  parent::init();

  // actions that have to be reirected to https

  $https = array('user/login', 'user/register');

  $current_location = Yii::app()->controller->id . '/' . Yii::app()->controller->action->id;

  if (in_array($current_location, $https)) {

    // redirect to https

  } else {

    // redirect to http

  } 

}

Remeber to call init() method for classes that are extending your base controller class.

Thank you.

I had a little trouble implementing that though, so I went for this, which almost works.

I have a HttpsFilter.php


<?php // For displaying HTTPS pages


class HttpsFilter extends CFilter {

    protected function preFilter( $filterChain ) {

        if ( !Yii::app()->getRequest()->isSecureConnection ) {

            # Redirect to the secure version of the page.

            $url = 'https://' .

                Yii::app()->getRequest()->serverName .

                Yii::app()->getRequest()->requestUri;

                Yii::app()->request->redirect($url);

            return false;

        }

        return true;

    }

}

and a HttpFilter.php


<?php // For displaying HTTP pages


class HttpFilter extends CFilter {


    protected function preFilter( $filterChain ) {

        if ( Yii::app()->getRequest()->isSecureConnection ) {

            # Redirect to non ssl version of the page.

                $url = 'http://' .

                Yii::app()->getRequest()->serverName .

                Yii::app()->getRequest()->requestUri;

                Yii::app()->request->redirect($url);

            return false;

        }

        return true;

    } 

}

in my components. One for each protocol.

I’ve been using filters to specify https




/**

	* Filter for HTTPS

	*/

	public function filters()

	{

	    return array(

	        'https +login', // Force https, but only on login page

	        'https +logout',

	    );

	}



But I seem to have to explicitly list a page as ‘http’ in the filters() for it to be redirected. Is there a way I don’t have to list a page as http in filters?