PHP Getting IP Address upon Login
Submitted by nurhodelta_17 on Thursday, November 16, 2017 - 20:07.
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.- CREATE TABLE `login` (
- `loginid` INT(11) NOT NULL AUTO_INCREMENT,
- `userid` INT(11) NOT NULL,
- `ip_address` VARCHAR(50) NOT NULL,
- `login_date` datetime NOT NULL,
- PRIMARY KEY(`loginid`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
- CREATE TABLE `user` (
- `userid` INT(11) NOT NULL AUTO_INCREMENT,
- `username` VARCHAR(30) NOT NULL,
- `password` VARCHAR(30) NOT NULL,
- `name` VARCHAR(50) NOT NULL,
- PRIMARY KEY(`userid`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
- INSERT INTO `user` (`userid`, `username`, `password`, `name`) VALUES
- (1, 'nurhodelta', 'devierte', 'neovic');
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.- <?php
- $conn = new mysqli("localhost", "root", "", "ip");
- if ($conn->connect_error) {
- }
- ?>
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.- <!DOCTYPE html>
- <html>
- <head>
- <title>PHP Getting IP Address upon Login</title>
- <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container">
- <h1 class="page-header text-center">PHP Getting IP Address upon Login</h1>
- <div class="row">
- <div class="col-md-4 col-md-offset-4">
- <div class="login-panel panel panel-primary">
- <div class="panel-heading">
- <h3 class="panel-title"><span class="glyphicon glyphicon-lock"></span> Sign In</h3>
- </div>
- <div class="panel-body">
- <form method="POST" action="login.php">
- <fieldset>
- <div class="form-group">
- <input class="form-control" placeholder="Username" name="username" type="text" autofocus>
- </div>
- <div class="form-group">
- <input class="form-control" placeholder="Password" name="password" type="password">
- </div>
- <button type="submit" class="btn btn-lg btn-primary btn-block"><span class="glyphicon glyphicon-log-in"></span> Login</button>
- </fieldset>
- </form>
- </div>
- </div>
- </div>
- </div>
- <div class="row">
- <?php
- ?>
- <div class="col-md-4 col-md-offset-4">
- <div class="alert alert-success text-center"><?php echo $_SESSION['error']; ?></div>
- </div>
- <?php
- }
- ?>
- </div>
- </div>
- </body>
- </html>
login.php
This is our user login code.- <?php
- include('conn.php');
- function get_address() {
- $address = $_SERVER['HTTP_CLIENT_IP'];
- $address = $_SERVER['HTTP_X_FORWARDED_FOR'];
- $address = $_SERVER['HTTP_X_FORWARDED'];
- $address = $_SERVER['HTTP_FORWARDED_FOR'];
- $address = $_SERVER['HTTP_FORWARDED'];
- $address = $_SERVER['REMOTE_ADDR'];
- } else {
- $address = 'UNKNOWN';
- }
- return $address;
- }
- $ip = get_address();
- $username=$_POST['username'];
- $password=$_POST['password'];
- $query = $conn->query("select * from user where username='$username' and password='$password'");
- if($query->num_rows > 0){
- $row = $query->fetch_array();
- $_SESSION['user'] = $row['userid'];
- $conn->query("insert into login (userid, ip_address, login_date) values ('".$row['userid']."', '$ip', NOW())");
- }
- else{
- $_SESSION['error']="Login Failed. User not Found.";
- }
- ?>
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.- <!DOCTYPE html>
- <html>
- <head>
- <title>PHP Getting IP Address upon Login</title>
- <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container">
- <h1 class="page-header text-center">PHP Getting IP Address upon Login</h1>
- <div class="row">
- <div class="col-md-8 col-md-offset-2">
- <h2>Login Table
- <a href="logout.php" class="btn btn-danger pull-right"><span class="glyphicon glyphicon-log-out"></span> Logout</a>
- </h2>
- <table class="table table-bordered table-striped">
- <thead>
- <th>User</th>
- <th>Ip Address</th>
- <th>Date</th>
- </thead>
- <tbody>
- <?php
- include('conn.php');
- $query=$conn->query("select * from login left join user on user.userid=login.userid");
- while($row=$query->fetch_array()){
- ?>
- <tr>
- <td><?php echo $row['name']; ?></td>
- <td><?php echo $row['ip_address']; ?></td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- </div>
- </div>
- </div>
- </body>
- </html>
logout.php
Lastly, this is our logout to destroy our session.- <?php
- ?>
Add new comment
- 491 views