2013-12-20 80 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]', 
);