dsh77114 2013-04-27 19:37
浏览 55
已采纳

php会话超时所有代码在一个页面中

Regarding to How do I expire a PHP session after 30 minutes?, I copied some code from the 2nd answer Simple way of PHP session expiry in 30 minutes. I'd like to combine login and information to 1 page and another page is logout.php here is my code.

homepage.php

if(isset($_POST["submitform"])){

    $v1 = "admin";
    $v2 = "admin";
    $v3 = $_POST['username'];
    $v4 = $_POST['password'];

    if($v1 == $v3 && $v2 == $v4){
    session_start();
    $_SESSION['username'] = $v1;
    $_SESSION['start'] = time(); // taking now logged in time
    $_SESSION['expire'] = $_SESSION['start'] + (1* 30) ; // ending a session in 30 seconds

    if(!isset($_SESSION['username'])){
      echo "Please Login again <a href='logout.php'>Click Here to Login</a>";
    }else{
       $now = time(); // checking the time now when home page starts
        if($now > $_SESSION['expire']){
         session_destroy();
          echo "Your session has expire !  <a href='logout.php'>Click Here to Login</a>";
        }else{
        echo "This should be expired in 1 min <a href='logout.php'>Click Here to Login</a>";
        }
    }
    }else{
     echo '
    <form  method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <button type="submit" name="submitform">Sign in</button>
    </form>';
    echo  '<font color="red">wrong password</font>"';
    }       
 }else{
    echo '
    <form  method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <button type="submit" name="submitform">Sign in</button>
    </form>';
 }
?>

Logout.php

<?php
session_start();
session_destroy();
header('Location: homepage.php');
?>

I set session expire to 30 seconds, however I found the session doesn't expire as expected. The session never expire. I am wondering if i put session_start(); in a right place? Thanks

  • 写回答

3条回答 默认 最新

  • drvpv7995 2013-04-27 20:03
    关注

    You are only checking the status of the session on form post.

    If you refresh the page it will resend the post, logging you in and extending the session.

    Your logic needs to be:

    if post, check password and extend session.

    check if session has expired (this must happen if post there or not, makes no difference.)

    based on the outcome of the session check display either a login form or the log out message.

    if (isset($_POST["submitform"])) {
    
        $v1 = "admin";
        $v2 = "admin";
        $v3 = $_POST['username'];
        $v4 = $_POST['password'];
    
        if ($v1 == $v3 && $v2 == $v4) {
            session_start();
            $_SESSION['username'] = $v1;
            $_SESSION['start'] = time();
            // taking now logged in time
            $_SESSION['expire'] = $_SESSION['start'] + (1 * 30);
            // ending a session in 30 seconds
    
        } else {
            echo '
        <form  method="post">
        <input type="text" name="username">
        <input type="password" name="password">
        <button type="submit" name="submitform">Sign in</button>
        </form>';
            echo '<font color="red">wrong password</font>"';
         die();
        }
    
        if (!isset($_SESSION['username'])) {
            echo "Please Login";
            echo '
                <form  method="post">
                <input type="text" name="username">
                <input type="password" name="password">
                <button type="submit" name="submitform">Sign in</button>
                </form>';
        } else {
            $now = time();
            // checking the time now when home page starts
            if ($now > $_SESSION['expire']) {
                session_destroy();
                echo "Your session has expired !  <a href='logout.php'>Click Here to Login</a>";
            } else {
                echo "This should be expired in 1 min <a href='logout.php'>Click Here to Login</a>";
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系