473,378 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

SSL php code

I am using linux, php and trying to do some ssl code. I want to send some identifier that connects
my database to the client browser. I was looking into a secure cookie. I only want the page
accessed over a SSL connection. Whats the best way to insure the clinet browser can only see my
page if they are ssl connected to it? In adding a 1 to the end of my send cookie code, it sends
the cookie regardless of if they are ssl connected or not. Any ideas???

TIA,
Rob

Jul 17 '05 #1
10 6943
Spam Bill Gates wrote:
I am using linux, php and trying to do some ssl code. I want to send some identifier that connects
my database to the client browser. I was looking into a secure cookie. I only want the page
accessed over a SSL connection. Whats the best way to insure the clinet browser can only see my
page if they are ssl connected to it? In adding a 1 to the end of my send cookie code, it sends
the cookie regardless of if they are ssl connected or not. Any ideas???


if (!$_SERVER['HTTPS']) {
die();
}

That should work. Haven't tried it though, but the server HTTPS variable
should be set if the connection is SSL, atleast with Apache and mod_ssl.
Can't say about IIS, but if you do a print_r($_SERVER) on a page through
SSL you'll find out soon enough :)
Roy W. Andersen
--
ra at broadpark dot no / http://roy.netgoth.org/

"Hey! What kind of party is this? There's no booze
and only one hooker!" - Bender, Futurama
Jul 17 '05 #2
On Mon, 10 Jan 2005 20:29:18 +0100, "Roy W. Andersen"
<ro******@netgoth.org> reverently intoned upon the aether:
Spam Bill Gates wrote:
I am using linux, php and trying to do some ssl code. I want to send some identifier that connects
my database to the client browser. I was looking into a secure cookie. I only want the page
accessed over a SSL connection. Whats the best way to insure the clinet browser can only see my
page if they are ssl connected to it? In adding a 1 to the end of my send cookie code, it sends
the cookie regardless of if they are ssl connected or not. Any ideas???


if (!$_SERVER['HTTPS']) {
die();
}

That should work. Haven't tried it though, but the server HTTPS variable
should be set if the connection is SSL, atleast with Apache and mod_ssl.
Can't say about IIS, but if you do a print_r($_SERVER) on a page through
SSL you'll find out soon enough :)
Roy W. Andersen


This is not necessarily true. Even using Apache and mod_ssl does not
guarantee this. There are many configuration issues involved, and it
may well show up by default, but it does not show up on the servers I
use. Albeit, I outsource hosting so I have limited control and less
liabilities (I am not responsible for making the system work, just my
code).

So if, if( isset( $_SERVER['HTTPS']) ) != true, does not work for you,
then I would simply suggest ensuring https is used via a redirect:

$site = 'www.whereever.net';

$ssl_Secure_SCRIPT_URI = "https://$site" . $_SERVER[PHP_SELF];

if( @strcasecmp($_SERVER[SCRIPT_URI], $ssl_Secure_SCRIPT_URI) != 0 ){
header("location:$ssl_Secure_SCRIPT_URI"); // Redirect browser
exit;
}

Put the above in an include file and require_once() this include file
in every page requiring encrypted access.

Some things to note:

1) This method does not look at GET data in the request. Hence, if
someone hacks out the 's' in the URI, then they get redirected to the
same page without any GET or POST data from the request. This is
okay, we are in an error state.

2) You might consider logging a protocol failure whenever the redirect
is needed.

3) $site would be best included from another file so that the domain
name for the site is only written down in exactly one location in the
entire we application.

4) Is this the best way? Probably not. Does it work? Yes. Does it
work in a restrictive environment (think shared hosting from a service
provider)? Yes.

5) The '@' used above is the "Error Control Operator" and silences any
error output by the function call. This serves two purposes:

a) The client never sees an error message.

b) No text is printed to the HTML client before the header call is
made. If any text (i.e., an error message) is output before the
header call, the header call will fail, and hence the redirect will
fail too.

I would enjoy hearing any constructive criticism of this method.

hope this helps,

Sean
"In the End, we will remember not the words of our enemies,
but the silence of our friends."

- Martin Luther King Jr. (1929-1968)

Photo Archive @ http://www.tearnet.com/Sean
Last Updated 29 Sept. 2004
Jul 17 '05 #3
>On Mon, 10 Jan 2005 20:29:18 +0100, "Roy W. Andersen"
<ro******@netgoth.org> reverently intoned upon the aether:
Spam Bill Gates wrote:
> I am using linux, php and trying to do some ssl code. I want to send some identifier that connects
> my database to the client browser. I was looking into a secure cookie. I only want the page
> accessed over a SSL connection. Whats the best way to insure the clinet browser can only see my
> page if they are ssl connected to it? In adding a 1 to the end of my send cookie code, it sends
> the cookie regardless of if they are ssl connected or not. Any ideas???


if (!$_SERVER['HTTPS']) {
die();
}

That should work. Haven't tried it though, but the server HTTPS variable
should be set if the connection is SSL, atleast with Apache and mod_ssl.
Can't say about IIS, but if you do a print_r($_SERVER) on a page through
SSL you'll find out soon enough :)
Roy W. Andersen


This is not necessarily true. Even using Apache and mod_ssl does not
guarantee this. There are many configuration issues involved, and it
may well show up by default, but it does not show up on the servers I
use. Albeit, I outsource hosting so I have limited control and less
liabilities (I am not responsible for making the system work, just my
code).

So if, if( isset( $_SERVER['HTTPS']) ) != true, does not work for you,
then I would simply suggest ensuring https is used via a redirect:

$site = 'www.whereever.net';

$ssl_Secure_SCRIPT_URI = "https://$site" . $_SERVER[PHP_SELF];

if( @strcasecmp($_SERVER[SCRIPT_URI], $ssl_Secure_SCRIPT_URI) != 0 ){
header("location:$ssl_Secure_SCRIPT_URI"); // Redirect browser
exit;
}

Put the above in an include file and require_once() this include file
in every page requiring encrypted access.

Some things to note:

1) This method does not look at GET data in the request. Hence, if
someone hacks out the 's' in the URI, then they get redirected to the
same page without any GET or POST data from the request. This is
okay, we are in an error state.

2) You might consider logging a protocol failure whenever the redirect
is needed.

3) $site would be best included from another file so that the domain
name for the site is only written down in exactly one location in the
entire we application.

4) Is this the best way? Probably not. Does it work? Yes. Does it
work in a restrictive environment (think shared hosting from a service
provider)? Yes.

5) The '@' used above is the "Error Control Operator" and silences any
error output by the function call. This serves two purposes:

a) The client never sees an error message.

b) No text is printed to the HTML client before the header call is
made. If any text (i.e., an error message) is output before the
header call, the header call will fail, and hence the redirect will
fail too.

I would enjoy hearing any constructive criticism of this method.

hope this helps,

Sean
"In the End, we will remember not the words of our enemies,
but the silence of our friends."

- Martin Luther King Jr. (1929-1968)

Photo Archive @ http://www.tearnet.com/Sean
Last Updated 29 Sept. 2004


Sean I am planning on exclusievely using secure pages (ssl) after the user requests to login.

I want some standardized php code on every page to verify with every hit that it is being accessed
via ssl.

If it is not no information should be displayed to the user other than a redirect to the login
page. I plan to exclusively use the post method when a user enters data into one of my pages that
will be self referenced to the php page that sent it. The data entered by the user will be pulled
up with the self referenced page and php code will do the appropriate sql insert/update/selects and
display the appropriate results to the user.

If I use your idea will my data thats sent with a post method have no problems if the user uses a
ssl connection to my server?

My limited knowledge at this point makes me think I will not be able to get the data using a post
method to the php code that needs it assuming the user is maintaining a secure connection to my
server if I use your logic.

I hope this makes sence. Is my concern unfounded???

TIA,
Bill Gates
Jul 17 '05 #4
Spam Bill Gates wrote:
<snip>
My limited knowledge at this point makes me think I will not be able to get the data using a post method to the php code that needs it assuming the user is maintaining a secure connection to my server if I use your logic.


This is not a big deal...

if (empty($_SERVER['HTTPS'])
redirect to https page.

If user posts from/to http page, he'll lose the data as he will be
redirected to https page. Try and see. Perhaps you may need to test
with XAMPP.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #5
Spam Bill Gates wrote:

I want some standardized php code on every page to verify with every hit that it is being accessed
via ssl.


As I said, do a print_r($_SERVER); and see if $_SERVER['HTTPS'] is set
when you're connected through SSL. If it is, then the code I posted will
work on that server. If not, chances are the server sets another
variable which you can use to identify the presence of SSL. Just look
for something named anything with SSL or HTTPS, and just to make sure,
see if the variable is also there when you connect without SSL. If it's
not, then you can use that variable to detect SSL.

So just make a page like this:

<?php
print_r($_SERVER);
?>

Open it with SSL, open it without SSL, and compare the two outputs.
Roy W. Andersen
--
ra at broadpark dot no / http://roy.netgoth.org/

"Hey! What kind of party is this? There's no booze
and only one hooker!" - Bender, Futurama
Jul 17 '05 #6
On Wed, 12 Jan 2005 22:53:55 +0100, "Roy W. Andersen"
<ro******@netgoth.org> reverently intoned upon the aether:
Spam Bill Gates wrote:

I want some standardized php code on every page to verify with every hit that it is being accessed
via ssl.


As I said, do a print_r($_SERVER); and see if $_SERVER['HTTPS'] is set
when you're connected through SSL. If it is, then the code I posted will
work on that server. If not, chances are the server sets another
variable which you can use to identify the presence of SSL. Just look
for something named anything with SSL or HTTPS, and just to make sure,
see if the variable is also there when you connect without SSL. If it's
not, then you can use that variable to detect SSL.

So just make a page like this:

<?php
print_r($_SERVER);
?>

Open it with SSL, open it without SSL, and compare the two outputs.


I would expand that a little and include a <pre> tag.

<pre>
<?php
print_r($_SERVER);
?>
</pre>

so that the output is readably formatted.

That said, I have a couple sites using SSL on Apache with mod_ssl and
the way the hosting company set them up there are no variables that
show what type of connection is used. Hence I had to fall back to
checking for https and redirecting if it is missing.

hth,

Sean
"In the End, we will remember not the words of our enemies,
but the silence of our friends."

- Martin Luther King Jr. (1929-1968)

Photo Archive @ http://www.tearnet.com/Sean
Last Updated 29 Sept. 2004
Jul 17 '05 #7
On Wed, 12 Jan 2005 20:26:07 GMT, Spam Bill Gates
<sp***********@microsoft.com> reverently intoned upon the aether:

Sean I am planning on exclusievely using secure pages (ssl) after the user requests to login.
While all your links may be https://... there is no way to guarantee
that a user will no change the address bar to reference the page as
http://...

I want some standardized php code on every page to verify with every hit that it is being accessed
via ssl.

If it is not no information should be displayed to the user other than a redirect to the login
page.
See the code snippet below. This will securely redirect to a login
page. My original code snippet simply redirected the browser back the
same page using ssl rather than unencrypted communications.
I plan to exclusively use the post method when a user enters data into one of my pages that
will be self referenced to the php page that sent it. The data entered by the user will be pulled
up with the self referenced page and php code will do the appropriate sql insert/update/selects and
display the appropriate results to the user.
Please make sure you validate all user input or you can end up
creating an easily hacked website with little effort on your part.
You might take a look at:

http://www.owasp.org/documentation/topten.html

as a starting point for securing your web application. All using
HTTPS/SSL does is encrypt the data between the web server and the
client, it in no way provides any security for the site as a whole.

Or visit google and try the following sets of search terms:

SQL Injection
Cross Site Scripting
Web Application Security

A site with more helpful info is:

http://www.securityfocus.com/infocus/foundations

If I use your idea will my data thats sent with a post method have no problems if the user uses a
ssl connection to my server?
Yes, if the user connects using SSL/HTTPS, then the POST data will be
properly transmitted. If they fail to use SSL, then the POST data
will be lost when they are redirected to the login page (see modified
code snippet below).

My limited knowledge at this point makes me think I will not be able to get the data using a post
method to the php code that needs it assuming the user is maintaining a secure connection to my
server if I use your logic.

I hope this makes sence. Is my concern unfounded???


Yes, the POST data will be lost on a redirect. But since the access
to the site was invalid losing the POST data is reasonable (if it is a
possible attack, do not trust the data).
This version of code will redirect a visitor to the login.php page if
they do not connect using SSL.

-------------------------------------------------------------------------------------

$site = 'www.whereever.net';

// Construct the proper https login string for this page.
$ssl_Secure_SCRIPT_URI = "https://$site" . $_SERVER[PHP_SELF];

// Ensure the user accesses this page using the secure URI, otherwise
// redirect them to the login page.
if( @strcasecmp($_SERVER[SCRIPT_URI], $ssl_Secure_SCRIPT_URI) != 0 ){
header("location:https://$site/login.php"); // Redirect browser
exit;
}

-------------------------------------------------------------------------------------

Beyond this, creating a login system in PHP is easy, creating a secure
login and session validation scheme is a lot more challenging.

hope this helps,

Sean


"In the End, we will remember not the words of our enemies,
but the silence of our friends."

- Martin Luther King Jr. (1929-1968)

Photo Archive @ http://www.tearnet.com/Sean
Last Updated 29 Sept. 2004
Jul 17 '05 #8
On 12 Jan 2005 12:36:45 -0800, "R. Rajesh Jeba Anbiah"
<ng**********@rediffmail.com> reverently intoned upon the aether:
Spam Bill Gates wrote:
<snip>
My limited knowledge at this point makes me think I will not be able

to get the data using a post
method to the php code that needs it assuming the user is maintaining

a secure connection to my
server if I use your logic.


This is not a big deal...

if (empty($_SERVER['HTTPS'])
redirect to https page.


Again, this is not necessarily true and whether or not
$_SERVER['HTTPS'] exists is a function of server configuration.
Simply using Apache and mod_ssl does not imply it will be there.
Hence, you can end up coding an infinite loop of redirects to the
secure page. And eventually you will get a rather error message from
Apache or the browser (likely about redirect limits).

enjoy,

Sean
"In the End, we will remember not the words of our enemies,
but the silence of our friends."

- Martin Luther King Jr. (1929-1968)

Photo Archive @ http://www.tearnet.com/Sean
Last Updated 29 Sept. 2004
Jul 17 '05 #9
Sean wrote:
On 12 Jan 2005 12:36:45 -0800, "R. Rajesh Jeba Anbiah"
<ng**********@rediffmail.com> reverently intoned upon the aether:

<snip>
This is not a big deal...

if (empty($_SERVER['HTTPS'])
redirect to https page.


Again, this is not necessarily true and whether or not
$_SERVER['HTTPS'] exists is a function of server configuration.
Simply using Apache and mod_ssl does not imply it will be there.
Hence, you can end up coding an infinite loop of redirects to the
secure page. And eventually you will get a rather error message from
Apache or the browser (likely about redirect limits).


Interesting contradiction. For me, such situation never happened and
I don't have any idea why HTTPS wouldn't get set even on SSL.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #10
On 13 Jan 2005 09:27:45 -0800, "R. Rajesh Jeba Anbiah"
<ng**********@rediffmail.com> reverently intoned upon the aether:
Sean wrote:
On 12 Jan 2005 12:36:45 -0800, "R. Rajesh Jeba Anbiah"
<ng**********@rediffmail.com> reverently intoned upon the aether:

<snip>
This is not a big deal...

if (empty($_SERVER['HTTPS'])
redirect to https page.


Again, this is not necessarily true and whether or not
$_SERVER['HTTPS'] exists is a function of server configuration.
Simply using Apache and mod_ssl does not imply it will be there.
Hence, you can end up coding an infinite loop of redirects to the
secure page. And eventually you will get a rather error message from
Apache or the browser (likely about redirect limits).


Interesting contradiction. For me, such situation never happened and
I don't have any idea why HTTPS wouldn't get set even on SSL.


It may work that way by default, but the hosting company I use is
configured such that it does not show up. Sadly their configuration
contains none of the SSL-HTTPS data. Some of which I would like to
use for session authentication. On the bright side, I can get a
server, domain, name, and secure certificate with decent online
storage and bandwidth for less than $100 US per year so I can survive
a little inconvenience.

enjoy,

Sean
"In the End, we will remember not the words of our enemies,
but the silence of our friends."

- Martin Luther King Jr. (1929-1968)

Photo Archive @ http://www.tearnet.com/Sean
Last Updated 29 Sept. 2004
Jul 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
4
by: jason | last post by:
Hello. Newbie on SQL and suffering through this. I have two tables created as such: drop table table1; go drop table table2; go
16
by: Dario de Judicibus | last post by:
I'm getting crazy. Look at this code: #include <string.h> #include <stdio.h> #include <iostream.h> using namespace std ; char ini_code = {0xFF, 0xFE} ; char line_sep = {0x20, 0x28} ;
109
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any...
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
37
by: Alan Silver | last post by:
Hello, Newbie here, so please forgive what is probably a basic question ... I see a lot of discussion about "code behind", which if I have understood correctly, means that the script code goes...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.