Is there a better way to do this?
Can I start a session inside a class?
and check if isset in the class??
class users {
function login($username, $password) {
$stmt = $db->prepare("SELECT * FROM users WHERE username=? AND password =?");
$result = $stmt->execute(array($username, $hashedPassword));
// count the returned rows, if you have 1 then good.
if($result->num_rows > 0) {
$row = $result->fetch_assoc();
// get the data you need and store it in the session.
$_SESSION['username'] = $row['username'];
return TRUE;
} else { return FALSE; }
}
function getForm() {
return "<form method='post'>username <input type=text name=username>\n";
return "password <input type=password name=password></form>;
}
}
My objective is to do less codes here:
$user = new users();
echo $user->getLoginForm()
if(isset($_POST['username'])) {
if($users->login($_POST['username'], crypt($_POST['password'], $salt) == TRUE) {
//logged in
session_start();
//other stuff
} else { echo "error"; }
}
I'm very new to Object-Oriented programming Thank you all in advance!