Hi,
so I have basically the code from the tutorial I mentioned to handle the login.
Now I want a function that onlogin takes the current time and adds it to the users tabel under the column lastvisit.
This is the function code (not working)
Code: Select all
function addlastlogin() {
$thistime = date("Y-m-d H:i:s"); //DENNA AR NY
if(isset($_SESSION['user_id'])) {
$user_id = $_SESSION['user_id']; //KAN ANVANDAS FOR ATT LAGRA UPPGIFTER I USERS
if ($stmt = $mysqli->prepare("SELECT lastvisit FROM users WHERE id = ? LIMIT 1")) {
$stmt->bind_param('i', $user_id); // Bind "$user_id" to parameter.
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
if($stmt->num_rows == 1) { // If the user exists
$stmt = $mysqli->prepare("UPDATE users SET lastvisit= :lastvisit WHERE id= :id") {
$stmt->bind_param(':id', $user_id)
$stmt->bind_param(':lastvisit', $thistime);
$stmt->execute();
} else {
echo 'No luck';
}
} else {
echo 'No user logged in';
}
}
}
I can't see what I'm doing wrong??
(I used the following (Working) function as a template for my function.
Code: Select all
function login_check($mysqli) {
// Check if all session variables are set
if(isset($_SESSION['user_id'], $_SESSION['username'], $_SESSION['login_string'])) {
$user_id = $_SESSION['user_id'];
$login_string = $_SESSION['login_string'];
$username = $_SESSION['username'];
$user_browser = $_SERVER['HTTP_USER_AGENT']; // Get the user-agent string of the user.
if ($stmt = $mysqli->prepare("SELECT password FROM users WHERE id = ? LIMIT 1")) {
$stmt->bind_param('i', $user_id); // Bind "$user_id" to parameter.
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
if($stmt->num_rows == 1) { // If the user exists
$stmt->bind_result($password); // get variables from result.
$stmt->fetch();
$login_check = hash('sha512', $password.$user_browser);
if($login_check == $login_string) {
// Logged In!!!!
//addlastlogin(); //NY FUNKAR DEN??????????????????????????????????????????????????
return true;
} else {
// Not logged in
return false;
}
} else {
// Not logged in
return false;
}
} else {
// Not logged in
return false;
}
} else {
// Not logged in
return false;
}
}