Https only on latest browsers

Is it possible to enable https only in supported browsers like latest version of chrome and disable https when browsers like Internet Explorer access

hmm maybe try this:

<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];

 if(strpos($useragent, 'Chrome') !== FALSE) { 
header ('Location: https://yoursite.com/');
} elseif(strpos($useragent, 'MSIE') !== FALSE) {
header('Location: http://yoursite.com/');
}
 ?>
1 Like

The above post is incorrect.

This is the working, tested code for you:

<?php
function getBrowser() { 
  $u_agent = $_SERVER['HTTP_USER_AGENT'];
  $version= "";
  

  // Next get the name of the useragent yes seperately and for good reason
  if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)){
    $browsername = "MSIE";
  } elseif(preg_match('/Chrome/i',$u_agent) && !preg_match('/Edge/i',$u_agent)){
    $browsername = "Chrome";
  } elseif(preg_match('/Trident/i',$u_agent)){
    $browsername = "MSIE";
  }

  // finally get the correct version number
  $known = array('Version', $ub, 'other');
  $pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
  if (!preg_match_all($pattern, $u_agent, $matches)) {
    // we have no matching number just continue
  }
  // see how many we have
  $i = count($matches['browser']);
  if ($i != 1) {
    //we will have two since we are not using 'other' argument yet
    //see if version is before or after the name
    if (strripos($u_agent,"Version") < strripos($u_agent,$browsername)){
        $version= $matches['version'][0];
    }else {
        $version= $matches['version'][1];
    }
  }else {
    $version= $matches['version'][0];
  }

  // check if we have a number
  if ($version==null || $version=="") {$version="?";}

  return array(
    'name'      => $browsername,
    'version'   => $version
  );
} 

// now try it
$ua=getBrowser();
$browserversion = round($ua["version"],0,PHP_ROUND_HALF_UP);
if($ua["name"] == "Chrome"){
if($browserversion > 80){
header("Location: https://yoursite.com/");
} else {
header("Location: http://yoursite.com/");
}
} elseif($browserversion == "MSIE") {
header("Location: http://yoursite.com/");
} else {
header("Location: http://yoursite.com/");
}
?>
2 Likes

Might be I’m asking wrong question, but why do you need todo that?

1 Like

Looking at your question literally, it’s not possible. When using HTTPS, the HTTPS connection is established before any information about the visitor’s browser is being transmitted. So it’s not possible to conditionally disable HTTPS for certain devices. Either your website supports HTTPS or it doesn’t.

What you could do is to conditionally enable forced HTTPS redirects. So if someone is visiting your website with Internet Explorer, they are not redirected to HTTPS. And if they are visiting with another browser, then HTTPS is enforced.

But how feasible that is depends on what you’re trying to achieve in the first place.

6 Likes

I am building a website for old as well as new browsers. Old Browsers don’t accept new certificate so, I want to use only http and new browsers like Google chrome blocks http only site downloads (from my other server) so I want to do it.

Ah now i can understand, i usually ask these, sorry if that bothered you.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.