6

Using just PHP, is there anyway to detect if the page is being loaded from a SPECIFIC device? Say, I would want the page to react differently if only MY iPhone was loading the page, no one else's?

The only solution I have so far is to use $_SERVER['REMOTE_PORT' in conjunction with $_SERVER['HTTP_USER_AGENT'], to verify iPhone, but that doesn't ensure a specific iPhone..

Chud37
  • 4,907
  • 13
  • 64
  • 116
  • `$_SERVER['REMOTE_PORT']` doesn't provide any information about the device. It is just a (more or less random) number. – axiac Feb 15 '18 at 09:53

6 Answers6

4

There is a really simple solution to use Mobile-detect here: https://github.com/serbanghita/Mobile-Detect/

Enjoy!

JeanR
  • 92
  • 11
3

$_SERVER['HTTP_USER_AGENT'] contains information about the browser and the device used. So if you know the User Agent - sent by your device - then you can quite easily write an if statement, that will see if it's the one you want or not.

However, usually you don't really want to play with manual aiming at the devices, especially when it comes down to mobiles. Think about using something like wurfl, a class that allows you to determine a type of device that loaded your webpage.

If you want to aim at specific iPhone you'd most likely want to compare it's user agent with user agent of another model. But as far as I know - it's very flawed method, and doesn't really work in a long term. So long answer short: There's no way to aim at very one specific iPhone model (because any of them sends roughly identical data to the server if they all got same iOS and same browser).

MarcinWolny
  • 1,600
  • 2
  • 27
  • 40
1

hi I am a noob at PHP but I came up with this code to solve my issue


<?php
require_once './likebutton/app/init.php';
$new_key= uniqid().'_'.gethostbyaddr($_SERVER["REMOTE_ADDR"]);
$md_key= md5($new_key);

if(!isset($_COOKIE['visitor_id']))
{
   $check= $mysqli -> prepare("SELECT * FROM `unique_id_table` WHERE `unique_id`=?");
   $check -> bind_param("s",$md_key);
   $check -> execute();
   $check_result= $check -> get_result();
   if($check_result -> num_rows > 0){

   }
   else{
       $insert= $mysqli -> prepare("INSERT INTO `unique_id_table`(`unique_id`)  VALUES (?)");
       $insert -> bind_param('s',$md_key);
       $insert -> execute();
       setcookie('visitor_id',$md_key,time() + (86400*30), "/");

   }

}
if(isset($_COOKIE['visitor_id'])) {
   echo "your id'" . $_COOKIE['visitor_id'] ;

   
} else {
   echo "please refresh ";
}



Iam storing in the cookie storage a unique id generated randomly via uniqid() function then storing the id in my table to later identify the visitor returning if their value is in my table .Hope this helps :)

Evans Mboya
  • 111
  • 1
  • 3
0

Only if that device sends such information.

You could use a cookie - have a setup page that requires you to log in, then send the cookie to the device. From then onward your device will send the cookie to other pages without needing to log in again.

By default devices do not send serial numbers or any other means of specifically identifying a device, probably because this would break some privacy rules.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • Argh - I need a QR Code reader to go to a certain PHP session page but only from a certain phone - Otherwise anyone could scan the QR Code and then log in. I need to restrict it. The only true solution so far is get someone to develop an App to do it - which is going to cost £1500. :( – Chud37 Feb 01 '13 at 13:09
0

By using Mobile_Detect PHP class you can detect your mobile devices. It uses the user-agent string combined with specific HTTP headers to detect the mobile environment.

laxonline
  • 2,657
  • 1
  • 20
  • 37
0

Using the $_SERVER['HTTP_USER_AGENT'] is not a solution, if there is an update on the device, that will change. So I will use cookies for it.

My plan is to generate a device id like this:

$device_id = md5($_SERVER['HTTP_USER_AGENT']);

Then push it into the database, and into the cookies. and I will check it in the begining of the session, just to update them. So I can detect if this id is changed, so I can always have the new ID in the cookies and in the database too, cookies will be always fresh - depending how the HUA will change on the device.

vimuth
  • 5,064
  • 33
  • 79
  • 116