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

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

May 25, 2016 pm 04:58 PM

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);
}
Copy after login

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)