2013-12-20 40 views
0

我嘗試在SESSION目錄的文件中存儲多個會話值,但是當我嘗試存儲新值時,它會覆蓋舊值並僅存儲新值。在會話日誌文件中存儲多個用戶會話值

<?php 
    session_start(); 
    ?> 
    <html> 
    <head> 
    <title>Simple HTML Form</title> 
    </head> 
    <body> 
    <?php 
    $_SESSION['userName'] = 'nancy'; 
    $_SESSION['emailAddress'] = '[email protected]'; 
    $_SESSION['userName'] = 'preeti'; 
    $_SESSION['emailAddress'] = '[email protected]'; 

    $session_data = session_encode(); // Get the session data 
    // change the name below for the folder you want 
$dir = "SESSION"; 
$file_to_write = 'session_log'; 
$content_to_write = " $session_data"; 
if(is_dir($dir) === false) 
{ 
    mkdir($dir); 
} 
$file = fopen($dir . '/' . $file_to_write,"w"); 
// a different way to write content into 
// fwrite($file,"Hello World."); 
fwrite($file, $content_to_write); 
// closes the file 
fclose($file); 
// this will show the created file from the created folder on screen 
include $dir . '/' . $file_to_write; 
?> 
</body> 
</html> 

我應該怎麼做

+0

我認爲你必須追加數據,每次你在閱讀模式下打開文件並覆蓋現有的數據,而不是你已經將數據追加到文件上 –

回答

1

創建用戶的陣列和數據追加到它:

$_SESSION['users'] = array(); 

$_SESSION['users'][] = array(
    'name' => 'nancy', 
    'email' => '[email protected]', 
); 

$_SESSION['users'][] = array(
    'name' => 'preeti', 
    'email' => '[email protected]', 
); 
0
add values in session array so your values not overide like this 

聲明數組 $ _SESSION [ '信息'] =陣列();

$_SESSION['info'][] = array(
    'fullname' => 'manish', 
    'email' => '[email protected]', 
); 


$_SESSION['info'][] = array(
    'fullname' => 'manish1', 
    'email' => '[email protected]', 
);