Hướng dẫn can we set time for session in php? - chúng ta có thể đặt thời gian cho phiên trong php không?

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.

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;

    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

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

    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

    •  

    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
     

    • 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
       
       

    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;

    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

    session_start[];
    5
    session_unset[];
    1

    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

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

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

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

    •  

    Làm cách nào để thay đổi thời gian chờ phiên?

    Để thay đổi giá trị, hãy làm theo các bước sau: Chọn Quản trị hệ thống> Cài đặt> Tham số hệ thống để mở trang tham số hệ thống.Trên tab Chung, trong phần Quản lý Phiên, nhập một giá trị trong thời gian chờ hoạt động không hoạt động trong trường phút.Chọn Lưu.

    Thời gian phiên mặc định trong PHP là gì?

    Thời gian phiên mặc định tính bằng PHP là 24 phút [1440 giây] và đường dẫn phiên mặc định trong Php là/var/lib/php5/phiên.Bạn có thể thay đổi nó bằng cách chỉnh sửa tệp cấu hình PHP [PHP. INI] trên máy chủ web của bạn.24 minutes [1440 seconds] and Default path of Session in PHP is /var/lib/php5/sessions. You can change it by editing your php-configuration[php. ini] file on your webserver.

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

    Một phiên trong 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.

    Chủ Đề