Search code examples
phphtmlflat-file

How to read data from the URL in PHP?


Just as an example:

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

I'm not sure what that is even called, but I can't find anything on it.


Solution

  • Of course you can use GET parameters

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

    Which you can get in PHP with:

    $_GET['foo'];
    

    I don't really know why you want to pass the database name in the link. Sure, it can be a parameter, but it so unsecure!

    UPDATE: you may use more/multiple databases, but use then a config file. The connection names or database names do not passed in the 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
    }
    

    Better is to set demo state to session, so you can read out it without the url (more secure)