Saving Echo to a txt file

Hi so i have added validation to my login system, now i want to add code that would allow the echo which displays the error such as “Invalid email format” to be written into a txt file in a folder.

Right now all i have been able to do is display the data/time and ip address of the location of error but i want to display the echo based on what the user did wrong.

So for example if they tried to login with a wrong email i want the error.txt file to say the date/time, ip address and the error which was incorrect email.

Login - Mas Agency
	<!--web page meta tags-->
	<meta charset="utf-8" />
	
	<!--specific web page meta tags-->
	
	
	<!--CSS Links-->
	<link rel="stylesheet" type="text/css" href="style.css">
	
	
</head>
<form name="register" method="post" 
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >


<strong> Register Account</strong> <br /><br /> 

	
User name:
	<input type="text" name="username"><br /><br />

Password:
	<input type="text" name="password"><br /><br />

	<br /><br />
	<input type = "submit" value = "register" / >
<?php

session_start();

    //validate registration form

if ( (empty($_POST['username'])) OR (empty($_POST['password'])) )
{
        	//if the values are empty - 

echo "<br /> Please fill in all the above inputs";

}else{
	echo "<br> Process Validation";
	//if the inputs has values

	//store the data into local variables

	$Username = $_POST['username'];
	$Password = $_POST['password'];

	$Check=true;
	
	//Validate password - has to be more than 5 char
	if (strlen($Username) <5 )
	{
		echo "<br> Username or Password is incorrect";
		$Check=false;
	}

	//if first name has any special characters
	if (is_numeric($Username))
	{
		echo "<br> error - username has number...";
		$Check=false;
	}

	//if username is email format

	// Variable to check

	// Validate email
	if(filter_var($_POST['username'],FILTER_VALIDATE_EMAIL)){
	}
	else{
		echo '<br>Invalid email format';
		$Check=false;
	}

	//all the password validations

	if (strlen($Password) <5 )
	{
		echo "<br> error - password has less than 5 characters";
		$Check=false;
		
	           }

             	//if all the validation are true then prepare

	          //to store to online database
	if ($Check == true)
	{
	echo "<br> Checking.....";
	
	require_once('db.php'); //connect the database
	
	$SQL = "SELECT * FROM user WHERE username='$Username' and password='$Password'";
	
	$result = $conn->query($SQL); //execute the SQL query and store the data in $result as an array
	
	if ($result->num_rows>0)
	{
		$_SESSION['username'] = $username;
		//user information available in database
		echo "Login successful";
		echo "<br><br> re-directing to members page";
		header ( 'refresh:5; url=member.php?id='.$Username ); //re-directing to member page
	}
	else{
		//users info not found
		echo "<br> Username or Password incorrect";
	if(isset($_GET['logout'])){
		session_unregister('username');
	}
}


   }



      //set local variables
$ip = $_SERVER['REMOTE_ADDR'];
$date= date("d-m-y | h:i:sa");
$error = "Incorrect input";
$page ="page 1";

//path of the text file
$Data ="error/error.txt";

//open file as append
$File = fopen($Data, 'a');

//set the error pattern
$log_error = $ip." - ".$date." - ".$error;

//execute the write function
fwrite($File, $log_error."\n");



//close file
fclose($File);





}

?>


</body>
</html>

You probably want to look at the PHP function fwrite() - along with fopen() and fclose().

2 Likes

I done it, thanks

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.