Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 禁止在平面文件数据库中使用相同的用户名_Php_File_Flat - Fatal编程技术网

Php 禁止在平面文件数据库中使用相同的用户名

Php 禁止在平面文件数据库中使用相同的用户名,php,file,flat,Php,File,Flat,我用平面文件数据库为我的申请做了一个注册页面。我想知道是否有可能使用平面文件数据库使注册者不能使用与已注册者相同的名称,如果有,如何实现 以下是我的注册页面: <div align="center"> <?PHP if (isset($_POST['submit'])) { $username = $_POST["username"]; $password = $_POST["password"]; $password1 = $_POST["password1"];

我用平面文件数据库为我的申请做了一个注册页面。我想知道是否有可能使用平面文件数据库使注册者不能使用与已注册者相同的名称,如果有,如何实现

以下是我的注册页面:

<div align="center">    
<?PHP

if (isset($_POST['submit']))
{

$username = $_POST["username"];
$password = $_POST["password"];
$password1 = $_POST["password1"];

if(empty($username)) die(print '<script> alert ("Enter Username"); window.location="registration.php"; </script>');
if(empty($password)) die(print '<script> alert ("Enter Password"); window.location="registration.php"; </script>');
if($password != $password1) die(print '<script> alert ("Password doesn\'t match"); window.location="registration.php"; </script>'); 

require_once('recaptchalib.php'); // reCAPTCHA Library
$privkey = "xxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Private API Key
$verify = recaptcha_check_answer($privkey, $_SERVER['REMOTE_ADDR'],   $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);

if ($verify->is_valid) { 

$file = file_get_contents("data.txt");
$string = "$username||$password";
if(!strstr($file, "$string"))
{
$myFile = "data.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$username||$password\n";
fwrite($fh, $stringData);
print '<script> alert ("Registration Complete"); window.location="/~u1206424/index.php";  </script>';
fclose($fh);
}
else
{
die(print '<script> alert ("Sorry the username: <b>$username</b> is already registered.   Please use diferent username"); window.location="registration.php"; </script>');

}

}
else {

die(print '<script> alert ("You did not enter the correct Captcha.  Please try again"); window.location="registration.php"; </script>');    

}
}
?>
</div>
<!doctype html>
<html>
<head>
<title>Registration</title>
</head>
<body>
<div id="container" style="width:500px; height:500px; border: 2px solid black; margin:auto">

<?php include "header.php"; ?>

<div id="content" style="background-color:#EEEEEE; width:500px; height:400px; float: left">
<br>
<form align="center" method="post" action="registration.php" >
Username:
<input type="text" name="username" />
<br/>
<br/>
Password:
<input type="password" name="password" />
<br/>
<br/>
Confirm:
<input type="password" name="password1" />
<br/>
<br/>
<?php
require_once('recaptchalib.php'); // reCAPTCHA Library
$pubkey = "6Lcx-esSAAAAAIps5xUbcy7ty45P1usxQWheLpXO"; // Public API Key
echo recaptcha_get_html($pubkey); // Display reCAPTCHA
?>
<input type="submit" value="Register" name="submit" />
</form>
</div>

<?php include "footer.php"; ?>

</div>
</body>
</html>

登记

用户名:

密码:

确认:

这个(我几年前写的代码片段)是我进入MySQL之前经常使用的,这是我强烈建议的

但是考虑到项目的性质,您可以使用以下内容

N.B.:我建议您通过
.htaccess
保护您的
data.txt
文件,例如:

<Files data.txt>
order allow,deny
deny from all
</Files>

如果我选择
dasdsadasd | | xcxaaqs\n | | | | | | | | | | | | | | | | | | | | | n\n
作为用户名会怎么样?我知道,这只是我正在尝试的一些基本功能,我还在学习你的代码@用户3112130
$emails = file_get_contents("data.txt");
$email = $_POST['email'];

if ( preg_match( "/(?:^|\W){$email}(?:\W|$)/", $emails ) ) {
//die ("Sorry, your Email is already in our database.");
header('Location: exists.php');
}

else {
    header('Location: thank_you.php');  
}