tags:

views:

485

answers:

4

i have this function here that i have in a class

function enable_ssl() {
     if ($_SERVER[HTTPS]!="on") {
     $domain = "https://".$_SERVER['HTTP_HOST']."/".$_SERVER['SCRIPT_NAME'];
     header("Location: {$domain}");
     }
     }

but the problem is when the server doesnt have ssl installed and i have this function initiating the page redirects to a 404 page. i was wondering how i can have this function work only when ssl is installed and working

is it possible?

thanks.

ps: did some google research and couldnt find much of anything.

+2  A: 

On a *nix server, you could try parsing the output of netstat -A inet -lnp for a web server listening on port 443. Kinda clunky.

Better option, I'd say, is to make it a configuration option for the user. Let them tell your app if they've got HTTPS enabled.

ceejayoz
+1 For the latter advice.
Gumbo
A: 

two ideas

  1. Setup a socket connection to port 443 and see if it connects.
  2. Read through an apache config file and see if there's anything listening on that port
SeanDowney
A: 

You can try to connect to the server using curl. However, I would also try to do a config option. If you use the below, make sure you don't cause an infinite loop.

function ignoreHeader($curl, $headerStr)
{
  return strlen($headerStr);
}

$curl = curl_init("https://example.com/");
curl_setopt($curl, CURLOPT_NOBODY, TRUE);
curl_setopt($curl, CURL_HEADERFUNCTION, 'ignoreHeader');
curl_exec($curl);
$res = curl_errno($curl);

if($res == 0)
{
  $info = curl_getinfo($curl);
  if($info['http_code'] == 200)
  {
    # Supports SSL
    enable_ssl();
  }
}
else
{
  # Doesn't.
}
Matthew Flaschen
A: 

I use xampp as my development server on my laptop. I have yet to set up a SSL connection on xampp. My production server does have SSL enabled and also has a valid cert.

I noticed that $_SERVER['HTTPS'] does not exist on my xampp development server, but does exist on my production server.

I am assuming (perhaps incorrectly) that if $_SERVER['HTTPS'] is not set, SSL is not enabled on the server.

<?php

if (isset($_SERVER['HTTPS')) echo 'SSL Exists'
else echo 'No SSL'

?>
mrbinky3000