No autoloader, no brackets for new instance (PSR), use of globals, incorrect if-else order (should be !empty()), SQL injection, no declare_strict, no strict === comparison, nested if statements...
It is far from worst I have seen, but PHP4/5 is not acceptable in 2022.
It's a really weird mix between bad old terrible code, and nice new code. Doing manual includes, weird naming style, using superglobals instead of objects that wrap them, no namesspaces, echoing errors etc. But it's also using prepared statements, the correct password_verify function. It's like someone's modernised old-style procedural code.
Not a lot actually, but the best way to receive help on your code is to invite people to insult it. The only downside to this code is security, trying to scam people into telling me how to fix it.
Looks like regular php code. It's kind of strange to have a method named select that apparently just accepts any SQL statement. I would just name it "query".
If it was something like $SQL->select("* FROM [...]");, it could avoid an unaware person from doing things like $SQL->select("UPDATE 'table' SET field = ?", "value"); or actually changing select() to fetch(), but that's just my opinion.
I actually tried doing that but with the way I set up the class it was throwing me for a loop and I gave up, but that is definitely on my todo list of things to fix
yes, but without giving it a name. If he pretended more specifically what parts he deems awesome then the internet could give better targeted suggestions while ripping it apart for ego boost.
<?php
// Begin Login.php backend code.
App\General::class_include('class.User.php');
App\General::class_include('class.SQL.php');
// Initialize variables required for registration.
$user = new App\User;
$SQL = new App\SQL;
// Begin processing form data.
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (isset($_POST['submit']))
{
$username = isset($_POST['username']) ? $_POST['username'] : '';
$username = isset($_POST['password']) ? $_POST['password'] : '';
// Assign variables to the user class and do minor sanitization.
$user->setUsername($username);
$user->setPassword($password);
// Check for errors logging in.
$user->setError($user->validateLogin());
// Output any errors.
$user->print_error();
// If there are no errors, proceed with logging in the user.
if (empty($user->getError()))
{
$hashed_password = $SQL->select("SELECT password FROM users WHERE username = ?", $user->getUsername());
if (password_verify($password, $hashed_password))
{
// The user has been verified, initialize their session and assign session variables.
session_start();
$_SESSION['logged_in'] = true;
$_SESSION['id'] = $SQL->select("SELECT id FROM users WHERE username = ?", $user->getUsername());
$_SESSION['username'] = $user->getUsername();
}
else
{
echo "Invalid username or password.";
}
}
}
}
?>
I only see issues with commentary. It's supposed to tell you why something is there, not what it does specifically. You're code is explanation enough (like the comments aboce the error logging, user variables and session_start)
There’s actually a pretty big problem here with the way you consider people logged in. Doing this means that anybody setting session variables on their own browser to what is relatively easy to access information will log them in.
If there is a DB leak or part of your code exposes primary keys at some point, then all I need to do is locally set my session variables for id and username to values I discovered. Rolling salts or passwords will do nothing in that respect.
What you should probably do is return a session hash with high entropy on successful login and set that to the session instead. Then keep a table (or store it on the user table) and validate against that.
Why Python + Django? It's way slower than PHP and Java. Also you could produce bad Code in every language. Modern PHP is fine, maybe it's not standardized as Java or JavaScript, but it is fine
Personally I like Java for backend because it's so strictly defined, but Python is easy to use. My gripe with Python is that IDE's are less good at working with it. Speed hasn't been an issue at the companies I've worked in.
30
u/MisterTheMonkeey Apr 23 '22
I don't get it, what's bad with that code ?
5
u/UAIMasters Apr 23 '22
Looks like regular php code. It's kind of strange to have a method named select that apparently just accepts any SQL statement. I would just name it "query".
3
u/mosskin-woast Apr 23 '22
Find me some good PHP and I'll point out the differences
2
u/dumb-ninja Apr 23 '22
You could save an SQL query by getting the password and id at the same time and using the id if the password check succeeds :)
29
u/[deleted] Apr 23 '22
op is doing this https://meta.wikimedia.org/wiki/Cunningham%27s_Law for code reviewing:
Cunningham's Law states "the best way to get the right answer on the internet is not to ask a question; it's to post the wrong answer."
51
u/carlson_001 Apr 23 '22
It could be improved, but it's not bad.
8
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 23 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
5
u/deep_clean_am Apr 23 '22
So what.
1
u/donut-boy03 Apr 23 '22
theme??
3
u/pascalxsome Apr 23 '22
I only see issues with commentary. It's supposed to tell you why something is there, not what it does specifically. You're code is explanation enough (like the comments aboce the error logging, user variables and session_start)
1
u/ebzlo Apr 23 '22
There’s actually a pretty big problem here with the way you consider people logged in. Doing this means that anybody setting session variables on their own browser to what is relatively easy to access information will log them in.
If there is a DB leak or part of your code exposes primary keys at some point, then all I need to do is locally set my session variables for id and username to values I discovered. Rolling salts or passwords will do nothing in that respect.
What you should probably do is return a session hash with high entropy on successful login and set that to the session instead. Then keep a table (or store it on the user table) and validate against that.
1
u/sony_anumo Apr 24 '22
Its not great but its not that bad.
Solid starter code
1
u/Bowiemtl Sep 29 '22
you're gonna love Laravel
0
u/[deleted] Apr 23 '22
I couldn't tell if it is good or bad tbh cos it's PHP, which in my mind is just bad.
-1
u/ThePeopleAtTheZoo Apr 24 '22
All PHP is bad.
0
u/[deleted] Apr 23 '22
[deleted] — view deleted comment