Saturday 15 September 2018

Codeigniter - Detect User IP Browser Operating System Details



If you want to get or fetch or collect user information like who has visit your website, so you can do this things very easily if you have use Codeigniter. By using Codeigniter we can easily collect user information like user IP Address, browser details, browser version, operating system which he has use in his computer, we can also detect from which device he has get access our website etc. We can fetch all these user information by using Codeigniter "user_agent" library.

User Agent Library in Codeigniter is one class which help programmer to detect user information about user browser, mobile device or even robot visiting to website also. If you have use simple PHP script from get all these information then it is very difficult but in Codeigniter you have simple load this user_agent library and simple way to fetch all user information. Because we want know who has visit our website, from which region or location our website has been open. So, If any one has do any fraud on our website then we can easily detect particular user based on his information. So, it is required to get such type of user information and this task we can easily do by using Codeigniter User Agent Library.









Source Code


Controllers


application/controllers/UserDetails.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class UserDetails extends CI_Controller {
 
 function index()
 {
  $this->load->library('user_agent');
  $data['browser'] = $this->agent->browser();
  $data['browser_version'] = $this->agent->version();
  $data['os'] = $this->agent->platform();
  $data['ip_address'] = $this->input->ip_address();
  $this->load->view('user_details', $data);
 }
 
}
?>


Views



<!DOCTYPE html>
<html>
<head>
 <title>How to Get User IP, Browser & OS Details in Codeigniter</title>
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
</head>

<body>
 <div class="container">
  <br />
  <h3 align="center">How to Get User IP, Browser & OS Details in Codeigniter</h3>
  <br />
  <div class="table-responsive">
   <table class="table table-bordered table-striped">
    <tr>
     <td><b>IP Address</b></td>
     <td><?php echo $ip_address; ?></td>
    </tr>
    <tr>
     <td><b>Operating System</b></td>
     <td><?php echo $os; ?></td>
    </tr>
    <tr>
     <td><b>Browser Details</b></td>
     <td><?php echo $browser . ' - ' . $browser_version; ?></td>
    </tr>
   </table>
  </div>
 </div>
</body>
</html>

3 comments: