Cách làm mới phiên php

Trong PagesController của tôi, tôi viết một vài biến phiên và cấu hình, tùy thuộc vào trang mà người dùng đang truy cập. Các giá trị cấu hình đến từ tệp app_local.php của tôi. Biến phiên

// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 kiểm soát một số mục menu và một vài cài đặt nền khác. Đây là một ví dụ

    $session = $this->getRequest()->getSession();
    if ($page == 'nc' && Configure::read('NC.installed')) { 
        $session->write('module', 'nc');
        $modname = Configure::read('headertitle') . ": " . Configure::read('NC.name') . " Module";
        Configure::write('headertitle', $modname); 
    } else if ($page == 'pm' && Configure::read('PM.installed')) {
        $session->write('module', 'pm');
        $modname = Configure::read('headertitle') . ": " . Configure::read('PM.name') . " Module";
        Configure::write('headertitle', $modname);

… và cứ thế cho mỗi mô-đun được cài đặt trong dự án của tôi. Hệ thống menu được tải từ

// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
1 của tôi và dựa vào giá trị hiện tại của
// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 để tải chính xác các mục menu

Vấn đề. Khi người dùng di chuyển giữa các trang trong dự án của tôi, biến

// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 chỉ làm mới nếu trang được làm mới theo cách thủ công. Ví dụ. Nếu giá trị hiện tại của
// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 là
// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 và tôi điều hướng đến
// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
1, tôi muốn giá trị của
// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 cập nhật từ
// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 thành
// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
1 khi tải trang, nhưng giá trị này vẫn ở mức
// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 cho đến khi tôi làm mới thủ công. Sau đó, menu của tôi hiển thị đúng. Tôi có thể buộc làm mới biến phiên 10 ________ khi tải trang không?

Có thể điều này sẽ giúp. Tôi đã sửa lỗi thêm và hóa ra biến

// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 đang cập nhật trong PagesController của tôi, nhưng bản cập nhật không được phản ánh trong AppController của tôi

Khi tôi gỡ lỗi

// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 trong cả hai bộ điều khiển, tôi nhận được hai giá trị khác nhau, trừ khi tôi làm mới trang. Đây là quy trình làm việc

// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    

Kết quả gỡ lỗi 'mô-đun' trong cả hai bộ điều khiển, sau khi tôi rời khỏi trang 'doc' và tải trang 'ml'

 APP/Controller/AppController.php (line 176)
'doc'

APP/Controller/PagesController.php (line 112)
'ml'

Giá trị của

// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 trong AppController là mô-đun tôi đã tham gia trước khi nhấp vào 'doc' trong menu của mình. Khi tôi hiển thị trang 'doc', tôi phải làm mới trang để giá trị của
// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 cập nhật thành 'doc'. Tôi đã giả định rằng, vì
// Set in PagesController via $session->write() 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write('module', 'ml');  

// setMenu() called in beforeFilter of AppController
$this->setMenu();   

// 'module' session variable read in AppController::setMenu();
$module = $session->read('module');

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 là một biến phiên, nó sẽ cập nhật trên ứng dụng của tôi khi nó thay đổi. Vì vậy, những gì tôi cần là để nó cập nhật trong AppController của tôi cùng lúc nó cập nhật trong PagesController của tôi. Nhưng tôi đoán nó cập nhật chính xác ở phần sau vì nó thực sự được viết ở đó?

Làm cách nào để cập nhật một phiên trong PHP?

Cập nhật biến phiên trong PHP . start the session by calling session_start() function and then simply overwrite the vakue to update session variable.

Làm cách nào để tạo lại ID phiên trong PHP?

session_regenerate_id() sẽ thay thế id phiên hiện tại bằng id phiên mới và giữ nguyên thông tin phiên hiện tại. Khi phiên. use_trans_sid được bật, đầu ra phải được bắt đầu sau lệnh gọi session_regenerate_id(). Mặt khác, ID phiên cũ được sử dụng.

Tại sao session_start() được sử dụng trong PHP?

session_start() tạo phiên hoặc tiếp tục phiên hiện tại dựa trên mã định danh phiên được truyền qua yêu cầu GET hoặc POST hoặc được truyền qua cookie. When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.

Làm cách nào để xóa dữ liệu phiên trong PHP?

Một phiên PHP có thể bị hủy bởi hàm session_destroy() . Hàm này không cần bất kỳ đối số nào và một lệnh gọi có thể hủy tất cả các biến phiên. Nếu bạn muốn hủy một biến phiên thì bạn có thể sử dụng hàm unset() để hủy đặt một biến phiên.