无重复的PHP编写
我想以这样一种方式编写代码,它可以将任何输入写入并保存到.TXT文件中。然后应该有功能,将搜索整个文件的txt,如果一个文本或字已经写或保存之前,它应该带来错误 如果有人写了爱,而另一个人想再次输入爱,它应该会带来一条错误消息 这是我的密码无重复的PHP编写,php,fopen,fwrite,Php,Fopen,Fwrite,我想以这样一种方式编写代码,它可以将任何输入写入并保存到.TXT文件中。然后应该有功能,将搜索整个文件的txt,如果一个文本或字已经写或保存之前,它应该带来错误 如果有人写了爱,而另一个人想再次输入爱,它应该会带来一条错误消息 这是我的密码 <?php $name=$_POST['name']; $myfile= fopen("man.txt" , "a+") or die("Unable to open file!"); fwrite($myfile, "Name:".$name. "\
<?php
$name=$_POST['name'];
$myfile=
fopen("man.txt" , "a+") or
die("Unable to open file!");
fwrite($myfile, "Name:".$name. "\r\n")
?>
在这里,我使用in_array函数检查了一个数组,我们为提交的名称填充了$users
如果找到该名称,则表示存在回显名称。如果找不到,则回显“否”,并添加名称
<?php
$name = $_POST['name'];
if (empty($name)) { echo "Please enter your name"; } else $name=$name;
$myfile= fopen("mam.txt" , "a+") or die("Unable to open file!");
$path="mam.txt";
if (file_exists($path)) {
$contents = file_get_contents($path);
$contents = explode("\r\n", $contents);
$users = array();
foreach ($contents as $value) {
$users[] = $value;
}
// Search the file for the submitted name
if (in_array($name, $users)) {
// The name already exists
echo "Name exist";
} else {
// The name doesnt exist
echo "NO";
// Add the new name
fwrite($myfile, $name. "\r\n");
}
}
?>
到目前为止,您尝试了什么代码/方法?这里是另一个代码,非常感谢。成功了。如果我刷新页面时在TeX字段中没有任何内容,它将告诉我name exist。它将空空间视为数组。帮助