Home >php教程 >PHP源码 >用户写入到(.txt)文件里

用户写入到(.txt)文件里

PHP中文网
PHP中文网Original
2016-05-25 16:58:441624browse

php代码

/* 检查用户昵称是否重复
 * 参数$user是登录用户的昵称
 * 返回值为true时,昵称可用
 * 返回值为false时,昵称不可用
 */
function chklogin($file,$user){
	$boo = false;
	if(file_exists($file)){
		$userarr = file($file);
		/* 判断昵称是否重复 */
		foreach($userarr as $value){
			$tmparr = explode('#',$value);
			if($user == $tmparr[0]){
				$boo = true;
				break;
			}
		}
	}
	return $boo;
}
/* 将登录的用户昵称写入到文件中
 * 保存格式为:昵称#ip#登录时间
 * $file:保存文件地址
 * $user:昵称
 * $ip:登录IP
 * $sex:性别
 */
function addlogin($file,$user,$ip,$sex){
	$tmp = $user.'#'.$ip.'#'.$sex.chr(13).chr(10);
	$fp = fopen($file,'a');
	$boo = fwrite($fp,$tmp);
	fclose($fp);
	return $boo;
}
/* 将用户列表转为数组
 * $file:用户列表文件
 */
function storeuser($file){
	$tmparr = file($file);
	$userarr = array();
	foreach($tmparr as $value){
		$tmparr = explode('#',$value);
		$userarr[] = $tmparr[0].','.$tmparr[2];
	}
	return $userarr;
}
/* 将发言内容写入到文件中
 * $file:保存文件地址
 * $mess:保存内容
 */
function addmess($file,$mess){
	$fp = fopen($file,'a');
	$boo = fwrite($fp,$mess.chr(13).chr(10));
	fclose($fp);
	return boo;
}
/* 在文件中删掉用户
 * $file:保存文件地址
 * $user:要删除的用户
 */
function deluser($file,$user){
	$tmparr = file($file);
	$rearr = array();
	foreach($tmparr as $value){
		$tmp = explode('#',$value);
		if($tmp[0] != $user){
			$rearr[] = $value;
		}
	}
	$fp = fopen($file,'w+');
	foreach($rearr as $value){
		fwrite($fp,$value);
	}
	fclose($fp);
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn