-1

I have a PHP function:

function http_protocol()
{
  return ($_SERVER['SERVER_PORT'] == 443) ? 'https' : 'http';
}

However, when the domain I am originating from is only HTTP, and I want to dynamically link to a domain that is HTTPS, the conditional returns http.

So, my thought is that I could check to see if the site responds to an HTTPS request, and if so, then return https, else return http.

I was thinking it could be doable using the @get_headers array, and check to see if the [0] value had HTTPS, but I wasn't able to confirm if an HTTPS header existed (e.g. HTTPS/1.1).

I was following this question: How can I check if a URL exists via PHP?, except I would be checking for HTTPS.

Is my logic flow correct, or is there something I'm missing?

Edit: in the particular example, I would only be checking sites I have control over. So I know beforehand if content is served over HTTPS or not.

Community
  • 1
  • 1
Tiffany
  • 680
  • 1
  • 15
  • 31
  • 1
    That an url exists on https doesn't mean it's the same page/content. Might as well be a completely different website or an error page. – Elwin Arens Jun 02 '16 at 19:01
  • 1
    It sounds like you want to check a remote site from PHP? But what you're doing is seeing if the client accessing your side is using https (or at least using port 443). – Jonnix Jun 02 '16 at 19:01
  • 1
    What are you trying to do? And why can't you just use https? – PeeHaa Jun 02 '16 at 19:03
  • 1
    Your code returns 'http' or 'https' depending on the request **your own** server is currently serving. That won't tell you much about another site. I suppose you could try and carry out a request to a `https` version of a URL on another website and see if that returns a 200 OK response – Jonathon Jun 02 '16 at 19:04
  • In my environment, two domains are created in php, for two different websites. One domain uses HTTPS (and IIS has a URL Rewrite rule to redirect HTTP traffic to HTTPS), and the other domain uses HTTP. I want to link to the HTTPS domain from the HTTP domain using: = http_protocol().SITE_DOMAIN.'/url/path/here/' ?> but the http_protocol() function would generate http, instead of https. – Tiffany Jun 02 '16 at 19:07
  • My devil's advocate is telling me it's a waste of time to worry about this, but I'm also just curious if this is possible. Not necessarily to use in a production environment, but to know whether or not my logic is sound. – Tiffany Jun 02 '16 at 19:08
  • @Jonathon Correct. But, both sites run on the same web server. I want to say the PHP code generates the URLs for each of the sites, because they are not separate sites in IIS, but I'm not finding where the URLs are set in the code. In the particular scenario I would want to use this in, I would be checking between two sites I have absolute control over, code and server. – Tiffany Jun 02 '16 at 19:24
  • I think he is trying to say that when you go to certain sites with https, if that site doesnt have a certificate installed it will not show the web site because of the lack of certificate. – pogeybait Jun 02 '16 at 19:51
  • I'm not sure what you're trying to achieve. If you know that Site A uses https and Site B uses http, you should just link to them as they are. They're treated as separate sites and have no need to function co-dependently. You could always just link to the http version and let the redirect take care of forwarding the request to the https version. Again, it all depends on what your goal is with it, because to me it seems like you're trying to do something unnecessarily. – Jonathon Jun 02 '16 at 20:23
  • As per your question though, theoretically, to see if a URL accepts https requests you could of course try to access it using the method you've suggested and check to make sure the HTTP response is 200 (200 means that the request was okay and you can assume that this page exists). I'm not sure why you would need to do this though. – Jonathon Jun 02 '16 at 20:25
  • The original website code was written by a vendor. In a lot of places they use dynamically generated URLs in links. So, this came up as a question in my head while I was updating a URL in a menu link. I was going to just change the URL and be done with it, but pondered making the URL dynamic like other URLs I've seen. Mostly to see if I could do it correctly. I saw the http_protocol function wouldn't work in the scenario I was wanting to (because of HTTP/HTTPS, even though the redirect handles it), so I was curious if it could be done, possibly to use, but more likely not. – Tiffany Jun 03 '16 at 13:24

1 Answers1

0

You can check with using curl library.

  $ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_exec($ch);
curl_close($ch);
if(curl_getinfo($ch)['http_code'] == 200){existing } 
Walker
  • 131
  • 2
  • 16