douzhuo1858 2013-02-08 10:00
浏览 36

创建config.php的正确方法

At the moment I am placing my config.php file in the include folder on my Apache Server, which gives me easy access to the file, with having to write a directory path. I assume this is a good idea since it is secure and not reachable from outside the public root folder.

I was never told how to create the config.php file so I improvised, but it seems to that it may have been done the wrong way.. (not that secure now). Then I began to search for tutorials on how to build these config files the right way, but each and every tutorial had their own way to do it.. Some did it by using arrays.. some did it by defining the configuration variables.. others by using a class.. There may not be a right way, but I want a secure and very functional system to work, and I don't really care how advanced it is going to be..

This is my current config file.. you should easily see that it is not that secure in any way, cause anybody might just be able to echo the variables and then read the connection.

<?
$main_host = 'db01.server.local'; // There may be db02, db03 etc.
$main_psw = '********';
$main_host_end = '.server.local'; // makes it possible for me to connect to a different datastore only knowing the subdomain.

// ***    USERS    *** //
$w_user = 'w_user';
$xr_user = 'xr_user';
$r_user = 'r_user';
$w_server = 'w_server';
$w_db_admin = 'dbw_admin';

// ***    DATABASES    *** //
$db_accounts = 'accounts';
$db_server = 'server_setup';

// ***    DB ACCOUNTS    *** //
try {
    $w_accounts = new PDO("mysql:host=$main_host;dbname=$db_accounts", $w_user, $main_psw);
    $w_accounts->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

try {
    $r_accounts = new PDO("mysql:host=$main_host;dbname=$db_accounts", $r_user, $main_psw);
    $r_accounts->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

// ***    DB SERVER SETUP    *** //
try {
    $w_server = new PDO("mysql:host=$main_host;dbname=$db_server", $w_user, $main_psw);
    $w_server->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

try {
    $r_server = new PDO("mysql:host=$main_host;dbname=$db_server", $r_user, $main_psw);
    $r_server->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}


?>

So.. to ask the question directly. What is the right way to create a config.php file? I really also hope that other people will find this useful to know.

EDIT I also should mension that this might not be all.. I may have further connections to make.. not only the accounts and server_setup

  • 写回答

2条回答 默认 最新

  • dongsiju1941 2013-02-08 10:17
    关注

    First, never ever echo exception messages in production, instead put them into a readable logfile.

    If You look at many MVC frameworks, their php config files only consist of an array, that is returned at the end of the file.

    Limiting access to these config files can be done via .htaccess with "Deny from all"

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line