0

I have a website in Zend framework. Here I want to identify whether the current URL contains HTTPS or HTTP? I have used the following code

if($_SERVER['HTTPS']==on){ echo "something";}else{ echo "something other";}

But the result is not correct. Is there is any other way to identify this? Also I have one more question. How to get complete current url (including HTTP/HTTPS) using php?

Please help me

Thanks in advance

5 Answers 5

13
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") { 
    echo "something";
} else { 
    echo "something other";
}

notice the on should be a string .

4
  • I've been looking for the same solution for a while now, but on my server I get Undefined index: HTTPS, even when I'm surfing on https
    – Jorre
    Sep 25, 2011 at 20:05
  • see the answer bellow, and check the port Sep 27, 2011 at 18:30
  • you should check for $_SERVER['HTTPS'] to be set before accessing it.
    – thwd
    Apr 20, 2012 at 9:34
  • @Tom thanks for pointing it out, however i believe we should let users do their homework and give them only guide lines ( where possible ) that can be applied to other questions too . Apr 20, 2012 at 9:44
13

You could use methods that are already defined in Zend Framework instead of explicitly using $_SERVER superglobals.

To determine if the connection is HTTP or HTTPS (this code should go into your controller):

if ( $this->getRequest()->isSecure() ) { echo 'https'; } else { echo 'http'; }

To get complete current url:

$this->getRequest()->getScheme() . '://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getRequestUri();
4

The better way to check is

if (isset($_SERVER['HTTPS']) && $_SEREVER['HTTPS'] != 'off')
{
  //connection is secure do something
}
else
{
  //http is used
}

As stated in manual

Set to a non-empty value if the script was queried through the HTTPS protocol.

Note: Note that when using ISAPI with IIS, the value will be off if the

request was not made through the HTTPS protocol.

3

you need to fix check it should be

if ($_SERVER['HTTPS'] == 'on')

or try following function

if(detect_ssl()){ echo "something";}else{ echo "something other";}

function detect_ssl() {
return ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1 || $_SERVER['SERVER_PORT'] == 443)
}
0
0

This will both check if you're using https or http and output the current url.

$https = ((!empty($_SERVER['HTTPS'])) && ($_SERVER['HTTPS'] != 'off')) ? true : false;

if($https) {
    $url = "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
} else {
    $url = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.