简体   繁体   中英

Creating a Registration and Login Page

I'm currently in the process of making a Registration and Login Page. My first page asks you if you want to create an account or login. What we have to do is take the information from the form that the user enters information into, and place it into a text file. I've got this working roughly. I understand that this is not good practice for security reasons, but this is an assignment for class and I MUST put the information into the text file. I have the information going into the text file, however I am having trouble comparing the posted username to all of the usernames inside of the database. Here is my code for the newaccount.html page, and the register.php file that the form submits to.

newaccount.html

<html>
<head>
</head>
<body>

<h3>Hello new user! Choose a user name and password ^_^</h3><br>

<form action = "register.php" method = "POST" >
Username: <input type = "text" name = "username"><br><br>
Password: <input type = "password" name = "pass"><br>
<input type = "submit" value = "Create Account" name = "submit"><br>
</form>
<form method = "LINK" action = "Proj2_practice.html">
<input type = "submit" value = "Home Page" name = "submit2">
</form>

</body>  
</html>

register.php

<?php

$username = $_POST['username'];
$password = $_POST['pass'];

$userAndPass = $username.",".$password;

$userNames = 'usernames.txt'." ";
$passwords = 'passwords.txt'." ";

$fh_users = fopen($userNames, 'a+')or die("sorry, we couldnt open the file");
$fh_passwords = fopen($passwords, 'a+')or die("sorry, we couldnt open the file");

fwrite($fh_users, $username." ");
fwrite($fh_passwords, $password." ");

$allUserNames = fread($fh_users, filesize('usernames.txt'));

echo $allUserNames;

?>

The usernames and passwords are being sent to the text files correctly, At the end of the code, it is not echoing the variable. As of right now, there is no information in the text files, I don't know if that is the reason that nothing is being echoed. Is my approach correct here? What I'm trying to do here is send each name and password to a username and password text file.Then, Im planning on

exploding each of those text files by a space between them, which is why I add one after writing them to the text file, and then comparing the usernames and the passwords by their respective array elements. Am I overcomplicating this as much as I think I am? Please share your comments with me, I'm just trying to get better ^_^

Thank you in advance!

if you open the file with the "a+" flag it will place the file pointer on the end of the file. so when you're reading then you won't get anything as you're already on teh end of teh file.

just go back to the beginning of the file before reading

fseek ($fh_users, 0);

or close and open it again with flag "r"

$fh_users = fopen($userNames, 'r')

or simply do

$dataString = file_get_contents($userNames);

or

$dataArray = file($userNames);

If this is a class assignment, can I suggest a method as well as a solution?

If things like this don't work, try to reduce the complexity of what you're trying to do and build step by step. You say yourself that your file is empty; if it's empty you're not going to be able to read from it so your first problem is figuring out how to correctly write to a file and read stuff back from it. Forget about all of the rest of your task, focus on that first.

Now, if you open a resource (such as a file), you must close it again when you no longer need it. So you definitely need an fopen / fclose pair to begin with.

$username = "Penguin";
$userNames = 'usernames.txt'; // Why did you have a space after this?
$fh_users = fopen($userNames, 'a+');
fwrite($fh_users, $username." ");
fclose($userNames);

I haven't tested this, but at this point, you should have a file that contains "Penguin ". If you run the code multiple times, you should see the file contents grow.

If you want to read things back from this file, realise that there is a file pointer that determines where you are going to read or write. You can open the file again in such a way that the file pointer is moved to the front (and so you can read what you need) or you can explicitly move it back to the front using fseek or rewind .

Once you have gotten this writing and reading to work, then add code to handle your form and so on.

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