PHP Getting IP Address upon Login

Getting Started

Please take note that Bootstrap used in this tutorial are hosted so you need internet connection for them to work.

Creating our Database

1. Open phpMyAdmin. 2. Click databases, create a database and name it as ip. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `login` (
  2. `loginid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `userid` INT(11) NOT NULL,
  4. `ip_address` VARCHAR(50) NOT NULL,
  5. `login_date` datetime NOT NULL,
  6. PRIMARY KEY(`loginid`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  1. CREATE TABLE `user` (
  2. `userid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `username` VARCHAR(30) NOT NULL,
  4. `password` VARCHAR(30) NOT NULL,
  5. `name` VARCHAR(50) NOT NULL,
  6. PRIMARY KEY(`userid`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  1. INSERT INTO `user` (`userid`, `username`, `password`, `name`) VALUES
  2. (1, 'nurhodelta', 'devierte', 'neovic');
howtodatabase Notice that we have created a sample user. You can use this to login: Username: nurhodelta Password: devierte

Creating our Connection

Next, we create our connection to our database. This will serve as the bridge between our forms and database. We name this as conn.php.
  1. <?php
  2.  
  3. $conn = new mysqli("localhost", "root", "", "ip");
  4.  
  5. if ($conn->connect_error) {
  6. die("Connection failed: " . $conn->connect_error);
  7. }
  8.  
  9. ?>

index.php

This contains our simple login. If you want to learn login with verifications, I have tutorials of them so feel free to look for them.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>PHP Getting IP Address upon Login</title>
  5. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  6. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1 class="page-header text-center">PHP Getting IP Address upon Login</h1>
  11. <div class="row">
  12. <div class="col-md-4 col-md-offset-4">
  13. <div class="login-panel panel panel-primary">
  14. <div class="panel-heading">
  15. <h3 class="panel-title"><span class="glyphicon glyphicon-lock"></span> Sign In</h3>
  16. </div>
  17. <div class="panel-body">
  18. <form method="POST" action="login.php">
  19. <fieldset>
  20. <div class="form-group">
  21. <input class="form-control" placeholder="Username" name="username" type="text" autofocus>
  22. </div>
  23. <div class="form-group">
  24. <input class="form-control" placeholder="Password" name="password" type="password">
  25. </div>
  26. <button type="submit" class="btn btn-lg btn-primary btn-block"><span class="glyphicon glyphicon-log-in"></span> Login</button>
  27. </fieldset>
  28. </form>
  29. </div>
  30. </div>
  31. </div>
  32. </div>
  33. <div class="row">
  34. <?php
  35. if (isset($_SESSION['error'])) {
  36. ?>
  37. <div class="col-md-4 col-md-offset-4">
  38. <div class="alert alert-success text-center"><?php echo $_SESSION['error']; ?></div>
  39. </div>
  40. <?php
  41. unset($_SESSION['error']);
  42. }
  43. ?>
  44. </div>
  45. </div>
  46. </body>
  47. </html>

login.php

This is our user login code.
  1. <?php
  2. include('conn.php');
  3.  
  4. function get_address() {
  5. if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  6. $address = $_SERVER['HTTP_CLIENT_IP'];
  7. } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  8. $address = $_SERVER['HTTP_X_FORWARDED_FOR'];
  9. } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
  10. $address = $_SERVER['HTTP_X_FORWARDED'];
  11. } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
  12. $address = $_SERVER['HTTP_FORWARDED_FOR'];
  13. } elseif (isset($_SERVER['HTTP_FORWARDED'])) {
  14. $address = $_SERVER['HTTP_FORWARDED'];
  15. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  16. $address = $_SERVER['REMOTE_ADDR'];
  17. } else {
  18. $address = 'UNKNOWN';
  19. }
  20. return $address;
  21. }
  22.  
  23. $ip = get_address();
  24.  
  25. $username=$_POST['username'];
  26. $password=$_POST['password'];
  27.  
  28. $query = $conn->query("select * from user where username='$username' and password='$password'");
  29.  
  30. if($query->num_rows > 0){
  31. $row = $query->fetch_array();
  32. $_SESSION['user'] = $row['userid'];
  33. $conn->query("insert into login (userid, ip_address, login_date) values ('".$row['userid']."', '$ip', NOW())");
  34. header('location:home.php');
  35. }
  36. else{
  37. $_SESSION['error']="Login Failed. User not Found.";
  38. header('location:index.php');
  39. }
  40. ?>

home.php

This is our go to page after a successful login. This also contains our login table where we can see the IP address of the user that logged in.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>PHP Getting IP Address upon Login</title>
  5. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  8. </head>
  9. <body>
  10. <div class="container">
  11. <h1 class="page-header text-center">PHP Getting IP Address upon Login</h1>
  12. <div class="row">
  13. <div class="col-md-8 col-md-offset-2">
  14. <h2>Login Table
  15. <a href="logout.php" class="btn btn-danger pull-right"><span class="glyphicon glyphicon-log-out"></span> Logout</a>
  16. </h2>
  17. <table class="table table-bordered table-striped">
  18. <thead>
  19. <th>User</th>
  20. <th>Ip Address</th>
  21. <th>Date</th>
  22. </thead>
  23. <tbody>
  24. <?php
  25. include('conn.php');
  26. $query=$conn->query("select * from login left join user on user.userid=login.userid");
  27. while($row=$query->fetch_array()){
  28. ?>
  29. <tr>
  30. <td><?php echo $row['name']; ?></td>
  31. <td><?php echo $row['ip_address']; ?></td>
  32. <td><?php echo date('M d, Y h:i A',strtotime($row['login_date'])); ?></td>
  33. </tr>
  34. <?php
  35. }
  36. ?>
  37. </tbody>
  38. </table>
  39. </div>
  40. </div>
  41. </div>
  42. </body>
  43. </html>

logout.php

Lastly, this is our logout to destroy our session.
  1. <?php
  2. header('location:index.php');
  3. ?>
That ends this tutorial. If you have any comment, question or suggestion, feel free to write it below or message me. Happy Coding :)

Add new comment