简体   繁体   中英

How can i make a proper database login directly from the user created on Cpanel?

i'm building an android application and i'm using phps scripts to do all my requests, i'm kind of new with this so i hope you all can help me. Normally if you are going to connect to a db using php you do the connection.php file and the login.php using the connection.php.

For Security purposes i´m not using a UsersTable in my DB, i'm connecting directly with the user you create on your Cpanel to manage the DB, so my php connection its something like this.

    <?php
$user = $_POST["Us"];
$pass= $_POST["Pss"];
$server='SERVER;$db='DB';
$connection = mysqli_connect($server,$user, $pass, $db);
if (!$connection) 
{die ("Error de conexion a la base de datos ... \n" . mysql_error ());}
else {echo 'Ok';}     
?>

but i need to make a request if the connection.php answer is OK then on my DataOb.php and other files do this for example to obtain some data?

<?php
require('Conexion.php');
$username = $_GET["Us"];
$statement=mysqli_prepare($connection,"SELECT idTrabajador,`Nombre del Trabajador` FROM `tablausuario` WHERE `Usuario`=?");
mysqli_stmt_bind_param($statement,"s",$username);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement,$idTrabajador,$name);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["idTrabajador"] = $idTrabajador;
$response["Nombre del Trabajador"] = $name;
}
echo json_encode($response);
?>

the thing is , how can i create the "if" condition, obtain the user? and keep the connection open to do the requests on the other files?, its better to use session_start(), so i can close the connection after? or i'm i3n the right path?

Instead of echoing "ok" why don't you set a variable.

 $connected = true;

And then elsewhere in the code just check the variable:

if($connected) {
    //execute your code here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM