V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Sparty
V2EX  ›  PHP

php 网站通过前端 form 更改数据库连接,如何实现?

  •  
  •   Sparty · 2016-04-15 10:01:08 +08:00 · 2592 次点击
    这是一个创建于 2934 天前的主题,其中的信息可能已经有所发展或是发生改变。

    网站的数据库连接信息原本存在一个静态的 config.php 文件里,现在需要一个页面让用户输入 dbname , dbuser , dbpass , dbhost 等信息(页面需要 admin 权限),实现数据库的随意转换。 现在能做到获取用户输入的信息并且测试链接,但是不知道如何保存这四个变量长期使用。唯一想到的方法是使用 php 在服务器上再创一个静态的文件(dbinfo.php)保存这些信息。但是目前遇到一些权限问题导致无法使用服务器上面的 php 文件写 php 文件。想问问大家还有没有什么别的更好的方法?

    9 条回复    2016-04-16 03:51:23 +08:00
    chenwl
        1
    chenwl  
       2016-04-15 10:04:19 +08:00
    建议直接黑了吧~!
    UnisandK
        2
    UnisandK  
       2016-04-15 10:04:36 +08:00
    把他输入的数据库信息存你数据库里
    chend
        3
    chend  
       2016-04-15 10:05:03 +08:00
    好奇怪的需求。。。

    每次都需要用户输入, 为什么不用 session cookie~~
    只需要一次, 可以存入数据库。。。。

    最后, i2ex 水得比较多, 这种问答类 还是到 问答 社区 比较好~~ 比如 国内的 ruby china, segment
    arnofeng
        4
    arnofeng  
       2016-04-15 10:05:25 +08:00 via iPad
    数据库另外一个表存这四个变量呗。不停维护就行。
    lissome
        5
    lissome  
       2016-04-15 10:54:20 +08:00
    存数据库,或者存其他格式的配置文件啊,比如 .ini ,.conf ……
    fate
        6
    fate  
       2016-04-15 11:26:35 +08:00
    加上一个数据库连接表,和用户关联,每次执行查询都先使用相关用户存到的数据库连接表的数据连接.
    domty
        7
    domty  
       2016-04-15 13:13:19 +08:00
    我不是写 php 的
    但是以前大学学 php 的时候有用过一个叫 phpMyAdmin 的工具,和你描述的需求好相似
    flowfire
        8
    flowfire  
       2016-04-15 13:22:16 +08:00
    @domty phpmyadmin 可是个大工程。。。
    Sparty
        9
    Sparty  
    OP
       2016-04-16 03:51:23 +08:00
    感谢大家的回复,已经跟 boss 讨论并且决定更改这个需求了,之前找到一段话我觉得是这个问题的标准答案,这里贴出来跟大家分享:

    This is a bad idea. You should not be modifying configuration file like this. If you you have configuration that can be administered by the user, you should be storing it in a database or, worst case scenario, in a generic file format (ini, XML etc). A configuration file that stores the database connection details should never be modified by an application, only manually by the administrator - because it is important that the file is secure and it is an event that should be very rare.Calling include/require on a file that is dynamically modified is asking for trouble.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1006 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:10 · PVG 03:10 · LAX 12:10 · JFK 15:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.