duan246558 2011-01-19 22:40
浏览 13
已采纳

快速函数告诉数据库初始化程序使用本地U / P或webhost用户名/传递?

I came across a similar question once on SO but didn't mark it and can't find it now.

Currently my init.php file looks like this:

$db_username = user;
$db_password = pass;
//$db_username = user213124; // webhost
//$db_password = pass214142; // webhost

and I alternately comment/uncomment those lines depending on if its running on my test machine's XAMPP installation or on my webhost.

Sometimes I forget, and upload the wrong one, which is always good for a laugh.

I'm sure there's a way to write a short function allowing init.php to detect its location and use the appropriate username/pass combo.

The first solution that popped into my head was a simple if(file_exists()) {} and checking for something that would ONLY be on my test machine. I'm guessing there's a better way that doesn't prompt disk access though, amirite?

Thank you.

PS - please edit my subject to make it more accurate. Tried my best. Kinda hard to describe...

  • 写回答

2条回答 默认 最新

  • douhaoqiao9304 2011-01-19 22:47
    关注

    There are several ways of accomplishing this, some may be better than others, I think it all depends on your situation.

    One way is to check for something in $_SERVER. For example, you might user the server name:

    if ($_SERVER['SERVER_NAME'] == 'my.dev.server') {
        $user = 'dev_user';
        $pass = 'dev_pass';
    } else {
        $user = 'other_user';
        $user = 'other_pass';
    }
    

    You might also consider having a local configuration file, unique to whatever environment you're on. This could be an ini file or php file that just defines the above variables, and is included, but isn't under version control and doesn't get changed very often.

    Sometimes people will also define environment variables. There are probably other ways as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂