Web Design Forum: db username and password in php - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

db username and password in php should i keep out of root? Rate Topic: -----

#1 User is offline   artisites 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 40
  • Joined: 30-October 08
  • Reputation: 0
  • Experience:Nothing
  • Area of Expertise:Designer

Posted 10 January 2012 - 12:03 AM

I currently have a simple php script to read in blog posts from a mysql database.

At the top there is

var $host = "localhost";
var $user = "";
var $pass = "";
var $db = "";


then later in the script there are lots of bits like this

function getCategories(){
		$sql = "SELECT id, name, DATE_FORMAT(date, '".$this->date_format."') as newdate FROM ".$this->table_pre."categories AS categories ORDER BY id";
		$items = mysql_query($sql) or die("Database Error: ".mysql_error());
		
		while ($obj = mysql_fetch_object($items)) {
        	$array[$obj->id]['id'] = $obj->id;
            $array[$obj->id]['name'] = $obj->name;
			$array[$obj->id]['newdate'] = $obj->newdate;
        }
		
		mysql_free_result($items);
		
		return $array;


Are there any simple steps i can take to make this a bit safer? Some scripts I've used before use pear library with a db-connect include, I'm just thinking it may be wise to go down this route, but I think it would be quite a task to redo the whole script.

Is it safer if I extract the db name and pw to an include outside of the public html root?
0

#2 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 10 January 2012 - 12:06 AM

View Postartisites, on 10 January 2012 - 12:03 AM, said:

I currently have a simple php script to read in blog posts from a mysql database.

At the top there is

var $host = "localhost";
var $user = "";
var $pass = "";
var $db = "";


then later in the script there are lots of bits like this

function getCategories(){
		$sql = "SELECT id, name, DATE_FORMAT(date, '".$this->date_format."') as newdate FROM ".$this->table_pre."categories AS categories ORDER BY id";
		$items = mysql_query($sql) or die("Database Error: ".mysql_error());
		
		while ($obj = mysql_fetch_object($items)) {
        	$array[$obj->id]['id'] = $obj->id;
            $array[$obj->id]['name'] = $obj->name;
			$array[$obj->id]['newdate'] = $obj->newdate;
        }
		
		mysql_free_result($items);
		
		return $array;


Are there any simple steps i can take to make this a bit safer? Some scripts I've used before use pear library with a db-connect include, I'm just thinking it may be wise to go down this route, but I think it would be quite a task to redo the whole script.

Is it safer if I extract the db name and pw to an include outside of the public html root?


mm u dont need to use var infront of your variables, you can just do

$host = "localhost";
$user = "";
$pass = "";
$db = "";

0

#3 User is offline   artisites 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 40
  • Joined: 30-October 08
  • Reputation: 0
  • Experience:Nothing
  • Area of Expertise:Designer

Posted 11 January 2012 - 10:00 PM

ok cool, but assuming the rest of the file is secure is it possible for someone to get at those details in the php file?

should i home them outwith the public html folder?
0

#4 User is offline   Olavi 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 10
  • Joined: 11-January 12
  • Reputation: 2
  • Gender:Male
  • Location:Norway
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 11 January 2012 - 10:14 PM

View Postartisites, on 11 January 2012 - 10:00 PM, said:

should i home them outwith the public html folder?


Store secure data outside public folder and include it where you need it.
0

#5 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 11 January 2012 - 10:32 PM

View Postartisites, on 11 January 2012 - 10:00 PM, said:

ok cool, but assuming the rest of the file is secure is it possible for someone to get at those details in the php file?

should i home them outwith the public html folder?

theres a little trick u can do to prevent direct access to these files, like this


<?php
//Deny direct access to this file
if(!defined('ALLOW_ACCESS')){die('No direct access allowed');}
//Db details
$host = "localhost";
$user = "";
$pass = "";
$db = "";
?>


then with in any file u include those details in u put this line above the file include
define('ALLOW_ACCESS',true);


you also can deny direct access to folders by placing a blank index.html file in each folder

This post has been edited by webdesigner93: 11 January 2012 - 10:34 PM

0

#6 User is offline   Samus 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 367
  • Joined: 05-August 11
  • Reputation: 27
  • Gender:Male
  • Location:Hackney, London, UK
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 12 January 2012 - 12:59 AM

They wouldn't even be able to see the connection details anyway if you're just assigning them to a variable. They'd have to be echo'ed first.
0

#7 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 12 January 2012 - 01:25 AM

View PostSamus, on 12 January 2012 - 12:59 AM, said:

They wouldn't even be able to see the connection details anyway if you're just assigning them to a variable. They'd have to be echo'ed first.

u would be supprised what hackers can do
0

#8 User is offline   gadgetgirl 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 152
  • Joined: 26-February 10
  • Reputation: 6

Posted 12 January 2012 - 09:48 AM

View Postwebdesigner93, on 11 January 2012 - 10:32 PM, said:

theres a little trick u can do to prevent direct access to these files, like this


<?php
//Deny direct access to this file
if(!defined('ALLOW_ACCESS')){die('No direct access allowed');}
//Db details
$host = "localhost";
$user = "";
$pass = "";
$db = "";
?>


then with in any file u include those details in u put this line above the file include
define('ALLOW_ACCESS',true);


you also can deny direct access to folders by placing a blank index.html file in each folder

this looks like a good security trick to try - thanks for posting
0

#9 User is online   rallport 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 3,818
  • Joined: 03-January 10
  • Reputation: 266
  • Gender:Male
  • Location:England, UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 12 January 2012 - 01:44 PM

View Postwebdesigner93, on 11 January 2012 - 10:32 PM, said:

theres a little trick u can do to prevent direct access to these files, like this


<?php
//Deny direct access to this file
if(!defined('ALLOW_ACCESS')){die('No direct access allowed');}
//Db details
$host = "localhost";
$user = "";
$pass = "";
$db = "";
?>


then with in any file u include those details in u put this line above the file include
define('ALLOW_ACCESS',true);


you also can deny direct access to folders by placing a blank index.html file in each folder


Don't see the point in that tbh. It takes seconds to place a file outside the web root and reference that location instead.
0

#10 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 12 January 2012 - 09:20 PM

View Postrallport, on 12 January 2012 - 01:44 PM, said:

Don't see the point in that tbh. It takes seconds to place a file outside the web root and reference that location instead.

True, its just a preference of mine i guess :)

This post has been edited by webdesigner93: 12 January 2012 - 09:21 PM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users