Error in php if, login authentication system

1

Hello, so I'm trying to make a simple login system with php and mysql, but it's giving error in my if. I already moved ... too much and nothing, when it seems that right it does not authenticate you put any password q or login name and will. This is my code here:

<?php 
$login = $_POST['nomel'];
 $senha = $_POST['senha'];


 include 'conexao.php';

 $busca = mysql_query("SELECT * from usuario where nm_login = '$login'");

 $reg = mysql_fetch_assoc($busca);

 if 
    ($login == $reg ['nm_login']) && ($senha == $reg ['nm_senha']) {
    # code...
    header("location: test.html");
 }
 else{
    echo "<center><b>Login Incorreto!</center>";
 }
 ?>

And this is the error message:

Parse error: syntax error, unexpected '& &' (T_BOOLEAN_AND) in E: \ IMPORTANT \ USBWebserver v8.6 \ root \ NK \ autologinlogin.php on line 13 *

    
asked by anonymous 09.11.2017 / 00:01

1 answer

3

There was a missing parenthesis in the if you have in the code, this:

if ($login == $reg ['nm_login']) && ($senha == $reg ['nm_senha']) {

It has a parenthesis for each comparison of if but does not have one that encompasses the entire comparison.

See a simplified example of Ideone error

You can then do this:

if (($login == $reg ['nm_login']) && ($senha == $reg ['nm_senha'])) {
//-^--------------------------------------------------------------^

Or simplify and use just one parenthesis for if all:

if ($login == $reg ['nm_login'] && $senha == $reg ['nm_senha']) {

I strongly advise upgrading the database functions to mysqli since the mysql versions have been discontinued and no longer exist since version 7 of php.

You should also consider handling the received input in $login and $senha or use prepared statements , because as it has the code it is currently susceptible to mysql injection attacks >

    
09.11.2017 / 00:15