1

I have a timeout function that runs correctly except for one thing. The field that checks if a user is currently logged in does not update once the script runs. I was testing it all week and got it to actually work, but when I tried to test it today, I stopped updating the field. Can anyone explain why? I also have a logout button that does a jQuery call which runs a logout.php script when the button is clicked which works perfectly. Could this be why the timeout script has stopped updating the database? I don't think that could what it is, but I could be wrong.

timeout script(php)

<?php
    require("../includes/header.php");
    $now = time();
    $expires = $_SESSION["expire"] + 30;
    $user = $_SESSION["id"];

    if(!isset($_SESSION["expire"]) || $expires > $now){
        $_SESSION["expire"] = $now;
    }
    else{
        mysqli_query($connect, "UPDATE `$user_table` SET `logged_in`=0 WHERE `user_id`='$user_id'");
        session_unset();
        session_destroy();
        mysqli_close($connect);
        header("Location: timed_out.php");
    }
?>

logout script(js & php)

$(document).ready(function(){
    $("#logout").on("click", function(){
        $.post("../php/logout.php", {}, function(response){
            if(response.success == "1"){
                location.replace("../pages/logged_out.php");
            }
        }, "json");
    })
})

logout script(php)

<?php
    ob_start();
    require("../includes/header.php");
    $user_id = $_SESSION["id"];

    ob_clean();
    mysqli_query($connect, "UPDATE `$user_table` SET `logged_in`=0 WHERE `user_id`='$user_id'");
    session_unset();
    session_destroy();
    mysqli_close($connect);
    echo json_encode(array("success"=>1));
?>

Another question I have is. Would it be beneficial to do the timeout script in jQuery instead of PHP? If so, how would I go about doing that?

1 Answer 1

3

I think you need some simple debuggin ;)

Your login script says in the query:

WHERE `user_id`='$user_id'

but the code a bit higher says

$user = $_SESSION["id"];

That's NOT the same variable ;)

Sign up to request clarification or add additional context in comments.

12 Comments

GAAHHH! Thank you! I was loosing my mind trying to figure out why. I must've done an accidental delete at some point.
same as his logout function
@Dorvalla nope, that says $user_id = $_SESSION["id"];
Indeed they do. Thanks again
@Robert That's where error reporting and checking for DB error comes in very handy ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.