Please note this is not for a client, this is a small test learning test project for myself. I’m aware of the security aspects people are commenting about.
From my current built login script how can I store a users error login inputs into a text file using PHP every time they enter invalid login credentials?
I have a login script which is called login script 2. I was struggling with storing the userserror login inputs into a text file using PHP. From my understanding for the login script 2 for example I should put the coding to store the userserror logs and output them into a text file within this part of my coding shown below, however I’m not sure what code to use. As a result, I wanted to see if anyone had any suggestions. If you need a GitHub repository of the whole website, please leave a comment.
//IF the "$query_run" is NOT successful, then run the below
else {
}
?>
From looking at this https://stackoverflow.com/questions/20435348/create-login-accounts-and-store-them-in-a-text-file-using-php even though my code is different I was trying to apply this to my code, but I couldn’t seem to get the PHP to run. As a result, I assumed I messed up the code. I did try utilizing form the post, but didn’t have any luck getting it to work in my code.
$fp = fopen('accounts.txt', 'a+');
if(fwrite($fp, $text)) {
echo 'saved';
}
Login script two page code:
<!-- THE FORM -->
<!-- action="index.php" -- This shows where the PHP script that does the processing is located -->
<!-- method="post" -- This aspect identifies the action that will be performed with the data of the form. For example POST data to the "users" database -->
<form action="index.php" method="post">
<!-- Form/animation -->
<div class="inner_container text-center animated bounceInDown">
<!-- Username section -->
<label><b>Username:</b></label>
<input type="text" placeholder="Enter Username:" name="username" required>
<!-- Password section -->
<label><b>Password:</b></label>
<input type="password" placeholder="Enter Password:" name="password" required>
<input type="hidden" name="login" value="true">
<!-- The Login button -->
<button class="login_button" type="submit">Login <i class="fas fa-sign-in-alt"></i></button>
<!-- The button that is linked to the "register.php" page -->
<a href="register.php">
<button type="button" class="register_btn">Register <i class="fas fa-user-plus"></i></button>
</a>
<hr>
<!-- Help -->
<a href="https://marketinginsidergroup.com/content-marketing/10-types-online-forms-use/">
<button type="button" class="register_btn">Help <i class="fas fa-question-circle"></i></button>
</a>
</div>
</form>
<?php
//Condition, checking the Login button is pressed
if(isset($_POST['login']))
{
//The data from the Form (username & password) is stored into the @$username & @$passwordVariables
//You use @ before a VARIABLE in PHP when you do not want to initialise the VARIABLE before using it
@$username=$_POST['username'];
@$password=$_POST['password'];
//Statement that will SELECT the data from the "login" table, WHERE the Usename and Password typed match the typed ones
//Once the database is checked, if login details match than it stores the data in the "$query" VARIABLE
$query = "SELECT * FROM login WHERE username='$username' and password='$password' ";
//echo $query;
//This statement performs both the connection to the database using the values in the "$con" VARIABLE and
//The SELECT statement stored in the "$query" VARIABLE
$query_run = mysqli_query($con,$query);
//echo mysql_num_rows($query_run);
//IF the "$query_run" is run successfully, then
if($query_run) {
//Check if the Username and Password exist in the database, if they exist
if(mysqli_num_rows($query_run)>0) {
$row = mysqli_fetch_array($query_run,MYSQLI_ASSOC);
$_SESSION['username'] = $username; //Username handle aspect
$_SESSION['password'] = $password; //Password handle aspect
//Sent the user to the "homepage.php" page
header( "Location: homepage.php");
}
}
//IF the "$query_run" is NOT successful, then
else {
//Display this message
echo '<script type="text/javascript">alert("Database Error")</script>';
}
}
//IF the "$query_run" is NOT successful, then run the below
else {
}
?>
</body>
In relation to the text file and outputting the errors, I was thinking of using the bootstrap 4 table below I created to store them, however I’m not sure if this is possible.
<table class="table table-hover table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Username which was used</th>
<th scope="col">Password which was used</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<th scope="row">2</th>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2"></td>
<td></td>
</tr>
</tbody>
</table>