Hướng dẫn how can solve session expired in php? - cách giải quyết phiên hết hạn trong php?

Bạn nên thực hiện thời gian chờ phiên của riêng bạn. Cả hai tùy chọn được đề cập bởi những người khác (session.gc_maxlifetime và session.cookie_lifetime) không đáng tin cậy. Tôi sẽ giải thích những lý do cho điều đó.

First:

session.gc_maxlifetime session.gc_maxlifetime Chỉ định số giây sau đó dữ liệu sẽ được xem là 'rác' và được làm sạch. Bộ sưu tập rác xảy ra trong phiên bắt đầu.
session.gc_maxlifetime specifies the number of seconds after which data will be seen as 'garbage' and cleaned up. Garbage collection occurs during session start.

Nhưng bộ thu rác chỉ được bắt đầu với xác suất của phiên.gc_probability chia cho phiên.gc_divisor. Và sử dụng các giá trị mặc định cho các tùy chọn đó (lần lượt là 1 và 100), cơ hội chỉ ở mức 1%.

Chà, bạn có thể chỉ cần điều chỉnh các giá trị này để người thu gom rác được bắt đầu thường xuyên hơn. Nhưng khi người thu gom rác được bắt đầu, nó sẽ kiểm tra tính hợp lệ cho mỗi phiên đã đăng ký. Và đó là tốn nhiều chi phí.

Hơn nữa, khi sử dụng các tệp phiên bản mặc định của PHP.save_handler, dữ liệu phiên được lưu trữ trong các tệp trong một đường dẫn được chỉ định trong phiên.save_path. Với trình xử lý phiên đó, tuổi của dữ liệu phiên được tính toán vào ngày sửa đổi cuối cùng của tệp và không phải là ngày truy cập cuối cùng:

Lưu ý: Nếu bạn đang sử dụng trình xử lý phiên dựa trên tệp mặc định, hệ thống tập tin của bạn phải theo dõi thời gian truy cập (ATIME). Windows Fat không, vì vậy bạn sẽ phải đưa ra một cách khác để xử lý rác thu thập phiên của bạn nếu bạn bị mắc kẹt với hệ thống tập tin chất béo hoặc bất kỳ hệ thống tập tin nào khác khi không theo dõi ATIME. Kể từ Php 4.2.3, nó đã sử dụng MTIME (ngày sửa đổi) thay vì ATIME. Vì vậy, bạn sẽ không gặp vấn đề với các hệ thống tập tin khi theo dõi ATIME không có sẵn. If you are using the default file-based session handler, your filesystem must keep track of access times (atime). Windows FAT does not so you will have to come up with another way to handle garbage collecting your session if you are stuck with a FAT filesystem or any other filesystem where atime tracking is not available. Since PHP 4.2.3 it has used mtime (modified date) instead of atime. So, you won't have problems with filesystems where atime tracking is not available.

Vì vậy, nó cũng có thể xảy ra rằng một tệp dữ liệu phiên bị xóa trong khi chính phiên vẫn được coi là hợp lệ vì dữ liệu phiên không được cập nhật gần đây.

Và thứ hai:

session.cookie_lifetime session.cookie_lifetime Chỉ định tuổi thọ của cookie trong vài giây được gửi đến trình duyệt. […]
session.cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser. […]

Vâng đúng vậy. Điều này chỉ ảnh hưởng đến tuổi thọ cookie và chính phiên vẫn có thể hợp lệ. Nhưng đó là nhiệm vụ của máy chủ để vô hiệu hóa một phiên chứ không phải máy khách. Vì vậy, điều này không giúp được gì. Trên thực tế, việc có phiên.cookie_lifetime được đặt thành 0 sẽ làm cho phiên cookie cookie thành một cookie phiên thực sự chỉ có giá trị cho đến khi trình duyệt được đóng.

Kết luận / Giải pháp tốt nhất:

Giải pháp tốt nhất là thực hiện thời gian chờ phiên của riêng bạn. Sử dụng dấu thời gian đơn giản biểu thị thời gian của hoạt động cuối cùng (tức là yêu cầu) và cập nhật nó với mọi yêu cầu:

if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
    // last request was more than 30 minutes ago
    session_unset();     // unset $_SESSION variable for the run-time 
    session_destroy();   // destroy session data in storage
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp

Cập nhật dữ liệu phiên với mọi yêu cầu cũng thay đổi ngày sửa đổi của tệp phiên để phiên không bị xóa bởi Trình thu thập rác sớm.

Bạn cũng có thể sử dụng dấu thời gian bổ sung để tái tạo ID phiên định kỳ để tránh các cuộc tấn công vào các phiên như cố định phiên:

if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}

Notes:

  • session.gc_maxlifetime ít nhất phải bằng với tuổi thọ của người xử lý hết hạn tùy chỉnh này (1800 trong ví dụ này);
  • 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 hết hạn
    if (!isset($_SESSION['CREATED'])) {
        $_SESSION['CREATED'] = time();
    } else if (time() - $_SESSION['CREATED'] > 1800) {
        // session started more than 30 minutes ago
        session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
        $_SESSION['CREATED'] = time();  // update creation time
    }
    
    0 để giữ cho cookie phiên hoạt động.

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

Lưu bài viết

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.sessions 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();

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

  • 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;
     
 $_SESSION['var1']=5;
  • 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;
     
$username="John";
$_SESSION['username']=$username;

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; To remove all session variables that are initialized before destroying the session, the following command should be used:
Syntax: 
 

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

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

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

PHP

if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
1

if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
2

if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
3
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
4
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
5
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
6
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
7
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
8

if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
9
session_start();
0
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
5
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
6
session_start();
3
session_start();
4

session_start();
5
session_start();
6
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
6
session_start();
8
session_start();
9
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
3
 $_SESSION['var1']=5;
1

session_start();
5
session_start();
6
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
6
 $_SESSION['var1']=5;
5
 $_SESSION['var1']=5;
6

session_start();
5
 $_SESSION['var1']=5;
8
 $_SESSION['var1']=5;
9
$username="John";
$_SESSION['username']=$username;
0

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

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

  •  

Trên trang chủ, để duy trì phiên, hàm session_start () được gọi. Điều này cho phép chúng tôi truy xuất các biến phiên từ trang này. Sử dụng hàm thời gian (), thời gian hiện tại có thể được tính toán. Sự khác biệt giữa thời gian hiện tại và biến phiên được tạo tại thời điểm đăng nhập không được vượt quá thời gian chờ mong muốn. Khi thời lượng vượt quá, phiên bị phá hủy và trang được chuyển hướng đến trang đăng nhập. Giống như nếu thời gian chờ phiên = 10 phút. Phiên sẽ tự động phá hủy sau 10 phút = 10*60 giây = 600 giây & nbsp;session_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
 

  • Trang chủ: & nbsp; & nbsp; 
     

PHP

if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
1

if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
2

if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
9
session_start();
0
session_start();
6
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
6
session_start();
8
session_unset();
0

session_unset();
1

session_start();
5
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
9
session_unset();
4
session_start();
6
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
6
 $_SESSION['var1']=5;
5
session_unset();
8

if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
3
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
4
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
5
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
6
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
7
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}
8

session_destroy();
1
session_destroy();
2

session_destroy();
1
session_destroy();
4

session_destroy();
1
 $_SESSION['var1']=5;
8
session_destroy();
7
$username="John";
$_SESSION['username']=$username;
0

session_start();
5
$username="John";
$_SESSION['username']=$username;
1

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

02

session_unset();
1

session_start();
5
 $_SESSION['var1']=5;
8
session_destroy();
7
$username="John";
$_SESSION['username']=$username;
0

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

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

  •  

Khi nào phiên hết hạn trong PHP?

Thời gian chờ phiên được sử dụng để đặt giới hạn thời gian cho sự không hoạt động của người dùng. Giả sử, nếu giới hạn thời gian chờ phiên được đặt thành 60 giây và người dùng không hoạt động trong 60 giây thì phiên của người dùng đó sẽ hết hạn và người dùng sẽ yêu cầu đăng nhập lại để truy cập trang web.if the session timeout limit is set to 60 seconds and the user is inactive for 60 seconds then the session of that user will be expired and the user will require to log in again to access the site.

Làm thế nào tôi có thể tăng thời gian phiên của tôi?

Nhấp vào Máy chủ> Loại máy chủ> Máy chủ ứng dụng WebSphere> WebSphere_portal_portalnode2.Nhấp vào Cài đặt container> Quản lý phiên> Đặt thời gian chờ.Nhập giá trị thời gian chờ mong muốn trong vài phút.Bấm OK.

Làm thế nào chúng ta có thể bảo tồn phiên trong PHP?

$ tuổi thọ = 600;session_start ();setCookie (session_name (), session_id (), thời gian ()+ $ oreetime);

Lý do cho phiên hết hạn là gì?

Nếu kết nối Internet của bạn không ổn định, việc ngắt kết nối và kết nối lại định kỳ, nó có thể khiến một phiên trang web hết hạn.Khi kết nối Internet bị mất, kết nối trang web có thể bị chấm dứt, kết quả là một thông báo đã hết hạn phiên nếu bạn cố gắng truy cập bất kỳ trang nào sau khi internet kết nối lại., it can cause a website session to expire. When the Internet connection is lost the website connection can be terminated, resulting in a session expired message if you try to access any page after the Internet reconnects.