JDatabase-基于 PHP 以文本储存的数据库 0.2.3 震撼发布!-抛弃 MySQL!

mysql 一般的数据库防注入、读取什么的都很麻烦。特别是 MySQL 数据库的搬运,不在同一个网站空间。编写 ASP 的人用 access 数据库,可是虽然说 php 也可以调用 access,但是需要 odbc,并且如果用它开发项目还涉及到版权问题。之后就尝试使用 ini 文件读取的方法来存储数据...

mysql 一般的数据库防注入、读取什么的都很麻烦。特别是 MySQL 数据库的搬运,不在同一个网站空间。编写 ASP 的人用 access 数据库,可是虽然说 php 也可以调用 access,但是需要 odbc,并且如果用它开发项目还涉及到版权问题。之后就尝试使用 ini 文件读取的方法来存储数据,但是又太麻烦,于是 JDatabase 就此诞生了

你是否觉得 PHP 的数据存储是个很大的问题?虽然 MySQL 具有多方面的优点,但其最大的、也是最让我们头疼的缺点就是数据备份、搬迁超级不方便!与 asp 相比 php 是优点胜于缺点的,但为了数据库的问题不少站长可是操碎了心。接下来让我们对比一下 MySQL 网站与 JDb 网站之间的不同点:

MySQL 运行环境: 服务器必须安装 MySQL 程序,连接速度慢 JDb: 只需一个 php class 类,全网通用

MySQL 的数据搬迁: 备份网站文件;使用 phpmyadmin 备份数据库,还要注意编码问题 JDb 的网站搬迁: 备份网站文件,数据库同时备份了,非常方便

MySQL 的防注入: 没有任何防注入机制,数据库极易被入侵 JDb 的防注入: 自带检查注入的函数,一键防止注入,安全有保障

JDb 就如一个 php 脚本数据库,无需安装;将数据文件保存在网站空间上,备份方便;随着版本的更新 JDb 也越来越快速、安全、简单,让小型数据存储更加方便快捷!

JDb 遵守 MIT 开源协议,任何组织或个人都可以随意更改与发布,但注意保留版权注释。

JDb 是一个大项目,正在持续更新,不断完善,速度越来越快,功能越来越多,越来越可靠!

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<pre class="prettyprint">
<?php
/*
* 文件名:JDb.php
* 程序名:JuzeonDataBase
* 目前版本:0.2.3
* [说明:
* 本程序遵循 MIT 开源守则。
* 被授权人权利:
* 被授权人有权利使用、复制、修改、合并、出版发行、散布、再授权及贩售软件及软件的副本。
* 被授权人可根据程序的需要修改授权条款为适当的内容。
* 被授权人义务:
* 在软件和软件的所有副本中都必须包含版权声明和许可声明。
* 协议:
* Copyright (C) 2015 juzeon
* Permission is hereby granted, free of charge, 
* to any person obtaining a copy of this software 
* and associated documentation files (the "Software"),
* to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, 
* merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom 
* the Software is furnished to do so, subject to the 
* following conditions:
* The above copyright notice and this permission notice 
* shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
* DEALINGS IN THE SOFTWARE.
* ]
* JDb 作者:居正
* 作者网站:http://www.skyju.cc
* 文件最初创作日期:2015/05/29 晚
* (本段注释在任何时候请勿删除)
* */
class JDb{
   function createTable($table,$columns){
   	$path=$this->mainpath;
   	$columns="id," . $columns;
   	$filedir=$path . $table . "/";
   	mkdir($path . $table);
   	$c=explode(",", $columns);
   	$count=count($c);
   	for($i=0;$i<$count;$i++){
   		$file=fopen($path . $table . "/" . $c[$i] . ".php", "w");
   		if(flock($file, LOCK_EX)){
   			fwrite($file, "<?php /* #!#");
   			flock($file, LOCK_UN);
   		}
   	}
   }
   function JDb($mainpath){
   	$this->mainpath=$mainpath . "/";
   	if(!file_exists($this->mainpath)){
   		mkdir($this->mainpath);
   	}
   }
   /*最初函数*/
   
   
   function read($table,$column,$name){
   	$path=$this->mainpath;
   	$filedir=$path . $table . "/";
   	$fr=fopen($filedir . $column . ".php","r");
   	if(flock($fr, LOCK_EX)){
   		$t=explode("#!#", fread($fr, filesize($filedir . $column . ".php")));
   		flock($fr, LOCK_UN);
   	}
   	$count=count($t);
   	for($i=0;$i<$count;$i++){
   		if(stripos($t[$i],$name . "=")===false){}else{
   			return str_ireplace($name . "=","" , $t[$i]);
   		}
   	}
   	return "jdb_error";
   }
   /*read 函数快想吐血了,终于出来了*/
   
   function update($table,$column,$name,$newdata){
   	$path=$this->mainpath;
   	$filedir=$path . $table . "/";
   	$fr=fopen($filedir . $column . ".php","r");
   	if(flock($fr, LOCK_EX)){
   		$oldtext=fread($fr, filesize($filedir . $column . ".php"));
   		flock($fr,LOCK_UN);
   	}
   	$newnovel=$name . "=" . $newdata;
   	$oldnovel=$name . "=" . $this->read($table, $column, $name);
   	$newtext=str_ireplace($oldnovel, $newnovel, $oldtext);
   	$fw=fopen($filedir . $column . ".php","w");
   	if(flock($fw,LOCK_EX)){
   		fwrite($fw, $newtext);
   		flock($fw, LOCK_UN);
   	}
   }
   /*update 在 insert 的脑补之后也完成了*/
   function insert($table,$column,$name,$data){
   	$path=$this->mainpath;
   	$filedir=$path . $table . "/";
   	$fw=fopen($filedir . $column . ".php","a");
   	if(flock($fw, LOCK_EX)){
   		fwrite($fw, $name . "=" . $data . "#!#");
   		flock($fw, LOCK_UN);
   	}
   }
   /*最后才知道原来可以如此简单,read 可以调用的*/
   
   function delete($table,$column,$name){
   	$path=$this->mainpath;
   	$filedir=$path . $table . "/";
   	$fr=fopen($filedir . $column . ".php","r");
   	if(flock($fw, LOCK_EX)){
   		$oldtext=fread($fr, filesize($filedir . $column . ".php"));
   		flock($fw, LOCK_UN);
   	}
   	$oldnovel=$name . "=" . $this->read($table, $column, $name);
   	$newtext=str_ireplace($oldnovel . "#!#", "", $oldtext);
   	$fw=fopen($filedir . $column . ".php","w");
   	if(flock($fw, LOCK_EX)){
   		fwrite($fw, $newtext);
   		flock($fw,LOCK_UN);
   	}
   }
   /*这次 delete 轻松地在 update 之后完成了*/
   /*呼~delete 与 insert 第二次修改,解决了换行符方式*/
   
   function findMaxId($table){/*返回 0 就是没有数据*/
   	$i=1;
   	while(true){
   		$return=$this->read($table,"id",$i);
   		if($return=="jdb_error"){
   			return $i-1;
   		}
   		$i++;
   	}
   }
   function findNextId($table){/*使用 insert 时候下一个添加的 ID 值*/
   	return $this->findMaxId($table)+1;
   }
   function findIdByData($table,$column,$data){/*使用一个字段的字符串查找它的 ID,登录验证(判断)时可以用到*/
   	$maxid=$this->findMaxId($table);
   	for($i=0;$i<=$maxid;$i++){
   		$result=$this->read($table,$column,$i);
   		if($data==$result){
   			return $i;
   		}
   	}
   	return "jdb_error";
   }
   function idPlus($table){/*增加数据时操作 ID 请使用此函数,不要直接调用 insert。本函数用于 id++*/
   	$nextid=$this->findNextId($table);
   	$this->insert($table, "id",$nextid, $nextid);
   }
   function idDelete($table,$id){/*删除数据时操作 ID 请使用此函数,不要直接调用 delete。本函数用于 delete an id*/
   	$this->delete($table, "id", $id);
   }
   /*新增为了操作方便的函数*/
   function debug($de){
   	if($de==0){
   		error_reporting(0);
   	}else{
   		error_reporting(-1);
   	}
   }
   function killInject($str){
   	$str=str_ireplace("/*","\\*", $str);
   	$str=str_ireplace("*/","*\\", $str);
   	$str=str_ireplace("#!#","!#!", $str);
   	$str=str_ireplace("?>","?)", $str);
   	return $str;
   }
}

/* 使用实例:(此处在使用时可以删除)
* (echo "<meta charset='utf-8' />")
* 性质:
* \
* 【唯一性】:ID 是所有字段的身份识别的标志。ID 是唯一的、绝对的、安全的。在此之内任何字段可以提交唯一性,比如 username 可以
* 是唯一的,但是 password 就不行。只有唯一的字段才能使用 findIdByData 函数。
* \
* 
* 初始化类
* \
* require 'JDb.php';//导入 jdb 类文件
* $jdb=new JDb("./test");//test 是你的目录(数据库)名字,随意取
* \
*
* 在 test 目录 (数据库) 下创建一个叫做 tb 的表
* \
* $jdb->createTable("tb","name,password");//第二个项填写的是字段(columns),不同字段之间用逗号隔开
* //其中第一个字段为 id,是系统自动加上的,不需要你自己写
* \
*
* 给 tb 这个表加入一条数据
* \
* $nextid=$jdb->findNextId("tb");//找到下一个要添加的 ID
* $jdb->insert("tb","name",$nextid,$jdb->killInject("admin"));//添加名字,并且防止注入。主键是 ID,所以任何字段都得通过 ID 来识别。切记 ID 要对上号!
* $jdb->insert("tb","password",$nextid,$jdb->killInject(md5("123456789")));//添加 admin 的密码,并且防止注入
* $jdb->idPlus("tb");//id 表++。本函数必须在所有其他字段添加完成之后调用,并由系统自动完成,不要你自己调用 insert
* \
* 
* 账号密码遍历器(读所有字段内容示范)
* \
* for($i=1;$i<=$jdb->findMaxId($table);$i++){//使用 for 循环,查找最大的 ID,如果读的字段达到了最大的 ID 就停止。这里切记使用<=
* 	echo "这个人的名字:" . $jdb->read("tb","name", $i);//读内容,名字是 name,读取 ID(主键)为$i的这个数据
* 	echo "&nbsp;&nbsp";
* 	echo "这个人的密码:" . $jdb->read("tb","password", $i);
* 	echo "<br/>";
* }
* \
* 
* 登录验证 (ID 与 findIdByData 的使用)
* \
* $name="admin";
* $password=md5("123456789");//这里实际使用时是传入的参数
* $name_id=findIdByData("tb","name",$name);//因为 name 有唯一性,所以使用 findIdByData
* $id_password=read("tb","password",$name_id);//因为 password 有不唯一性,所以通过主键 ID 读取对应的 password
* if($password==$id_password){//通过输入的密码和数据库读取到的密码判断是否正确
* 	//登录成功
* }else{
* 	//登录失败
* }
* \
* 
* 删除一个数据 (idDelete 和 delete 函数的使用)
* \
* //比如我要删除一个名字是 admin 的人,我可以这样操作
* $name="admin";//名字是 admin 的人
* $id=findIdByData("tb","name",$name);//找到这个人的 ID。注意此处的 name(用户名)必须具有唯一性
* $jdb->delete("tb","name",$id);//删除这个人的名字
* $jdb->delete("tb","password",$id);//删除这个人的密码
* $jdb->idDelete("tb",$id);//最后删除这个人的 ID 表
* \
* 
* */
?>

JDb 下载地址:http://pan.baidu.com/s/1c08lTMo

JDb 使用范例:http://ec.skyju.cc

在 JDb 的代码中已经包括了使用的教程与实例,没有厚厚的帮助手册,查看几段简单的教程就可以轻松上手,毫无压力!

Licensed under CC BY-NC-SA 4.0