I am creating a web platform, which can be accessed by different workers' organizations.
When a worker wants to enter the platform, he must select which organization he belongs to, then put his mail and password, as shown in the following entry form:
The field "Organization:" sends by POST
the name of the database to my file validar.php, which connects to that database to then start the session and the session variables.
The following code does what has just been described, but to guide you in the process, just look at the last session variable I am starting:
validar.php
<?php
try {
$ndb = $_POST['dbname'];
$params = 'mysql:host=localhost;dbname='.$ndb.';charset=utf8';
$db = new PDO($params, 'root', '');
} catch (Exception $e) {
die('Error: ' . $e->getMessage());
}
if (!isset($_SESSION)) {
session_start();
}
$email= $_POST['email'];
$contrasena= $_POST['contrasena'];
$select = $db->prepare("SELECT * FROM 'empleados' WHERE email = :email AND clave = :contrasena");
$select->bindValue(':contrasena',$contrasena,PDO::PARAM_STR); //PDO::PARAM_INT para enteros
$select->bindValue(':email',$email,PDO::PARAM_STR);
$select->execute();
if($select->rowCount()>0){
$fila = $select->fetch(PDO::FETCH_ASSOC);
$_SESSION['rut'] = htmlentities($fila['rut']);
$_SESSION['nombre1'] = htmlentities($fila['nombre1']);
$_SESSION['nombre2'] = htmlentities($fila['nombre2']);
$_SESSION['nombre3'] = htmlentities($fila['nombre3']);
$_SESSION['apellido1'] = htmlentities($fila['apellido1']);
$_SESSION['apellido2'] = htmlentities($fila['apellido2']);
$_SESSION['edad'] = htmlentities($fila['edad']);
$_SESSION['email'] = htmlentities($fila['email']);
$_SESSION['rango'] = htmlentities($fila['rango']);
$_SESSION['clave'] = htmlentities($fila['clave']);
$_SESSION['database'] = $ndb;
header("Location: principal.php");
}
else{
echo '<script language = javascript>
alert("email y/o Password incorrecta.");
self.location = "index.html";
</script>';
}
?>
$_SESSION['database']
contains the name of the database, so for all my .php files that contain sql queries, I am using the following file to connect to the correct database:
conex.php
<?php
try {
$ndb = $_SESSION['database'];
$params = 'mysql:host=localhost;dbname='.$ndb.';charset=utf8';
$db = new PDO($params, 'root', '');
} catch (PDOException $e) {
die('Error: ' . $e->getMessage());
}
?>
In all those files that contain sql queries, I start / verify session and then I include this file "conex.php".
This works perfectly, without errors, but my question is:
- Is this an appropriate / correct / efficient way to create this type of multi-user "multi-organization" platforms? Why?
- Is it insecure? Why?