如何从PHP中的URL中读取数据?

How to read data from the URL in PHP?

举个例子:

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

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

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

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

然后在PHP代码:

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

当然可以使用GET参数

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

您可以在 PHP 中获得:

$_GET['foo'];

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

更新:您可以使用 more/multiple 数据库,但随后使用配置文件。 url.

中未传递连接名称或数据库名称

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

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(更安全)的情况下读取它