LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2005, 09:55 PM   #1
michaelsanford
Member
 
Registered: Feb 2005
Location: Ottawa/Montréal
Distribution: Slackware + Darwin (MacOS X)
Posts: 468

Rep: Reputation: 30
Check if URL is SSL or not in PHP


I want to try to check if the page being loaded is being loaded via SSL or not.

I just for SSL working on my Apache 1.3 server and I wanted to insert checks at the index pages of my various apps, like phpMyAdmin, WordPress (admin), to check whether the page was accessed via an https:// link, and if not, you could be redirected to the encrypted page.

I might add that an .htaccess directive, if one exists, would also work for me since all the sensetive files and apps are in their own folders.

I've noticed, however, that there's no such URL checking possible in PHP using the superglobals, like HTTP_* or _SERVER[], since none of those bother to prefix the URL with the protocol. Is there another way to go about it ?

Failing this, I suppose I could always make a subdomain 'admin.' that's only served by an SSL VirtualHost (that doesn't even listen on :80), right ?
 
Old 09-29-2005, 06:54 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
I just for SSL working on my Apache 1.3 server and I wanted to insert checks at the index pages of my various apps, like phpMyAdmin, WordPress (admin), to check whether the page was accessed via an https:// link, and if not, you could be redirected to the encrypted page.

[..snip..]

I've noticed, however, that there's no such URL checking possible in PHP using the superglobals, like HTTP_* or _SERVER[], since none of those bother to prefix the URL with the protocol. Is there another way to go about it ?
That's a mistake. You can do this very well with _SERVER[]:
PHP Code:
if ($_SERVER['HTTPS'] != "on") {
    
$url "https://"$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    
header("Location: $url");
    exit;

Note: Redirection to the https URL is done above with header(). This will only work if no single (html) output is done before calling header(). In other words: you need to do this before any html-tag, and before any PHP echo or print. This is probably what you want anyway, so this shouldn't be a real problem. Just keep it in mind when it doesn't seem to work.

Last edited by Hko; 09-29-2005 at 07:41 PM.
 
Old 09-29-2005, 08:32 PM   #3
michaelsanford
Member
 
Registered: Feb 2005
Location: Ottawa/Montréal
Distribution: Slackware + Darwin (MacOS X)
Posts: 468

Original Poster
Rep: Reputation: 30
I feel like such a n00b. I was confusing the various SERVER_* variables with the _SERVER[] array.

Thanks for catching me on that one

EDIT: Now that I look back at what phpinfo() outputs, the reason I initially assumed there would beno relevant variable is that the string "https" doesn't appear anywhere on the page.

Last edited by michaelsanford; 09-29-2005 at 08:33 PM.
 
Old 04-14-2010, 07:34 AM   #4
Web31337
Member
 
Registered: Sep 2009
Location: Russia
Distribution: Gentoo, LFS
Posts: 399
Blog Entries: 71

Rep: Reputation: 65
There is no such option "out-of-box" if you run PHP as fastcgi server(php-cgi) and connect it with nginx webserver.
If you want to check if it is SSL-connected you may wish to add the following line to your fastcgi params list:
Code:
fastcgi_param  HTTPS		  $ssl_protocol;
or $ssl_cipher variable instead of $ssl_protocol.
Hope it helps someone(I know this thread is 5-years old but still it's on top of google from "php determine if ssl enabled").
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting the url with PHP linux_pioneer Linux - General 2 06-20-2004 02:08 PM
Can you verify a url with php? digitalgravy Programming 4 03-01-2004 01:38 PM
gkrellm mail check + ssl wr3ck3d Linux - Software 2 01-21-2004 10:16 PM
PHP Url? HappyDude Programming 2 09-20-2003 04:20 PM
Check URL if accepting incomming on port 80 mithras Linux - General 4 07-25-2003 06:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration