WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Database Discussion Forum This is the place to find help resolving those nagging questions you have about implementing and using all kinds of databases. Need help writing a query? Need an opinion on Oracle? Post here!

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-10-2008, 04:26 PM
chandrika's Avatar
WebProWorld Veteran
 

Join Date: Oct 2005
Location: Cambridge, UK
Posts: 378
chandrika RepRank 1
Default secute database login with php

When connecting to an SQL DB in a php script, is it secure enough just to put the DB username/password etc in a separate config.php file in the same folder and then put a php require for that in your script.

Or what are the usual ways to make sure that the info in a config.php file, with UN/PW etc, are secure?
__________________
Hairstyles - Pictures of 2008 hairstyles and a virtual hairstyler demo.
Price Comparison Site - Compare prices of well known brands and products.
Reply With Quote
  #2 (permalink)  
Old 06-10-2008, 04:49 PM
kgun's Avatar
WebProWorld 1,000+ Club
 

Join Date: May 2005
Location: Norway
Posts: 5,150
kgun RepRank 3kgun RepRank 3
Default Re: secute database login with php

Security by obscurity is not secure at all. The best is to make
  1. A database connection class.
  2. An authentication class.
  3. A session class.
Introduction and litterature:
  1. Introduction Object Oriented Programming in PHP
  2. Book The PHP Anthology: 101 Essential Tips, Tricks & Hacks, 2nd Edition - SitePoint Books
Reply With Quote
  #3 (permalink)  
Old 06-10-2008, 06:29 PM
chandrika's Avatar
WebProWorld Veteran
 

Join Date: Oct 2005
Location: Cambridge, UK
Posts: 378
chandrika RepRank 1
Default Re: secute database login with php

That book looks good, I have been looking for a useful php ref book, thanks.

I will check out the first link tomorrow, see if I can hide the UN/PW that way.
__________________
Hairstyles - Pictures of 2008 hairstyles and a virtual hairstyler demo.
Price Comparison Site - Compare prices of well known brands and products.
Reply With Quote
  #4 (permalink)  
Old 06-11-2008, 05:19 PM
vwickam's Avatar
WebProWorld New Member
 

Join Date: Oct 2005
Location: Evansville, IN
Posts: 14
vwickam RepRank 0
Default Re: secute database login with php

Additionally, I would suggest if you have any username/pw in a config file, you should secure the file using unix file permissions.
__________________
Vi Wickam Principal Web Solutions - http://www.PrincipalWebSolutions.com - Web Application for Real Business
Reply With Quote
  #5 (permalink)  
Old 06-12-2008, 01:12 AM
edhan's Avatar
WebProWorld Veteran
 

Join Date: Aug 2003
Location: Singapore
Posts: 553
edhan RepRank 1
Default Re: secute database login with php

Normally config.php requires 666 permission to make changes. Thereafter, you can set it to 644. At the same time, you may want to rename the config.php to something else like 871asfeljs.php so that it will not be easily guess.

Hope this helps.
Reply With Quote
  #6 (permalink)  
Old 06-12-2008, 07:48 AM
vwickam's Avatar
WebProWorld New Member
 

Join Date: Oct 2005
Location: Evansville, IN
Posts: 14
vwickam RepRank 0
Default Re: secute database login with php

A file setting of 600 would give you read and write access to the file, and giving group and world no permissions at all. This is how I always set config files.

You set the file permissions using chmod if you have shell (telnet or ssh) access to the server. The syntax would be:

chmod 600 config.php

If you don't have shell access to your server, the is a good chance that you can change file permissions using the control panel on your webhost.

If your program can't read the file with that setting, than you may have a file ownership issue. There is a unix command to change ownership of files as well (chown), but you might be best to just place a trouble ticket with your webhost detailing the situation.

Thanks,
Vi
__________________
Vi Wickam Principal Web Solutions - http://www.PrincipalWebSolutions.com - Web Application for Real Business
Reply With Quote
  #7 (permalink)  
Old 06-12-2008, 08:30 AM
WebProWorld Veteran
 

Join Date: Aug 2003
Location: Cornwall, UK
Posts: 833
speed RepRank 1
Default Re: secute database login with php

Creating classes won't help at all in protecting the username/password as it still has to be placed in the PHP somewhere.

If you are worried about your settings being access from the web if PHP fails then just put config.php outside the web root.

Or try putting your database info in .htaccess e.g.
Code:
SetEnv mydbuser theuser
SetEnv mydbpass thepass
Then in the PHP access it with $_ENV['mydbuser'] and so on.

If you are worried about others on the server accessing your information then the above won't help, you'll need to do one of the following.

If your host runs PHP via suPHP or the like then as already said setting permissions to 600 works, but if using use mod_php you'll probably be blocking access as PHP normally runs as nobody or the apache user.

Or you can pass all the PHP through ionCube which gives you encrypted PHP files. Good luck trying to decipher that if you encode with everything on. Just make sure you keep an unencoded copy for future work. Note: You have to encode more than just the config file for this to work.
__________________
TOLRA Micro Systems Limited US & UK Web Hosting with Continuous Data Protection
Web Directory 2 for 1 Offer : Web Directory Script
Reply With Quote
  #8 (permalink)  
Old 06-12-2008, 11:32 AM
chandrika's Avatar
WebProWorld Veteran
 

Join Date: Oct 2005
Location: Cambridge, UK
Posts: 378
chandrika RepRank 1
Default Re: secute database login with php

I am not worried of anyone else on the server accessing DB info, just public and I wasnt sure how secure such referenced files in scripts were. As although when php is executed such things do not show in the source, I thought maybe someone could download the actual php file somehow and simply read it.

As you say, setting permissions to 600 on my server does indeed block access for my script that includes the file as well.

I have settled for putting it outside the web root.

The ioncube info is interesting, I have ioncube that came packaged with a script I bought once, never looked much into it.
__________________
Hairstyles - Pictures of 2008 hairstyles and a virtual hairstyler demo.
Price Comparison Site - Compare prices of well known brands and products.
Reply With Quote
  #9 (permalink)  
Old 06-12-2008, 12:03 PM
WebProWorld Veteran
 

Join Date: Aug 2003
Location: Cornwall, UK
Posts: 833
speed RepRank 1
Default Re: secute database login with php

Quote:
Originally Posted by chandrika View Post
I am not worried of anyone else on the server accessing DB info, just public and I wasnt sure how secure such referenced files in scripts were. As although when php is executed such things do not show in the source, I thought maybe someone could download the actual php file somehow and simply read it.
Only if something goes wrong with Apache or PHP is the source possibly available in the visitors browser.
Quote:
As you say, setting permissions to 600 on my server does indeed block access for my script that includes the file as well.
You're probably using mod_php so all your scripts run as the Apache user.
Quote:
The ioncube info is interesting, I have ioncube that came packaged with a script I bought once, never looked much into it.
There's two parts to ioncube, the encoder which encrypts the script and can optionally lock the script to a single server, and the runtime which is the bit you would have had delivered with the script.
__________________
TOLRA Micro Systems Limited US & UK Web Hosting with Continuous Data Protection
Web Directory 2 for 1 Offer : Web Directory Script
Reply With Quote
  #10 (permalink)  
Old 06-12-2008, 12:18 PM
wige's Avatar
Moderator
WebProWorld Moderator
 

Join Date: Jun 2006
Location: United States
Posts: 1,785
wige RepRank 4wige RepRank 4wige RepRank 4wige RepRank 4
Default Re: secute database login with php

Putting the file outside the web root is generally the best method, as it prevents the file from being downloaded by a user. If you don't have a folder outside the web root (you can only FTP to the web root) you can create an "offlimits" folder very easily. Simply create the folder in your root, named "lib" for example. Then, add the following lines to your .htaccess file:

RedirectMatch 404 /lib/.*

Any request for a file in the lib folder will get a 404 not found response, preventing download of the file. This also hides the folder very effectively by giving the user a correct Not Found message. If you use a Deny All method, the user will get an error 403 Forbidden message, which lets the user know the folder actually exists.

Changing of permissions is not actually intended to prevent someone on your server from reading the files. The purpose is to prevent what is known as a directory transversal attack. Imagine you have created a script that displays an image to the user, depending on some user selected variable. An attacker might be able to cause that script to serve the contents (unexecuted) to the browser. The workaround, if I recall correctly, is to turn off the write bit, but leave the file executable. Most shared servers execute scripts as either 'apache' or 'php' (depending on how Apache sends the file to the PHP interpreter), but the owner of the file is usually 'ftp' or your username, depending on how the FTP software is configured. This means that the file needs to be owner read and writeable, other executable and world executable. That way, you can upload new copies of the file, and Apache can execute the file but not read it. (I think this is correct. It has been a long time since I have dealt with this.)
__________________
The best way to learn anything, is to question everything.

Last edited by wige : 06-12-2008 at 12:29 PM.
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Tags: , , ,



Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why login to read in wpw? AjiNIMC WebProWorld: Guidelines/Announcements/Suggestions 12 01-12-2006 09:55 PM
login problem pbrollwitme Flash Discussion Forum 7 09-01-2005 05:46 PM
Login functionality anthonyl Web Programming Discussion Forum 4 08-31-2004 11:16 PM
simple login jmdb71 Web Programming Discussion Forum 3 08-09-2004 11:27 AM
JS Error on login AlanMCSD WebProWorld: Guidelines/Announcements/Suggestions 0 10-27-2003 02:23 AM


Search Engine Optimization by vBSEO 3.2.0