patternphpMinor
Login and registration pages for a PHP and PDO e-commerce project
Viewed 0 times
registrationphploginprojectforcommerceandpagespdo
Problem
We are doing an e-commerce project in a PHP-MySQL combination. Here is the code with PDO we wrote for login and register. I've heard that writing less code is better, so how we can reduce the lines of code?
connection.php
login
register
Library file to handle request of index.php file
```
Login($username, $password); // check user login
if($user_id > 0)
{
$_SESSION['user_id'] = $user_id; // Set Session
header("Location: profile.php"); // Redirect user to the profile.php
}
else
{
$login_error_message = 'Invalid login details!';
}
}
}
// check Register request
if (!empty($_POST['btnRegister'])) {
if ($_POST['name'] == "") {
$register_error_message = 'Name field is required!';
} else if ($_POST['email'] == "") {
$register_error_message = 'Email field is required!';
} else if ($_POST['username'] == "") {
$register_error_message = 'Username field is required!';
} else if ($_PO
connection.php
getMessage();
die();
}
}
?>login
Login
Error: ' . $login_error_message . '';
}
?>
Username/Email
Password
register
Register
Error: ' . $register_error_message . '';
}
?>
Name
Email
Username
Password
Library file to handle request of index.php file
```
Login($username, $password); // check user login
if($user_id > 0)
{
$_SESSION['user_id'] = $user_id; // Set Session
header("Location: profile.php"); // Redirect user to the profile.php
}
else
{
$login_error_message = 'Invalid login details!';
}
}
}
// check Register request
if (!empty($_POST['btnRegister'])) {
if ($_POST['name'] == "") {
$register_error_message = 'Name field is required!';
} else if ($_POST['email'] == "") {
$register_error_message = 'Email field is required!';
} else if ($_POST['username'] == "") {
$register_error_message = 'Username field is required!';
} else if ($_PO
Solution
if you really want reduce amount of lines -- delete your comments :)
I can understand that this is start session even without comment, and so on. You should comment some unclear part, some dirty hacks, etc, not every line.
Write comments for every line is anti-pattern, you shouldn't do this. Also less code doesn't mean "better code". I think deleting unnecessary comments is only one case, when this works.
And of course you can minimize your html to one line.
// start session
session_start();I can understand that this is start session even without comment, and so on. You should comment some unclear part, some dirty hacks, etc, not every line.
Write comments for every line is anti-pattern, you shouldn't do this. Also less code doesn't mean "better code". I think deleting unnecessary comments is only one case, when this works.
And of course you can minimize your html to one line.
Context
StackExchange Code Review Q#143177, answer score: 3
Revisions (0)
No revisions yet.