输入错误的登录详细信息时,为什么我的文本文件中没有用户IP地址输出?

问题描述 投票:0回答:1

在我的登录表单脚本中输入错误的登录凭据时,我无法输出用户IP地址到文本文件。我怎么能解决这个问题呢? Error at the bottom of the screenshot. GitHub repo link

此外,我认为这两个问题都存在于functions.php。但是,我会把index.php文件编码加入。

的functions.php

function getBrowser()
{
    $u_agent = $_SERVER['HTTP_USER_AGENT'];
    $ip_add = $_SERVER['HTTP_CLIENT_IP'];
    $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
    }
    elseif(preg_match('/Microsoft edge/i',$u_agent))
    {
        $bname = 'Microsoft edge';
        $ub = "Edge"; //supports Edge
    }


    // 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(
        'name'      => $bname,
        'version'   => $version,
        'platform'  => $platform,
        'ip' => $ip_add,
    );
}

function saveLog($log, $logFileName = 'log')
{
    if (is_null($log)) {
        $log = 'empty';
    }
    $now = time();

    $logFileName = str_replace('.txt', '', $logFileName);
    $logFileName = __DIR__.'/' . $logFileName .'.txt';

    $data = "\n-------";
    $data .= $now;
    $data .= "-----------\n";
    $data .= print_r($log, true);
    $data .= " \n\n------- SCRIPT----------\n\n";

    file_put_contents($logFileName, $data, FILE_APPEND);

}

index.php代码

<?php
//PHP method to use cache memory to store details
session_start();
//Makes the "config.php" file available to be executed from this page
require_once('dbconfig/config.php');
require_once 'functions.php';
?>

    <!DOCTYPE html>
    <html>

    <head>
        <!-- Site title, CSS external file and font awesome -->
        <title>Login Page</title>
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
        <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.css">
        <link rel="stylesheet" type="text/css" href="design.css">
    </head>

    <body>
        <!-- Nav Bar-->
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-bottom animated bounceInDown">
            <div class="container">
                <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 / Titles -->
        <div class="container">
            <div class="row animated bounceInDown">
                <div class="col text-center">
                    <h2>Login Form</h2>
                    <img src="imgs/icon-person-512.png" class="mx-auto d-block" style="width:15%">
                </div>
            </div>

            <!-- 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");
            } else { //here the login failed
                saveLog(getBrowser(),'wrongLogin');

                //redirect even if login has failed - to show an error msg
                header("Location: index.php?wrong_login");
            }
        }
    }
    ?>
</body>

</html>
php html
1个回答
2
投票

对于第一个错误:$_SERVER["HTTP_CLIENT_IP"]没有标准化,你可能需要$ip_add = $_SERVER['REMOTE_ADDR']

第二个错误是因为您在文档已经写出后尝试修改文档标题 - 在将一些内容写入页面之后,您正在使用header("Location: index.php?wrong_login");。您可以使用Javascript重定向替换它:

        } else { //here the login failed
            saveLog(getBrowser(),'wrongLogin');
            //redirect even if login has failed - to show an error msg

            ?>
                <script>window.location = "index.php?wrong_login"</script>
            <?php

或者移动检查HTML开头上方的$query_run变量的代码。

热门问题
推荐问题
最新问题