简体   繁体   中英

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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