【问题标题】:Show alert message automatically after session timeout without manually refresh the page会话超时后自动显示警报消息,无需手动刷新页面
【发布时间】:2017-09-18 11:05:34
【问题描述】:

我有一个关于如何自动显示警报消息的问题。我已将时间限制设置为 10 秒,但我需要手动刷新页面,然后会弹出警报消息。将显示的警报消息将告诉用户会话结束并重新加载页面。这是我的代码

<?php
        //start session
        session_start();

        //database connection
        $conn = mysqli_connect("localhost","root","","test"); 

        //default timezone
        date_default_timezone_set('Asia/Kuala_Lumpur');

        //if user click login button
        if(!empty($_POST["login"])) 
        {
            //query table to verify inserted value
            $result = mysqli_query($conn,"SELECT * FROM users WHERE username = '" . $_POST["user_name"] . "' and password = '". $_POST["password"]."'");

            //fetch result result row as an associative, a numeric array, or both
            $row  = mysqli_fetch_array($result);

            //if it is true
            if($row) 
            {
                //declare a session for selected value using id and time logged in
                $_SESSION["user_id"] = $row['id']; 
                $_SESSION['timestamp'] = time();            
            } 
            else 
            {
                //redirect to homepage
                echo '<script type="text/javascript">alert("Invalid Username or Password!");window.location = "userlogin_session.php";</script>';
            }
        }

        //check for session timeout
        if(isset($_SESSION['timestamp']))
        {
            //set time limit in seconds
            $expireAfterSeconds = 10;

            //calculate many seconds have passed since the user was last active 
            $secondsInactive = time() - $_SESSION['timestamp'];

            //convert seconds into minutes
            $expireAfter = $expireAfterSeconds / 60 ;

            //check to see if time is equals or above given time limit
            if($secondsInactive >= $expireAfter)
            {
                //kill session.
                session_unset();
                session_destroy();

                //redirect to homepage
                echo '<script type="text/javascript">alert("Session Over");window.location = "userlogin_session.php";</script>';
            }
        }

        //if user click logout button
        if(!empty($_POST["logout"])) 
        {
            //kill session.
            session_unset();        
            session_destroy();
        }
    ?>

【问题讨论】:

标签: javascript php html


【解决方案1】:

您需要使用 Javascript,而不是 PHP。但是,您可以将 PHP var 发送到 javascript,或者只是对其进行硬编码(秒 * 1000),然后将其发送到警报或模式窗口:

setTimeout(function(){
   alert ('Session timeout message or code here');
}, <?= $timeout; ?>);

【讨论】:

  • 如果我这样做,警报消息将不会自动弹出。我已经试过了。但我使用 echo 你写的脚本。只有在 10 秒后才会显示警报。这就是我想要的。你能举个完整的例子吗?
  • @RichmondGingingon,为什么不会自动发出警报? ` setTimeout(function(){ alert('Session timeout message or code here'); }, 3000);` 3秒后会收到alert。
【解决方案2】:

这是我的完整代码。

<?php
        //start session
        session_start();

        //database connection
        $conn = mysqli_connect("localhost","root","","test"); 

        //default timezone
        date_default_timezone_set('Asia/Kuala_Lumpur');

        //if user click login button
        if(!empty($_POST["login"])) 
        {
            //query table to verify inserted value
            $result = mysqli_query($conn,"SELECT * FROM users WHERE username = '" . $_POST["user_name"] . "' and password = '". $_POST["password"]."'");

            //fetch result result row as an associative, a numeric array, or both
            $row  = mysqli_fetch_array($result);

            //if it is true
            if($row) 
            {
                //declare a session for selected value using id and time logged in
                $_SESSION["user_id"] = $row['id']; 
                $_SESSION['timestamp'] = time();            
            } 
            else 
            {
                //redirect to homepage
                echo '<script type="text/javascript">alert("Invalid Username or Password!");window.location = "userlogin_session.php";</script>';
            }
        }

        //check for session timeout
        if(isset($_SESSION['timestamp']))
        {
            //set time limit
            $expireAfterSeconds= 10;

            //calculate many seconds have passed since the user was last active 
            $secondsInactive = time() - $_SESSION['timestamp'];
            echo $secondsInactive;

            //check to see if time is equals or above given time limit
            if($secondsInactive >= $expireAfterSeconds)
            {
                //kill session.
                session_unset();
                session_destroy();

                //redirect to homepage
                //echo '<script type="text/javascript">alert("Session Over");window.location = "userlogin_session.php";</script>';
            ?>
                <script>
                    alert("Session Over");
                    window.location = "userlogin_session.php";
                </script>';
            <?php
            }
        }

        //if user click logout button
        if(!empty($_POST["logout"])) 
        {
            //kill session.
            session_unset();        
            session_destroy();
        }
    ?>
    <html>
        <head>
            <title>User Login</title>
        </head>
        <body>
            <?php 
                //if session not exist
                if(empty($_SESSION["user_id"])) 
                { 
                ?>
                    <form action="" method="post" id="frmLogin">
                        <div><?php if(isset($message)) { echo $message; } ?></div>  
                        <div>
                            <div><label for="login">Username</label></div>
                            <div><input name="user_name" type="text"></div>
                        </div>
                        <div>
                            <div><label for="password">Password</label></div>
                            <div><input name="password" type="password"> </div>
                        </div>
                        <div>
                            <div><input type="submit" name="login" value="Login"></span></div>
                        </div>       
                    </form>
                <?php 
                }

                //if session exist
                else 
                { 
                    $result = mysqli_query($conn,"SELECT * FROM users WHERE id = '" . $_SESSION["user_id"] . "'");
                    $row  = mysqli_fetch_array($result);
                ?>  
                    <form action="" method="post" id="frmLogout">
                        <div>
                            Welcome <?php echo ucwords($row['username']); ?>, You have successfully logged in!<br>Click to <input type="submit" name="logout" value="Logout">
                        </div>
                    </form>
                    </div>
                    </div>
                <?php 
                } 
            ?>
        </body>
    </html>

【讨论】:

  • @IstiaqueAhmed 希望你能修复它 :)
猜你喜欢
  • 2013-12-14
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
  • 2015-04-09
  • 2013-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多