简体   繁体   中英

Mysql database not updating when PHP script executes

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?

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 ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM