Jump to content

Hard coding DB username and pass in php


nameez

Recommended Posts

As long as your username and pass are stored in variable they can not be displayed while your server is processing php properly. Another option is to move them outside of your web root all together though.

 

Also can i connect to multiple DB's and work using data from them in PHP?

 

Yes, see mysql_connect.

Not really something you would find a tutorial on. Heres a simple explination.

 

[pre]

/home/username

  /home/username/htdocs <-- your web root.

  credentials.php

[/pre]

 

Now, if you have a file (index.php) in your web root you would use the following to include the credentials file.

 

include "../credentials.php";

 

Another option is to place your username and passwords in your .htaccess file. if the server is configured properly this shouldn't be readable via the web.

 

.htaccess

SetEnv DB_UNAME foo
SetEnv DB_UPASS bar

 

Then in php these variables are accessible using....

 

$db_uname = $_SERVER['DB_UNAME'];
$db_upass = $_SERVER['DB_UPASS'];

Not really something you would find a tutorial on. Heres a simple explination.

 

[pre]

/home/username

  /home/username/htdocs <-- your web root.

  credentials.php

[/pre]

 

Now, if you have a file (index.php) in your web root you would use the following to include the credentials file.

 

include "../credentials.php";

 

Another option is to place your username and passwords in your .htaccess file. if the server is configured properly this shouldn't be readable via the web.

 

.htaccess

SetEnv DB_UNAME foo
SetEnv DB_UPASS bar

 

Then in php these variables are accessible using....

 

$db_uname = $_SERVER['DB_UNAME'];
$db_upass = $_SERVER['DB_UPASS'];

 

Thanks ill try that  :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.