自己搭建网络微数据库后端
2022-09-16 20:17 | 阅读量: 0 | 标签: tutorial, database, tinywebdb系统自带网络微数据库使用国外的服务器,速度太慢?使用比目微数据库,数据存在别人服务器上,不安全?下面介绍自己搭建简单的网络微数据库后端。不用复杂的mySQL知识。
准备工作
首先你要有自己的服务器,可以把相关文件上传,服务器要支持php。
下载文件 myTinyWebDB.zip ,解压,上传到你的服务器。
我是在本机测试(win10+wamp64),文件结构如下:
其中index.php文件内容如下:
<?php
header("Content-Type: application/json");
$file = "database.json";
if ($_SERVER['REQUEST_METHOD'] != "POST" || !isset($_REQUEST['tag'])) {
die("BadRequest");
}
if (isset($_REQUEST['value'])){
$tag = trim($_REQUEST['tag']);
$value = trim($_REQUEST['value']);
$f = fopen($file, 'r');
$data = fgets($f);
fclose($f);
$parsedData = json_decode($data, true);
$parsedData[$tag] = $value;
$f = fopen($file, 'w') or die("Can't open file");
fwrite($f, json_encode($parsedData));
fclose($f);
$result = array("STORED", $tag, $value);
echo json_encode($result);
}else{
$tag = trim($_REQUEST['tag']);
$f = fopen($file, 'r');
$data = json_decode(fgets($f), true);
fclose($f);
if(isset($data[$tag])){
$result = array("VALUE", $tag, $data[$tag]);
}else{
$result = array("VALUE", $tag, "");
}
echo json_encode($result);
}
?>
其中database.json文件内容如下(就一对括号):
{}
好了,运行你的服务器,已经可以开始用了。
逻辑设计
这里192.168.0.104是我本机的ip,你要换成你自己的服务器地址。
ok,网络微数据库就可以跟以前一样使用了。