Note that there are some explanatory texts on larger screens.

plurals
  1. POSwitching databases in PHP
    primarykey
    data
    text
    <p>I'm new to PHP, so this question might best be answered by a brief explanation of fundamentals of PHP rather than addressing what I'm trying to do (although that would be useful too).</p> <p>I want to set up multiple databases for my application, based on user. When the user logs in, it would authenticate them against DB-1, and retrieve from there which database is to be used for everything else.</p> <p>I define my login.php file as follows:</p> <pre><code>&lt;?php $db_hostname = 'localhost'; $db_database = 'testing'; $db_username = 'someuser'; $db_password = 'somepass'; ?&gt; </code></pre> <p>and then I have another PHP file defined as follows:</p> <pre><code>&lt;%php require_once 'login.php'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) die ("Unable to connect to MySQL: " . mysql_error()); mysql_select_db($db_database) or die ("Unable to select database: " . mysql_error()); function check_login($user, $password) { $user = mysql_entities_fix_string($user); $password = mysql_entities_fix_string($password); $password = secure_password($password); $query = "SELECT DB_NAME, DB_USER FROM USERS WHERE USER_NAME='$user' AND PASSWORD='$password'"; $result = mysql_query($query); if (!$result) 0; //no access to the database elseif (mysql_num_rows($result)) { $row = mysql_fetch_row($result); $db_database = $row[0]; $db_username = $row[1]; $db_password = $password; return 1; //login succeeded } else { return 2; //login failure } } ?&gt; </code></pre> <p>What I am wondering is about the end of the function check_login(). Assume that the USERS table would return the name of the database (e.g. 'db123522') and the username for that database (e.g. 'jsmith'), and the password would be the same as their salted and hashed password. That is, each user would have their own database, with a generated user name and a password matching their salted+hashed password.</p> <p>In this case, if my second PHP file had another function to access the database, how would I go about ensuring that it would use the new database definition, and not the database definition from login.php? </p> <p>What is retained in memory between one call to the functions in this PHP file and the next call, and can this differentiate between users? If I have to put some of this information into the session so that I can load the appropriate database on the next call, what would be the minimal amount of information to let me do this, without compromising the security of the application (that is, I obviously don't want to put a password into the session)?</p> <p>Is there a better way to do this (I'm sure there is, but can someone explain it to me or point me in the right direction)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload