File is not beeing saved correctly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I have made a mechanism using PHP to take the user ID and check if it is 
in an array, if it is *not* it is supposed to put it there as the key 
and the time as the value, this array is being saved in serialized file 
and recovered everything one user logs in the check it again.

The problem is sometimes it just *erase* the whole content of the 
array/file and start it again.

Following is the explained code I am using:

In the login procedure:
setMemberTodayVisit($user_id);

/*
-  It is supposed to create *one* file each day to store the data
-  For each access of different ID it is supposed to store the ID and 
the time
*/
function setMemberTodayVisit($id){
    $folder = getTodayVisitFolder();
    $file = $folder."/".date('dmY').".dat";
    $when = date('H:i:s');
    if(is_file($file)){
        $fp = fopen($file, "r");
        while($temp = fgets($fp, 1000)){
           $obj .= $temp;
        }
        fclose($fp);
        $visit = unserialize($obj);
       
        $found = false;
        // search the ID in the file
        foreach($visit as $key => $value){
            if($key == $id){
                $found = true;
                break;
            } else continue;
        }       
       
        // if not found the ID put it in the file, otherwise do nothing
       // Possiblely the problem is happening here.... How can I have 
this operation in a safe-mode?
        if(!$found){
            @unlink($file);
            $visit[$id] = $when;
            $fp = fopen($file, "w");
            fputs($fp, serialize($visit));
            fclose($fp);           
        } else {
            // does nothing
        }
    } else { // the first insert - file does not exist
        $visit[$id] = $when;
        $fp = fopen($file, "w");
        fputs($fp, serialize($visit));
        fclose($fp);           
    }
}

Thanks
F.



PHP Data object relational mapping generator
http://www.metastorage.net/ 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-objects/

<*> To unsubscribe from this group, send an email to:
    php-objects-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 




[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux