Click here to Skip to main content
15,919,178 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
0){

$_SESSION['id']=$row['user_id'];

echo 'true';

mysqli_query($conn,"insert into user_log (username,login_date,user_id)values('$username',NOW(),".$row['user_id'].")")or die(mysqli_error());
}else{
echo 'false';
}

?>

What I have tried:

0){

$_SESSION['id']=$row['user_id'];

echo 'true';

mysqli_query($conn,"insert into user_log (username,login_date,user_id)values('$username',NOW(),".$row['user_id'].")")or die(mysqli_error());
}else{
echo 'false';
}

?>
Posted
Updated 22-Sep-22 21:47pm
Comments
Richard Deeming 23-Sep-22 3:47am    
Two identical copies of an unformatted, unexplained code block, with no details of the "problem" you're having, does not make for a good question.

Click the green "Improve question" link and update your question to include a clear and complete description of the problem you're trying to solve, what you have tried, and where you are stuck. Include the full details of any error messages.

1 solution

None of that code involves passwords: it just saves a "successful" login with a timestamp.

But it even does that badly. Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

If your login code is similar, then that could well be causing the problem you are seeing. But even if it isn't you need to fix this throughout your app as a matter of urgency.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900