I’m having problems with my login system

Asked

Viewed 49 times

-1

<?php
include('conexao.php');

if(empty($_POST['usuario']) || empty($_POST['senha'])) {
    header('Location: index.html');
    exit();
}


$usuario = mysqli_real_escape_string($conexao, $_POST['usuario']);
$senha = mysqli_real_escape_string($conexao, $_POST['senha']);

$query = 'select usuario_id, usuario from usuario where usuario = '{$usuario}' and senha = md5('{$senha}')';

$result =  mysqli_query($conexao, $query);

$row = mysql_num_rows($result);

if($row == 1) {
    $_SESSION['usuario'] = $usuario;
    header('Location: home.html');
    exit();
} else {
    header('Location: index.html');
    exit();
}

It returns the following error:

Parse error: syntax error, Unexpected '' and password = md5('' (T_CONSTANT_ENCAPSED_STRING) in C: xampp htdocs login.php on line 13

Please help me I have no idea what it might be ...

  • When using variables inside a string use double-quoted strings. Anyway, on the line your error replaces the first and last ' with ".

  • But to make a query to the database, even more in an area as sensitive as login, please use at least mysqly:().

  • Nor should md5 be used to "encrypt" passwords

2 answers

1

I recommend using Preparestatement for security reasons and to avoid major risks of Sqlinjection, I simulated a session by taking the user’s access profile, and the user’s id, because that’s all you would need, in theory, in a session:

if (!$_POST || empty($_POST['usuario']) || empty($_POST['senha'])) {
    header('Location: index.html');
    exit();
}

$usuario = mysqli_real_escape_string($_POST['usuario']);
$senha = MD5($_POST['senha']);

$query = "SELECT id, role FROM usuario WHERE usuario = ? AND senha = ?";

 if (!($stmt = mysqli_prepare($conexao, $query) && $usuario && $senha)) {
    header('Location: index.html');
    exit();
 } else {
    $setParams = mysqli_stmt_bind_param($stmt, "ss", $usuario, $senha);
    $execute = mysqli_stmt_execute($stmt);
    $results = mysqli_stmt_get_result($stmt);
    $array_result = mysqli_fetch_assoc($results);

    //print_r($array_result);

   if (!$results) {
        header('Location: index.html');
        exit();
    }

    if (!isset($array_result['id'])) {
        header('Location: index.html');
        exit();
    }

    session_start();
    $_SESSION['usuario'] = $usuario;
    $_SESSION['id'] = $array_result['id'];
    $_SESSION['nivel_acesso'] = $array_result['role'];
    header('Location: home.html');
    exit();
}

-1

Hello @Jeanextreme002, all right? I updated the $_POST assigned in the variable "password" and updated your query. Change your code to do so:

<?php
include('conexao.php');

if(empty($_POST['usuario']) || empty($_POST['senha'])) {
    header('Location: index.html');
    exit();
}


$usuario = mysqli_real_escape_string($conexao, $_POST['usuario']);
$senha = mysqli_real_escape_string($conexao, md5($_POST['senha']));

$query = "SELECT * FROM usuario WHERE usuario = '$usuario' AND senha = '$senha'";

$result =  mysqli_query($conexao, $query);

$row = mysql_num_rows($result);

if($row == 1) {
    $_SESSION['usuario'] = $usuario;
    header('Location: home.html');
    exit();
} else {
    header('Location: index.html');
    exit();
}

I hope I helped. Regards, Minds'.

  • So @Minds, the way you did, if I pass the parameter ' or 1 = 1 or 1 = ' in the field, I enter the system. Because the query would be: SELECT * FROM usuario WHERE usuario = '' or 1 = 1 or 1 = '' AND senha = 'qualquercoisa'

  • 1

    The author’s name is Pedro Henrrique. Jean was the editor improving the text.

  • @Ivanferrer But he’s using the mysqli_real_escape_string() function for the user and password fields.

  • 1

    I know @Andre, but mysqli_real_escape_string, only escapes quotes, does not prevent data entry, it is risky to think. for example it does not protect you from: 5 OR 1=1

  • @Ivanferrer I understood Ivan... I asked just out of curiosity... Obrgiado pelos esclarecimentos!

  • Welcome to Sopt @Minds, see above the comment by Augusto Vasques.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.