在登录时保存IP,如果IP更改则注销,PHP MySql

时间:2018-09-15 13:47:53

标签: php mysql

请注意,我是php和mysql的新手, 现在,我有一个可以正常使用的注册和登录系统。

我的mysql表如下: id用户名电子邮件密码ip

我想在每次登录时保存用户ip,如果ip已更改为自动注销用户,那么如果他再次登录,则新ip将在数据库中更新。

这是我的server.php

<?php
session_start();

// initializing variables
$username = "";
$email    = "";
$errors = array(); 

// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'registration');

// REGISTER USER
if (isset($_POST['reg_user'])) {
  // receive all input values from the form
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $email = mysqli_real_escape_string($db, $_POST['email']);
  $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
  $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);

  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($username)) { array_push($errors, "Username is required"); }
  if (empty($email)) { array_push($errors, "Email is required"); }
  if (empty($password_1)) { array_push($errors, "Password is required"); }
  if ($password_1 != $password_2) {
	array_push($errors, "The two passwords do not match");
  }

  // first check the database to make sure 
  // a user does not already exist with the same username and/or email
  $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);
  
  if ($user) { // if user exists
    if ($user['username'] === $username) {
      array_push($errors, "Username already exists");
    }

    if ($user['email'] === $email) {
      array_push($errors, "Email already exists");
    }
  }

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
  	$password = md5($password_1);//encrypt the password before saving in the database

  	$query = "INSERT INTO users (username, email, password) 
  			  VALUES('$username', '$email', '$password')";
  	mysqli_query($db, $query);
  	$_SESSION['username'] = $username;
  	$_SESSION['success'] = "You are now logged in";
  	header('location: index.php');
  }
}

// ... 
// ... 

// LOGIN USER
if (isset($_POST['login_user'])) {
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $password = mysqli_real_escape_string($db, $_POST['password']);

  if (empty($username)) {
  	array_push($errors, "Username is required");
  }
  if (empty($password)) {
  	array_push($errors, "Password is required");
  }

  if (count($errors) == 0) {
  	$password = md5($password);
  	$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
  	$results = mysqli_query($db, $query);
  	if (mysqli_num_rows($results) == 1) {
  	  $_SESSION['username'] = $username;
  	  $_SESSION['success'] = "You are now logged in";
  	  header('location: index.php');
  	}else {
  		array_push($errors, "Wrong username or password combination");
  	}
  }
}

?>

1 个答案:

答案 0 :(得分:0)

用户登录后,您可能正在为每个用户使用php会话。这是如何执行此操作的示例。 Using sessions & session variables in a PHP Login Script

为满足您的需要,您可以像这样将IP地址存储在会话的登录页面中。

$_SESSION['ip'] = $_SERVER['HTTP_X_FORWARDED_FOR'] 
             ? $_SERVER['HTTP_X_FORWARDED_FOR']
             : $_SERVER['REMOTE_ADDR'];

然后在其他页面上,您可以执行此操作以检测IP更改。

$ip = $_SERVER['HTTP_X_FORWARDED_FOR'] 
             ? $_SERVER['HTTP_X_FORWARDED_FOR']
             : $_SERVER['REMOTE_ADDR'];
if (isset($_SESSION['ip']) && $ip != $_SESSION['ip']) {
    session_destroy();
    /* redirect to your login pagge */ 
}

一些要点:

  1. 确保在您的php服务器上设置了session strict mode,以便session_destroy()有效。

  2. 当心:用户的IP地址可以在活动会话期间合法地更改:DHCP可以在用户不知道的情况下更改地址。稀有,但确实会发生。

  3. 如果您的用户在机构(企业)中,其中许多人似乎都具有相同的IP地址:您的网站所看到的就是他们共享的Internet网关的IP地址。

因此,关注用户的IP地址有点狡猾。