登录PHP mysql时记录用户ip

时间:2016-06-16 00:15:16

标签: php mysql logging login

我想在用户登录时记录用户IP,我希望它能够更新列 我知道$ _SERVER [' REMOTE_ADDR'];得到他们的IP。 我想在mysql的用户名行登录时登录。 这是我的mysql表的图像 https://gyazo.com/cf5b223df03d0da8a15bf61ed037d847

LoginCheck.php:

    <?php

    # Processes
    function cleanString($con, $string) {
        return mysqli_real_escape_string($con, stripcslashes($string));
    }

    # buttons use the request method
    if (isset($_REQUEST['login'])) {
        $username = strtolower(cleanString($con, $_POST['username']));
        $password = cleanString($con, $_POST['password']);

        $errors = array();

        if (empty($username) || empty($password)) {
            # If they left the shit blank like a jew
            $errors[] = "Please make sure you entered a valid username and password";
        }

        $password = md5($password);

        $db_check_username = mysqli_query($con, "SELECT username FROM users WHERE username='$username' OR email='$username'");
        $db_check_userdata = mysqli_query($con, "SELECT username,password FROM users WHERE username='$username' AND password='$password' OR email='$username' AND password='$password'");

        if (!$db_check_username || !$db_check_userdata) {
            $errors[] = mysqli_error($con);
        }

        if (mysqli_num_rows($db_check_username) == 0) {
            # If the username doesn't exist like a bitch
            $errors[] = "No account could be found with that username.";
        }
        if (mysqli_num_rows($db_check_userdata) == 0) {
            # If the Username and Password don't match
            $errors[] = "Username and Password combination incorrect.";
        }

        if(empty($errors)) {
            session_start();
            $_SESSION['username'] = $username;

            $success[] = "You have successfully logged in. Redirecting in a moment.";

            echo '<meta http-equiv="refresh" content="5; url=index.php" />';
        } else {
            $danger = $errors;
        }
    }

?>

的login.php

<?php

    # Include da files mate
    include('includes/config.php');
    include('includes/logincheck.php');

    # Other Shit
    //nothing yet
?>
<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->
<!--[if !IE]><!-->
<html lang="en">
<!--<![endif]-->
<head>
    <meta charset="utf-8" />
    <title>Twisted Movies | Login Page</title>
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport" />
    <meta content="" name="description" />
    <meta content="" name="author" />

    <!-- ================== BEGIN BASE CSS STYLE ================== -->
    <link href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet">
    <link href="assets/plugins/jquery-ui/themes/base/minified/jquery-ui.min.css" rel="stylesheet" />
    <link href="assets/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
    <link href="assets/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
    <link href="assets/css/animate.min.css" rel="stylesheet" />
    <link href="assets/css/style.min.css" rel="stylesheet" />
    <link href="assets/css/style-responsive.min.css" rel="stylesheet" />
    <link href="assets/css/theme/red.css" rel="stylesheet" id="theme" />
    <!-- ================== END BASE CSS STYLE ================== -->

    <!-- ================== BEGIN BASE JS ================== -->
    <script src="assets/plugins/pace/pace.min.js"></script>
    <!-- ================== END BASE JS ================== -->
</head>
<body class="pace-top">
    <!-- begin #page-loader -->
    <div id="page-loader" class="fade in"><span class="spinner"></span></div>
    <!-- end #page-loader -->

    <div class="login-cover">
        <div class="login-cover-image"><img src="assets/img/login-bg/bg-1.jpg" data-id="login-cover-image" alt="" /></div>
        <div class="login-cover-bg"></div>
    </div>
    <!-- begin #page-container -->
    <div id="page-container" class="fade">
        <!-- begin login -->
        <div class="login login-v2" data-pageload-addclass="animated fadeIn">
            <!-- begin brand -->
            <div class="login-header">
                <div class="brand">
                    <span class="logo"></span> Twisted Movies
                    <small>Where the best movies are AD free!</small>
                </div>
                <div class="icon">
                    <i class="fa fa-sign-in"></i>
                </div>
            </div>
            <!-- end brand -->
            <div class="login-content">
                <form action="" method="POST" class="margin-bottom-0">

                    <div class="text-center">
                        <?php

                            if (!empty($success)) {
                                foreach ($success as $value) {
                                    echo '<div class="alert alert-success">';
                                    echo $value.'<br>';
                                    echo '</div>';
                                }
                            } elseif (!empty($danger)) {
                                foreach ($danger as $value) {
                                    echo '<div class="alert alert-danger">';
                                    echo $value.'<br>';
                                    echo '</div>';
                                }
                            } elseif (!empty($warning)) {
                                foreach ($warning as $value) {
                                    echo '<div class="alert alert-warning">';
                                    echo $value.'<br>';
                                    echo '</div>';
                                }
                            } elseif (!empty($info)) {
                                foreach ($info as $value) {
                                    echo '<div class="alert alert-info">';
                                    echo $value.'<br>';
                                    echo '</div>';
                                }
                            } else {
                                echo '<div class="alert alert-info">';
                                echo "Please enter a username and password.";
                                echo '</div>';
                            }

                        ?>
                    </div>

                    <div class="form-group m-b-20">
                        <input type="text" name="username" placeholder="Username" class="form-control input-lg"/>
                    </div>
                    <div class="form-group m-b-20">
                        <input type="password" name="password" placeholder="Password" class="form-control input-lg"/>
                    </div>
                    <div class="checkbox m-b-20">
                        <label>
                            <input type="checkbox" /> Remember Me
                        </label>
                    </div>
                    <div class="login-buttons">
                        <button type="submit" name="login" class="btn btn-success btn-block btn-lg">Sign me in</button>
                    </div>
                    <div class="m-t-20">
                        Not a member yet? Click <a href="register.php">here</a> to register.
                    </div>
                        <center><?php include 'includes/footer.php'; ?></center>
                </form>
            </div>
        </div>
        <!-- end login -->
    </div>
    <!-- end page container -->

    <!-- ================== BEGIN BASE JS ================== -->
    <script src="assets/plugins/jquery/jquery-1.9.1.min.js"></script>
    <script src="assets/plugins/jquery/jquery-migrate-1.1.0.min.js"></script>
    <script src="assets/plugins/jquery-ui/ui/minified/jquery-ui.min.js"></script>
    <script src="assets/plugins/bootstrap/js/bootstrap.min.js"></script>
    <!--[if lt IE 9]>
        <script src="assets/crossbrowserjs/html5shiv.js"></script>
        <script src="assets/crossbrowserjs/respond.min.js"></script>
        <script src="assets/crossbrowserjs/excanvas.min.js"></script>
    <![endif]-->
    <script src="assets/plugins/jquery-hashchange/jquery.hashchange.min.js"></script>
    <script src="assets/plugins/slimscroll/jquery.slimscroll.min.js"></script>
    <script src="assets/plugins/jquery-cookie/jquery.cookie.js"></script>
    <!-- ================== END BASE JS ================== -->

    <!-- ================== BEGIN PAGE LEVEL JS ================== -->
    <script src="assets/js/login-v2.demo.min.js"></script>
    <script src="assets/js/apps.min.js"></script>
    <!-- ================== END PAGE LEVEL JS ================== -->

    <script>
        $(document).ready(function() {
            App.init(ajax=true);
            LoginV2.init();
        });
    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

在你有session_start()之后:

$ip = $_SERVER['REMOTE_ADDR'];;
$stmt = $con->prepare("UPDATE users SET `IP`=? WHERE username=?");
$stmt->bind_param("ss", $ip, $username);
$stmt->execute();

一些提示:

  • 尝试在处理用户数据时始终使用预准备语句。
  • 使用HTTP类获取IP。 IP并不总是在REMOTE_ADDR中,特别是如果站点位于代理之后,例如Cloudflare