Why is my text file not linked my script to save the users IP?

How can I save a users IP in an external text file within my code? The purpose of my code is to save the users IP address when they input the wrong login details into my script. As you can see from my code it doesn’t output the IP as the link between when the user inputs the wrong details to the text file isn’t correctly set up. Can someone help me with doing this please?

saveLog(getBrowser(),'log.txt'); This code doesn’t seem to of successfully store the users IP after they input the wrong login details as the text file is empty.

else
 		{
 			$_SESSION['login'] = "";
 			echo "Invalid username or password, your information has been logged.";
            //here the login failed
                saveLog(getBrowser(),'log.txt');
                ?>
 		}
<?php
    //removing the errors/warnings from the page  
	
	if(isset($_POST['submit'])){
	
		$user = $_POST["username"];
		$pass = $_POST["password"];
		$validated = false;
			session_start();
		$_SESSION['login'] = "";
 
 	if($user!="" && $pass!="")
 	{
 		$conn = @mysql_connect ("localhost", "root", "") or die ("Sorry - unable to connect to MySQL database.");
 		$rs = @mysql_select_db ("users",$conn) or die ("error");
 
 		$sql = "SELECT * FROM user WHERE username = '$user' AND password ='$pass'";
 		$rs = mysql_query($sql,$conn);

 		$result = mysql_num_rows($rs);
 
 		if ($result > 0) $validated = true;
 		
 		if($validated)
 		{
 			$_SESSION['login'] = "OK";
 			$_SESSION['username'] = $user;
 			$_SESSION['password'] = $pass;
 				header('Location: protectedData.php');
 		}
 	
 	else
 		{
 			$_SESSION['login'] = "";
 			echo "Invalid username or password, your information has been logged.";
 		}
 	}
 	else $_SESSION['login'] = "";
	}
?>	

<html>
	<link rel="stylesheet" href="style1.css" type="text/css" />
	<body class="login">
 		<h1>Login Page</h1>
 		<p>Please enter your username and password:</p>
 
 <form action="loginData.php" method="post">
 	<table>
 		<tr>
 			<td align="right">Username: </td>
 			<td><input size=\"20\" type="text" size="20" maxlength="15" name="username"></td>	
 		</tr>
 
 		<tr>
 			<td align="right">Password: </td>
 			<td><input size=\"20\" type="password" size="20" maxlength="15" name="password"></td>
 		</tr>
 
 		<tr>
 			<td> </td>
 			<td colspan="2" align="left"><input type="submit" name="submit" value="Login"></td>
 			
 			
 		</tr>
 	</table>
 	</form>
	</body>
</html>

saveLog is not a built in function. So if it isn’t working we would need that code.

But there are so many issues with your code as is…
mysql_ functions are gone.
Prepared Statements are not being used.
Using isset($_POST[‘submit’]) for form submission.
Using a plain text password.
Storing the password in the session.
Using $_SESSION without starting [or continuing] the session.
Tables for layout purposes.

2 Likes

This might help.

Sponsor our Newsletter | Privacy Policy | Terms of Service