如何在用户每次输入无效登录凭据时使用 PHP 将他们正在使用的 IP 地址和浏览器类型存储到文本文件中?



从我当前构建的登录脚本中,每次用户在我的网站上输入无效的登录凭据时,如何使用 PHP 将他们正在使用的用户 IP 地址和浏览器类型存储到文本文件中?

通过查看此创建登录帐户并使用 php 将它们存储在文本文件中,即使我的代码不同,我试图将其应用于我的代码,但我似乎无法让 PHP 运行。结果,我以为我搞砸了代码。我确实尝试使用帖子,但没有任何运气让它在我的代码中工作。

$fp = fopen('accounts.txt', 'a+');
if(fwrite($fp, $text))  {
echo 'saved';
} 

登录脚本两页代码:

<!-- 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>

就浏览器信息而言,如果有帮助,我已经在 PHP 中构建了它。我不确定这是否适用,因为我需要将其存储在文本文件中。

<?php
function getBrowser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version= "";
//Fetch platform info 
if (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'mac';        //supports mac 
}
elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'windows'; //supports windows 
}
// Next get the name of the useragent yes seperately and for good reason
if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
{
$bname = 'Internet Explorer'; //supports IE 
$ub = "MSIE";
}
elseif(preg_match('/Firefox/i',$u_agent)) //supports FIREFOX 
{
$bname = 'Mozilla Firefox'; 
$ub = "Firefox";
}
elseif(preg_match('/Chrome/i',$u_agent))
{
$bname = 'Google Chrome'; //supports chrome 
$ub = "Chrome";
}
elseif(preg_match('/Safari/i',$u_agent))
{
$bname = 'Apple Safari';
$ub = "Safari"; //supports safari 
}

// finally get the correct version number
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
// we have no matching number just continue
}
// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
$version= $matches['version'][0];
}
else {
$version= $matches['version'][1];
}
}
else {
$version= $matches['version'][0];
}
// check if we have a number
if ($version==null || $version=="") {$version="?";}
return array(
'userAgent' => $u_agent,
'name'      => $bname,
'version'   => $version,
'platform'  => $platform,
'pattern'    => $pattern
);
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<style>
body {
background-color: lightblue;
}
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
display: table
}
.container {
display: table-cell;
text-align: center;
vertical-align: middle
}
.text, .subtext {
font-weight: bold;
text-decoration: underline;
text-transform: uppercase;
}
</style>
</head>
<body> 
<!-- NAVBAR -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-bottom animated bounceInRight">
<div class="container">
<a class="navbar-brand text-uppercase" href="https://www.linkedin.com/in/liam-docherty/">handcrafted BY Liam Docherty <i class="far fa-copyright"></i></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="http://webbrowserinfo.96.lt">Go back to Homepage <i class="fa fa-home"></i>
<span class="sr-only">(current)</span>
</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- STRUCTURE -->
<div class="container text-center">
<div class="row">
<div class="col-md-12">
<img src="https://toppng.com/public/uploads/preview/browser-icon-android-lollipop-1153095741359wwghidpw.png" class="mx-auto d-block animated bounceInRight" style="width:20%">
</div>
</div>
<div class="row">
<?php
$ua=getBrowser();
?>
<div class="col-md-2"></div>
<div class="col-md-8 text animated bounceInRight" id="content">
<?php echo "Your browser: {$ua['name']}"; ?>
<br />
<?php echo "Version of the browser you're using is : {$ua['version']}"; ?>
<p class="subtext text-center animated bounceInRight">Thanks for using Liam's browser info script <i class="fas fa-smile-wink"></i></p>
</div>
<div class="col-md-2"></div>
</div>
</div>
</body>
</html>

关于文本文件和输出错误,我正在考虑使用下面我创建的引导程序 4 表来存储它们,但我不确定这是否可能。

<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>

您可以使用 file_put_contents() 函数。

file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] ) : int

此函数与依次调用 fopen()、fwrite() 和 fclose() 将数据写入文件相同。

如果文件名不存在,则创建该文件。否则,除非设置了 FILE_APPEND 标志,否则将覆盖现有文件。

参数:文件名:
写入数据的文件的路径。

数据:要写入的数据。可以是字符串、数组或流资源。

如果数据是流资源,则该流的剩余缓冲区将复制到指定的文件中。这与使用 stream_copy_to_stream() 类似。

您还可以将数据参数指定为单维数组。这相当于 file_put_contents($filename, implode('', $array))。

flags:标志的值可以是以下标志的任意组合,与二进制OR(|)运算符连接。

在您的情况下,您可以做的是:

file_put_contents("/path/of/file/to/store/data" , print_R($ua , true) , FILE_APPEND);

file_put_contents("/path/of/file/to/store/data" , json_encode($ua) , FILE_APPEND);

最新更新