Hướng dẫn php session expire after 10 minutes - phiên php hết hạn sau 10 phút

7

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi đang sử dụng kỹ thuật sau đây ...

Từ


0, biểu mẫu đăng đến trang

1 nơi tôi làm điều này

 0)
{
    $array = mysql_fetch_assoc($result);    

    session_start();
    $_SESSION['user_id'] = $uzer;
    header("Location:loggedin.php");            
}
else
{
    header("Location:login.php");
}
?>

Và trên trang


2 Điều đầu tiên tôi làm là


Nhưng một khi đăng nhập khi tôi trực tiếp nhập URL


3, nó sẽ hiển thị trang ... điều này có ý nghĩa hoàn hảo vì phiên đã bắt đầu

Những gì tôi muốn thực hiện là

  • Phiên URL trực tiếp \ hoạt động trong 10 phút sau khi phiên bị chấm dứt \ hết hạn

Tôi cần làm gì hoặc học

Đã hỏi ngày 22 tháng 9 năm 2010 lúc 14:17Sep 22, 2010 at 14:17

Hướng dẫn php session expire after 10 minutes - phiên php hết hạn sau 10 phút

Mặt trăngMoon

19.1K56 Huy hiệu vàng136 Huy hiệu bạc199 Huy hiệu đồng56 gold badges136 silver badges199 bronze badges

2

Lưu trữ dấu thời gian trong phiên:

 0)
{
    $array = mysql_fetch_assoc($result);    

    session_start();
    $_SESSION['user_id'] = $uzer;
    $_SESSION['login_time'] = time();
    header("Location:loggedin.php");            
}
else
{
    header("Location:login.php");
}
?>

Kiểm tra xem dấu thời gian có nằm trong cửa sổ thời gian được phép không (600 giây là 10 phút):

 600)
{
    header("Location:login.php");
}
else
{
    // uncomment the next line to refresh the session, so it will expire after ten minutes of inactivity, and not 10 minutes after login
    //$_SESSION['login_time'] = time();
    echo ( "this session is ". $_SESSION['user_id'] );
    //show rest of the page and all
}
?>

Đã trả lời ngày 22 tháng 9 năm 2010 lúc 14:31Sep 22, 2010 at 14:31

LekensteynlekensteynLekensteyn

62.7K21 Huy hiệu vàng154 Huy hiệu bạc190 Huy hiệu đồng21 gold badges154 silver badges190 bronze badges

4

Tôi sẽ xem xét session_set_cookie_params và


4

Đã trả lời ngày 22 tháng 9 năm 2010 lúc 14:31Sep 22, 2010 at 14:31

LekensteynlekensteynKyle

62.7K21 Huy hiệu vàng154 Huy hiệu bạc190 Huy hiệu đồng1 gold badge17 silver badges29 bronze badges

2

Tôi sẽ xem xét session_set_cookie_params và


4

session_set_cookie_params(600);

Kylekyle

1.8881 Huy hiệu vàng17 Huy hiệu bạc29 Huy hiệu đồngJun 1, 2021 at 9:49

Sử dụng chức năng Cookie Set Set Set trong tệp PHP của bạn, nơi bạn sẽ bắt đầu phiên, nó sẽ hết hạn sau khi xác định x phút.Kishan Chauhan

Như trên ở trên sau 10 phút phiên hết hạn.1 gold badge10 silver badges18 bronze badges

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọcsessions are maintained to check if the user is active. When the user becomes inactive and the user forgets to logout from the web page, there is a chance of other users viewing the page causing security breach. By default, a session in PHP gets destroyed when the browser is closed. Session timeout can be customized, to make the user’s page inactive after a fixed time. 
    Starting session: The PHP, session_start() function is used to start a session in the web page.
    Syntax: 
     

    session_start();

    Bàn luận After the start of the session, session variables can be created for future use. Session variables can be created and the values can be stored in those variables as follows:
    Syntax: 
     

    • Trong PHP, các phiên được duy trì để kiểm tra xem người dùng có hoạt động không. Khi người dùng không hoạt động và người dùng quên đăng xuất từ ​​trang web, có khả năng người dùng khác xem trang gây ra vi phạm bảo mật. Theo mặc định, một phiên trong PHP bị phá hủy khi trình duyệt bị đóng. Thời gian chờ phiên có thể được tùy chỉnh, để làm cho trang của người dùng không hoạt động sau một thời gian cố định.
       
     $_SESSION['var1']=5;
    • Các biến phiên: Sau khi bắt đầu phiên, các biến phiên có thể được tạo để sử dụng trong tương lai. Các biến phiên có thể được tạo và các giá trị có thể được lưu trữ trong các biến như sau: Cú pháp: & NBSP; & NBSP;
       
    $username="John";
    $_SESSION['username']=$username;

    Tạo biến phiên với tên biến ‘var1, và gán giá trị của‘ 5, có thể được thực hiện là: & nbsp; & nbsp; To remove all session variables that are initialized before destroying the session, the following command should be used:
    Syntax: 
     

    • Việc gán một biến cho một biến phiên có thể được thực hiện là: & nbsp; & nbsp;
       
    session_unset();
    • Phá hủy các biến phiên và phiên: Để xóa tất cả các biến phiên được khởi tạo trước khi phá hủy phiên, lệnh sau nên được sử dụng: Cú pháp: & nbsp; & nbsp;
       
    session_destroy();

    Để phá hủy phiên nhất định, lệnh sau đây sẽ được sử dụng: & nbsp; & nbsp; Considering there’s a login page with the ‘Login’ button in an HTML form. When the user clicks on the ‘Login’ button, session starts and session variables are set. A session variable to store the time of login is initialized. It is then directed to the home page of the user. 
     

    • Để phá hủy phiên hoàn thành, lệnh sau đây sẽ được sử dụng: & nbsp; & nbsp; 
       

    Thay đổi thời gian chờ phiên: Xem xét có một trang đăng nhập với nút ‘Đăng nhập ở dạng HTML. Khi người dùng nhấp vào nút ‘Đăng nhập, phiên bắt đầu và các biến phiên được đặt. Một biến phiên để lưu trữ thời gian đăng nhập được khởi tạo. Sau đó, nó được chuyển đến trang chủ của người dùng. & Nbsp; & nbsp;

    
    
    5

    
    
    6

    Trang đăng nhập: & nbsp; & nbsp;

     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    3
     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    4
    
    
    9
     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    0
     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    7
     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    8

     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    9
     600)
    {
        header("Location:login.php");
    }
    else
    {
        // uncomment the next line to refresh the session, so it will expire after ten minutes of inactivity, and not 10 minutes after login
        //$_SESSION['login_time'] = time();
        echo ( "this session is ". $_SESSION['user_id'] );
        //show rest of the page and all
    }
    ?>
    
    0
     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    0
     600)
    {
        header("Location:login.php");
    }
    else
    {
        // uncomment the next line to refresh the session, so it will expire after ten minutes of inactivity, and not 10 minutes after login
        //$_SESSION['login_time'] = time();
        echo ( "this session is ". $_SESSION['user_id'] );
        //show rest of the page and all
    }
    ?>
    
    2
     600)
    {
        header("Location:login.php");
    }
    else
    {
        // uncomment the next line to refresh the session, so it will expire after ten minutes of inactivity, and not 10 minutes after login
        //$_SESSION['login_time'] = time();
        echo ( "this session is ". $_SESSION['user_id'] );
        //show rest of the page and all
    }
    ?>
    
    3
    
    
    7
     600)
    {
        header("Location:login.php");
    }
    else
    {
        // uncomment the next line to refresh the session, so it will expire after ten minutes of inactivity, and not 10 minutes after login
        //$_SESSION['login_time'] = time();
        echo ( "this session is ". $_SESSION['user_id'] );
        //show rest of the page and all
    }
    ?>
    
    5

     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    9
     600)
    {
        header("Location:login.php");
    }
    else
    {
        // uncomment the next line to refresh the session, so it will expire after ten minutes of inactivity, and not 10 minutes after login
        //$_SESSION['login_time'] = time();
        echo ( "this session is ". $_SESSION['user_id'] );
        //show rest of the page and all
    }
    ?>
    
    0
     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    0
     600)
    {
        header("Location:login.php");
    }
    else
    {
        // uncomment the next line to refresh the session, so it will expire after ten minutes of inactivity, and not 10 minutes after login
        //$_SESSION['login_time'] = time();
        echo ( "this session is ". $_SESSION['user_id'] );
        //show rest of the page and all
    }
    ?>
    
    9
    session_set_cookie_params(600);
    
    0

     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    9
    session_set_cookie_params(600);
    
    2
    session_set_cookie_params(600);
    
    3
    session_set_cookie_params(600);
    
    4

    session_set_cookie_params(600);
    
    5

    session_set_cookie_params(600);
    
    6

    •  

    PHPsession_start() function is called. This enables us to retrieve session variables from this page. Using time() function, the current time can be calculated. The difference between the current time and the session variable created at the time of login should not exceed the desired timeout. When the duration exceeds, the session is destroyed and the page is redirected to the Login page.
    Like if the Session timeout=10 minutes. The session should automatically destroy after 10 minutes = 10*60 seconds = 600 seconds
     

    • 
      
      7
      
      
      8
      
      
      9
       0)
      {
          $array = mysql_fetch_assoc($result);    
      
          session_start();
          $_SESSION['user_id'] = $uzer;
          $_SESSION['login_time'] = time();
          header("Location:loggedin.php");            
      }
      else
      {
          header("Location:login.php");
      }
      ?>
      
      0
       0)
      {
          $array = mysql_fetch_assoc($result);    
      
          session_start();
          $_SESSION['user_id'] = $uzer;
          $_SESSION['login_time'] = time();
          header("Location:loggedin.php");            
      }
      else
      {
          header("Location:login.php");
      }
      ?>
      
      1
       0)
      {
          $array = mysql_fetch_assoc($result);    
      
          session_start();
          $_SESSION['user_id'] = $uzer;
          $_SESSION['login_time'] = time();
          header("Location:loggedin.php");            
      }
      else
      {
          header("Location:login.php");
      }
      ?>
      
      2
       
       

    Thay đổi thời gian chờ phiên: Xem xét có một trang đăng nhập với nút ‘Đăng nhập ở dạng HTML. Khi người dùng nhấp vào nút ‘Đăng nhập, phiên bắt đầu và các biến phiên được đặt. Một biến phiên để lưu trữ thời gian đăng nhập được khởi tạo. Sau đó, nó được chuyển đến trang chủ của người dùng. & Nbsp; & nbsp;

    
    
    5

    
    
    6

     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    3
     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    4
     600)
    {
        header("Location:login.php");
    }
    else
    {
        // uncomment the next line to refresh the session, so it will expire after ten minutes of inactivity, and not 10 minutes after login
        //$_SESSION['login_time'] = time();
        echo ( "this session is ". $_SESSION['user_id'] );
        //show rest of the page and all
    }
    ?>
    
    0
     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    0
     600)
    {
        header("Location:login.php");
    }
    else
    {
        // uncomment the next line to refresh the session, so it will expire after ten minutes of inactivity, and not 10 minutes after login
        //$_SESSION['login_time'] = time();
        echo ( "this session is ". $_SESSION['user_id'] );
        //show rest of the page and all
    }
    ?>
    
    2
    session_start();
    4

    session_start();
    5

     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    9
     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    3
    session_start();
    8
     600)
    {
        header("Location:login.php");
    }
    else
    {
        // uncomment the next line to refresh the session, so it will expire after ten minutes of inactivity, and not 10 minutes after login
        //$_SESSION['login_time'] = time();
        echo ( "this session is ". $_SESSION['user_id'] );
        //show rest of the page and all
    }
    ?>
    
    0
     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    0
     600)
    {
        header("Location:login.php");
    }
    else
    {
        // uncomment the next line to refresh the session, so it will expire after ten minutes of inactivity, and not 10 minutes after login
        //$_SESSION['login_time'] = time();
        echo ( "this session is ". $_SESSION['user_id'] );
        //show rest of the page and all
    }
    ?>
    
    9
     $_SESSION['var1']=5;
    2

     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    9
    session_start();
    5

     $_SESSION['var1']=5;
    5
     $_SESSION['var1']=5;
    6

     $_SESSION['var1']=5;
    5
     $_SESSION['var1']=5;
    8

     $_SESSION['var1']=5;
    5
    session_set_cookie_params(600);
    
    2
    $username="John";
    $_SESSION['username']=$username;
    1
    session_set_cookie_params(600);
    
    4

     0)
    {
        $array = mysql_fetch_assoc($result);    
    
        session_start();
        $_SESSION['user_id'] = $uzer;
        $_SESSION['login_time'] = time();
        header("Location:loggedin.php");            
    }
    else
    {
        header("Location:login.php");
    }
    ?>
    
    9
    session_set_cookie_params(600);
    
    5

    session_set_cookie_params(600);
    
    5

    $username="John";
    $_SESSION['username']=$username;
    6

    session_start();
    5

    Trang đăng nhập: & nbsp; & nbsp;

    session_set_cookie_params(600);
    
    5

    session_set_cookie_params(600);
    
    6

    •  

    Làm cách nào để hết hạn một phiên PHP sau 30 phút?

    Notes:..
    phiên họp. ....
    Nếu bạn muốn hết hạn phiên sau 30 phút hoạt động thay vì sau 30 phút kể từ khi bắt đầu, bạn cũng sẽ cần sử dụng setcookie với thời gian hết hạn ()+60*30 để giữ cho cookie phiên hoạt động ..

    Làm thế nào để bạn tăng thời gian hết hạn phiên trong PHP?

    Nếu bạn sử dụng xử lý phiên mặc định của PHP, cách duy nhất để thay đổi đáng tin cậy thời lượng phiên trong tất cả các nền tảng là thay đổi php.ini.Đó là bởi vì trong một số nền tảng, bộ sưu tập rác được triển khai thông qua một tập lệnh chạy mọi thời điểm nhất định (một tập lệnh cron) đọc trực tiếp từ PHP.change php. ini. That's because in some platforms, garbage collection is implemented through a script that runs every certain time (a cron script) that reads directly from php.

    Phiên PHP kéo dài bao lâu?

    Theo mặc định, các biến phiên kéo dài cho đến khi người dùng đóng trình duyệt.Vì thế;Các biến phiên chứa thông tin về một người dùng duy nhất và có sẵn cho tất cả các trang trong một ứng dụng.until the user closes the browser. So; Session variables hold information about one single user, and are available to all pages in one application.

    Làm cách nào để giữ cho một phiên PHP tồn tại?

    Mỗi phiên PHP có giá trị thời gian chờ - thời lượng, được đo bằng giây - xác định thời gian một phiên sẽ tồn tại trong trường hợp không có bất kỳ hoạt động người dùng nào.Bạn có thể điều chỉnh thời gian chờ này bằng cách thay đổi giá trị của biến.GC_MAXLIFETIME trong tệp cấu hình PHP (php.ini).changing the value of session. gc_maxlifetime variable in the PHP configuration file ( php. ini ).