【问题标题】:How to read data from the URL in PHP? [closed]如何从 PHP 中的 URL 读取数据? [关闭]
【发布时间】:2015-11-10 10:42:20
【问题描述】:

举个例子:

http://domain.com/main.php(then the database file here).

我什至不知道那叫什么,但我在上面找不到任何东西。

【问题讨论】:

  • 我不知道我是否正确,但您不要想将您的数据库名称放在链接中...

标签: php html flat-file


【解决方案1】:

出于安全原因,不建议将您的数据库名称放在查询字符串中的 GET 请求中。但是,如果您仍然需要它,您可以这样做:

http://domain.com/main.php?database_name=MyDatabase

然后是 PHP 代码:

<?php
    $databaseName = $_GET['database_name'];
?>

【讨论】:

    【解决方案2】:

    当然可以使用GET参数

    http://domain.com/main.php?foo=bar

    您可以通过以下方式在 PHP 中获得:

    $_GET['foo'];
    

    我真的不知道你为什么要在链接中传递数据库名称。当然,它可以是一个参数,但它太不安全了!

    更新:您可以使用更多/多个数据库,但使用配置文件。连接名称或数据库名称未在 url 中传递。

    .../main.php?state=demo&type=1, .../main.php?state=demo&type=2等

    if ($_GET['state'] == 'demo')
    {
        switch ($_GET['type'])
        {
           case 1:
              $databaseName = 'demo_type_1';
           break;
           case 2:
              $databaseName = 'demo_type_2';
           break;
           default:
               throw new Exception('Wrong demo specified');
    
        }
    
        // connect to database with the name in $databaseName
    }
    else
    {
       // Other connection
    }
    

    更好的是将演示状态设置为会话,这样你就可以在没有url的情况下读出它(更安全)

    【讨论】:

    • 谢谢,原因是我需要做一个demo,需要展示不同的数据库状态...非常感谢。
    • 所以如果我有 4 个不同的演示,那将是 4 个不同的变量是 $databasename1 $databasename2 等?
    • 查看我的更新答案...
    • 哇,谢谢你.. o 欠你的......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    相关资源
    最近更新 更多