简体   繁体   English

在平面文件数据库中不允许使用相同的用户名

[英]disallowing same username in flat file database

i made a registration page for my application by using flat file database. 我通过使用平面文件数据库为我的应用程序创建了注册页面。 I was wondering if it is possible using flat file database to make so whoever is registering cannot use the same name that is already registered, if so how? 我想知道是否有可能使用平面文件数据库来制作,所以无论谁注册,都不能使用已经注册的相同名称,如果可以,怎么办?

Below is my register page: 以下是我的注册页面:

<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>

This (snippet which I wrote a few years ago) is what I used to use before I got into MySQL, which is strongly suggested. 这(我几年前写的代码段)是我进入MySQL之前曾经使用的,强烈建议这样做。

But given the nature of your project, you can use what follows. 但是,鉴于项目的性质,您可以使用以下内容。

NB: I suggest you protect your data.txt file through .htaccess , for example: 注意:建议您通过.htaccess保护data.txt文件,例如:

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

The list would only contain email addresses, and not passwords, so you will need to do a seperate check to see if both usernames and passwords match, which I believe you already have. 该列表仅包含电子邮件地址,而不包含密码,因此您将需要进行单独检查以查看用户名和密码是否匹配,我相信您已经知道。

$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');  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM