V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Sparty
V2EX  ›  PHP

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

  •  
  •   Sparty · Apr 15, 2016 · 3116 views
    This topic created in 3666 days ago, the information mentioned may be changed or developed.

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

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

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

    最后, i2ex 水得比较多, 这种问答类 还是到 问答 社区 比较好~~ 比如 国内的 ruby china, segment
    arnofeng
        4
    arnofeng  
       Apr 15, 2016 via iPad
    数据库另外一个表存这四个变量呗。不停维护就行。
    lissome
        5
    lissome  
       Apr 15, 2016
    存数据库,或者存其他格式的配置文件啊,比如 .ini ,.conf ……
    fate
        6
    fate  
       Apr 15, 2016
    加上一个数据库连接表,和用户关联,每次执行查询都先使用相关用户存到的数据库连接表的数据连接.
    domty
        7
    domty  
       Apr 15, 2016
    我不是写 php 的
    但是以前大学学 php 的时候有用过一个叫 phpMyAdmin 的工具,和你描述的需求好相似
    flowfire
        8
    flowfire  
       Apr 15, 2016
    @domty phpmyadmin 可是个大工程。。。
    Sparty
        9
    Sparty  
    OP
       Apr 16, 2016
    感谢大家的回复,已经跟 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.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1052 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 22:42 · PVG 06:42 · LAX 15:42 · JFK 18:42
    ♥ Do have faith in what you're doing.