Hướng dẫn php destroy session by id - phiên hủy php theo id

(Php 4, Php 5, Php 7, Php 8)

session_destroy - Phá hủy tất cả dữ liệu được đăng ký vào một phiênDestroys all data registered to a session

Sự mô tả

session_destroy (): bool(): bool

session_destroy () phá hủy tất cả các dữ liệu được liên kết với phiên hiện tại. Nó không hủy bỏ bất kỳ biến toàn cầu nào liên quan đến phiên hoặc mở cookie phiên. Để sử dụng lại các biến phiên, session_start () phải được gọi. destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

Lưu ý: Bạn không phải gọi session_destroy () từ mã thông thường. Dọn dẹp mảng $ _Session thay vì phá hủy dữ liệu phiên.: You do not have to call session_destroy() from usual code. Cleanup $_SESSION array rather than destroying session data.

Để tiêu diệt phiên hoàn toàn, ID phiên cũng phải không được đặt. Nếu cookie được sử dụng để tuyên truyền ID phiên (hành vi mặc định), thì cookie phiên phải bị xóa. setCookie () có thể được sử dụng cho điều đó.setcookie() may be used for that.

Khi phiên.use_strict_mode được bật. Bạn không phải xóa cookie ID phiên lỗi thời vì mô -đun phiên sẽ không chấp nhận cookie ID phiên khi không có dữ liệu liên quan đến ID phiên và đặt cookie ID phiên mới. Kích hoạt phiên.use_strict_mode được khuyến nghị cho tất cả các trang web.

Cảnh báo

Xóa phiên ngay lập tức có thể gây ra kết quả không mong muốn. Khi có các yêu cầu đồng thời, các kết nối khác có thể thấy mất dữ liệu phiên đột ngột. ví dụ. Yêu cầu từ JavaScript và/hoặc yêu cầu từ các liên kết URL.

Mặc dù mô -đun phiên hiện tại không chấp nhận cookie ID phiên trống, nhưng việc xóa phiên ngay lập tức có thể dẫn đến cookie ID phiên trống do điều kiện đua phụ của máy khách (trình duyệt). Điều này sẽ dẫn đến việc khách hàng tạo ra nhiều ID phiên không cần thiết.

Để tránh những điều này, bạn phải đặt dấu thời gian xóa thành $ _Session và từ chối truy cập trong khi sau này. Hoặc đảm bảo ứng dụng của bạn không có yêu cầu đồng thời. Điều này cũng áp dụng cho session_regenerate_id ().session_regenerate_id() also.

Thông số

Chức năng này không có tham số.

Trả về giá trị

Trả về true khi thành công hoặc false về thất bại.true on success or false on failure.

Ví dụ

Ví dụ số 1 phá hủy một phiên với $ _Session

// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();// Unset all of the session variables.
$_SESSION = array();// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
    
$params session_get_cookie_params();
    
setcookie(session_name(), ''time() - 42000,
        
$params["path"], $params["domain"],
        
$params["secure"], $params["httponly"]
    );
}
// Finally, destroy the session.
session_destroy();
?>

Xem thêm

  • session.use_strict_mode
  • session_reset () - Tái tạo lại mảng phiên với các giá trị ban đầu
  • session_regenerate_id () - Cập nhật ID phiên hiện tại với một bản mới được tạo
  • unset () - Und đặt một biến đã cho
  • setCookie () - Gửi cookie

Praveen V ¶

10 năm trước

If you want to change the session id on each log in, make sure to use session_regenerate_id(true) during the log in process.

session_start();
session_regenerate_id(true);
?>

[Edited by moderator (googleguy at php dot net)]

Jack Luo ¶

8 năm trước

It took me a while to figure out how to destroy a particular session in php. Note I'm not sure if solution provided below is perfect but it seems work for me. Please feel free to post any easier way to destroy a particular session. Because it's quite useful for functionality of force an user offline.

1. If you're using db or memcached to manage session, you can always delete that session entry directly from db or memcached.

2. Using generic php session methods to delete a particular session(by session id).

$session_id_to_destroy = 'nill2if998vhplq9f3pj08vjb1';
// 1. commit session if it's started.
if (session_id()) {
   
session_commit();
}
// 2. store current session id
session_start();
$current_session_id = session_id();
session_commit();// 3. hijack then destroy session specified.
session_id($session_id_to_destroy);
session_start();
session_destroy();
session_commit();// 4. restore current session id. If don't restore it, your current session will refer to the session you just destroyed!
session_id($current_session_id);
session_start();
session_commit();?>

JBH ¶

5 năm trước

false1

false2

false3

false4

Greald tại Gmail Dot Com ¶

1 năm trước

false6

Làm thế nào chúng ta có thể phá hủy một phiên?

session_destroy () phá hủy tất cả các dữ liệu được liên kết với phiên hiện tại.Nó không hủy bỏ bất kỳ biến toàn cầu nào liên quan đến phiên hoặc mở cookie phiên.Để sử dụng lại các biến phiên, session_start () phải được gọi.Lưu ý: Bạn không phải gọi session_destroy () từ mã thông thường. destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called. Note: You do not have to call session_destroy() from usual code.

Làm thế nào để bạn làm trống một biến phiên trong PHP?

$ _Session ['tên'];Đầu ra: Nếu bạn muốn phá hủy tất cả các biến phiên, thì hãy sử dụng hàm PHP sau.session_destroy ();session_destroy();

Phương pháp nào được sử dụng để xóa một phiên trong PHP?

Chức năng session_destroy () được sử dụng để phá hủy một phiên.Hàm này phá hủy phiên hoàn chỉnh.Để hủy đặt một biến phiên duy nhất, chúng ta có thể sử dụng hàm Und (). function is used to destroy a session. This function destroys the complete session. To unset a single session variable, we can use the unset() function.

Phiên $ trong PHP là gì?

PHP $ _Session là một mảng kết hợp có chứa tất cả các biến phiên.Nó được sử dụng để đặt và nhận các giá trị biến phiên.Ví dụ: Lưu trữ thông tin.an associative array that contains all session variables. It is used to set and get session variable values. Example: Store information.