Facing issue in implementing ssl redirect plugin

Question

I was trying to implement an SSL redirect plugin, I am facing issues. because it is not working. suggest me an idea, how to implement it.
And what is wrong with the code.

<?PHP

class SSLRedirect {
    private $redirect;
    private $uri;
    private $domain;
    private $current_domain;

function __construct() {
    $this->uri = $_SERVER['REQUEST_URI'];
    $this->domain = $_SERVER['HTTP_HOST'];
    $this->current_domain = get_site_url();
    add_action( 'admin_init', array($this, 'ssl_register_settings'));
    add_action( 'admin_menu', array($this, 'ssl_register_options_page'));
    add_action( 'send_headers', array($this, "add_header"));
}

public function add_header(){
    if($this->current_domain == "http://". $this->domain || $this->current_domain == "https://". $this->domain){
        if (isset($_SERVER['HTTPS'])){
            $this->redirect = get_option('ssl-redirect-check');
        
            if ( 'on' === strtolower( $_SERVER['HTTPS'] && strpos( home_url(), 'https' ) == false )) {
                //wp_redirect(str_replace( 'http', 'https', home_url() ));
                header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], true, 301);
            }
            if ( '1' == $_SERVER['HTTPS'] ) {
                wp_redirect(str_replace( 'http', 'https', home_url() ));
            }
        } else {
            if ($this->redirect == "yes"){ 
                header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], true, 301);
            }
        }
    } else {
        Header( "HTTP/1.1 301 Moved Permanently" );
        Header( "Location: ". $this->current_domain . $this->uri );
        die();
    }
}

public function ssl_register_options_page() {
    add_options_page('SSL Redirect', 'SSL Redirect', 'manage_options', 'ssl_redirect', array($this, 'ssl_redirect_plugin_options_page'));
}

public function ssl_register_settings() {
   
   add_option( 'ssl-redirect-check', '');
   
   register_setting( 'ssl_option_group', 'ssl-redirect-check', 'hsts-callback' );
   
}

public function ssl_redirect_plugin_options_page() {
    $redirect = get_option('ssl-redirect-check');
    $redirect_checked = ($redirect == "yes") ? 'checked="checked"' : 'no';
    ?>
    <div>
    <h2>HSTS Plugin Options</h2>
        <form method="post" action="options.php">
            <?php settings_fields( 'ssl_option_group' ); ?>
            <table>
                <tr valign="top">
                    <td><label for="ssl-redirect-check"><input type="checkbox" id="ssl-redirect-check" name="ssl-redirect-check" value="yes" <?php echo $redirect_checked; ?>> Include redirect</label></td>
                </tr>
                
            </table>
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}

}

if( is_admin() )
    $ssl_redirect = new SSLRedirect();

I was getting issues with the add-header() function. Please suggest what should I do there.
And The major portion I was getting stuck to auto redirect when my site in HTTP then it will automatically redirect to HTTPS.

0
sunfarma hulla 2 months 0 Answers 15 views 0

Leave an answer