0

嗨,我正在使用serialize&unserialize函数和fwrite&fopen用于基于文本的数据库

<?php

ini_set('display_errors', 'on');
header('Content-Type: text/html; charset=utf-8');
$debugged = (bool) isset($_GET['debug']);
error_reporting($debugged ? E_ALL : 0);
$data_filename = "database.txt";

$admins = array(
    "admin" => "admin",
    "admin2" => "admin2"
);

$superadmin = "admin";
$order      = "asc";
include_once "functions.php";
?>

<?php
function clean($d) {
    return str_replace(array(
        "\t",
        "\n",
        "\s",
        "\r"
    ), "", $d);
}

function add($email, $age_id, $gender_id, $city, $prof_id, $model, $token, $os_version, $udid, $admin) {
    global $data_filename;

    $datax['email']      = $email;
    $datax['age_id']     = $age_id;
    $datax['gender_id']  = $gender_id;
    $datax['city']       = $city;
    $datax['prof_id']    = $prof_id;
    $datax['model']      = $model;
    $datax['token']      = $token;
    $datax['os_version'] = $os_version;
    $datax['udid']       = $udid;
    $datax['admin']      = $admin;

    $data         = array();
    $data_content = get_file_content($data_filename);
    if ($data_content !== NULL)
        $data = unserialize($data_content);
    $entrie       = serialize($datax);
    $data[]       = $entrie;
    $data_content = serialize($data);
    put_file_content($data_filename, $data_content);
    return $data_content;
}

function get_file_content($filename) {
    if (!file_exists($filename))
        fclose(fopen($filename, "w"));
    if (!file_exists($filename))
        die("sie.");
    $content = (filesize($filename) > 0) ? @fread(fopen($filename, "r+"), filesize($filename)) : NULL;
    if ($content === false)
        die("aq");
    return $content;
}
if (!$data_filename) {
    die("sie-31");
}
$datasey      = array();
$data_content = get_file_content($data_filename);
if ($data_content !== NULL)
    $datasey = @unserialize($data_content);

function put_file_content($filename, $content) {
    if (file_put_contents($filename, $content, LOCK_EX)) {
        return true;
    } else {
        usleep(250000);
        put_file_content($filename, $content, LOCK_EX);
    }
}

function reverse_data($data) {
    $data = array_reverse($data);
    foreach ($data as $key => $entrie)
        $key = $entries_num - $key - 1;
    return $data;
}




function del_admin_contents($who) {
    global $admin, $superadmin, $data_filename;
    ;
    $array        = array();
    $data_content = get_file_content($data_filename);
    if ($data_content === NULL)
        return;
    $array = unserialize($data_content);
    foreach ($array as $key => $value) {
        $bok = unserialize($value);
        if ($bok["admin"] == $who) {
            unset($array[$key]);
        }
    }
    $temp = array();
    foreach ($array as $value)
        $temp[] = $value;
    $data_content = serialize($array);
    put_file_content($data_filename, $data_content);
    return $data_content;
}


function del_entries($select, $udid) {
    global $data_filename;
    $data         = array();
    $data_content = get_file_content($data_filename);
    if ($data_content === NULL)
        return;
    $data         = unserialize($data_content);
    $data         = del_id($data, $select, $udid);
    $data_content = serialize($data);
    put_file_content($data_filename, $data_content);
    return $data_content;
}
function del_id($array, $id_array, $udid) {
    global $admin, $superadmin;
    foreach ($id_array as $id)
        if (isset($array[$id])) {
            $bok = unserialize($array[$id]);
            if ($bok["admin"] == $admin || $admin == $superadmin) {
                if ($bok["udid"] == $udid) {
                    unset($array[$id]);
                }
            }
        }
    $temp = array();
    foreach ($array as $value)
        $temp[] = $value;
    return $temp;
}

function up_id($array, $new_array, $id) {
    global $admin, $superadmin;
    if (isset($array[$id])) {
        $array[$id] = serialize($new_array);
    }
    return $array;
}


function update_id($data, $id, $new_array) {
    global $data_filename;
    $data         = up_id($data, $new_array, $id);
    $data_content = serialize($data);
    put_file_content($data_filename, $data_content);
    return true;
}

function emailupdate($udid, $email) {
    global $data_filename;
    $data         = array();
    $ret          = false;
    $data_content = get_file_content($data_filename);
    if ($data_content === NULL)
        return false;
    $data = unserialize($data_content);
    $id   = 0;
    foreach ($data as $d) {
        $new_array = unserialize($d);
        if ($new_array["udid"] == $udid) {
            $new_array["email"] = $email;
            update_id($data, $id, $new_array);
            $ret = true;
        }
        $id++;
    }
    return $ret;
}

function alldata() {
    global $datasey;
    $benimdata = array();
    if (count($datasey) > 0) {
        foreach ($datasey as $key => $entrie) {
            $gec         = unserialize($entrie);
            $benimdata[] = $gec;
        }
        return $benimdata;
    } else {
        return false;
    }
}



function cleaner() {
    global $data_filename, $max_entries;
    if ($max_entries == 0)
        return;
    $data         = array();
    $data_content = get_file_content($data_filename);
    if ($data_content !== NULL)
        $data = unserialize($data_content);
    while (count($data) >= $max_entries)
        $data = clear_id($data, array(
            0
        ));
    $data_content = serialize($data);
    put_file_content($data_filename, $data_content);
    return true;
}

function logincheck($user, $pass) {
    global $admins;
    if (array_key_exists($user, $admins)) {
        if ($admins[$user] == $pass) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}



$backup = "./backup/" . date("Ymd-H:i") . ".dat";

if (!file_exists($backup)) {
    $backup_data_content = get_file_content($data_filename);
    file_put_contents($backup, $backup_data_content, LOCK_EX);
}
?>

因为这里是我如何添加新值的示例:

 $data_content=add($_POST["email"],$_POST["age_id"],$_POST["gender_id"],$_POST["city_id"],$_POST["prof_id"],$_POST["model"],"N/A",$_POST["os_version"],$randomudid,$admin);

删除:

 $data_content=del_entries(array($_GET["del"]),$_GET["delete"]);

我的问题是,当 2 个管理员尝试同时输入一个值时,它只将最后一个发送的值写入数据库并删除旧值,我该如何解决这个问题?

4

0 回答 0