dongxie9448 2017-07-14 03:12
浏览 44

如何在PHP中获取有关用户设备的信息并将其放入数据库? [重复]

This question already has an answer here:

Im creating a script that puts the users device information and their IP into a database here is the script I am using. I keep getting this big amount of info for the user agent and I want to just make it insert in the database saying "iPhone" "Android" "Computer" instead of all that complicated stuff, how would i do that? UPDATE: Oh and the affsub is the tracking id.

<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "db";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
date_default_timezone_set('America/New_York');

$affsub     =            $_GET['affsub'];
$userip     =            $_SERVER['REMOTE_ADDR'];
$userdevice =            $_SERVER['HTTP_USER_AGENT'];
$date       =            date('Y-m-d');
$time       =            date('H:i:s');

$sql = "INSERT INTO clicks (affsub, userip, userdevice, date, time)
VALUES ('$affsub', '$userip', '$userdevice', '$date', '$time')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);

header("Location: linkgoeshere"); /* Redirect browser */
exit();
?>

THIS IS NOT A DUPLICATE OF THE IP ONE I am trying to get the users device NOT their IP, I can already do that perfectly fine.

</div>
  • 写回答

1条回答 默认 最新

  • doudang9147 2017-07-14 03:46
    关注

    $_SERVER['HTTP_USER_AGENT'], It is correct for getting user device.

    You should check below.

    1.

    echo($_SERVER['HTTP_USER_AGENT']);
    

    I tried this code on safari of my mac and iPhone.

    On mac, it is printed this.

    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"

    On iPhone, it is printed this

    Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1

    Conclude, it is correct on mine enviroment.



    2.

    Check "userdevice" field on the table.

    Is length of the field enough? $_SERVER['HTTP_USER_AGENT'] is printed a long string.


    In any case, I suggest this.

    $userdevice = "";
    
    $mobileAgent = array("iPhone","iPod","Android","Blackberry",
        "Opera Mini", "Windows ce", "Nokia", "sony" );
    for($i=0; $i<sizeof($mobileAgent); $i++){
        if(stripos( $_SERVER['HTTP_USER_AGENT'], $mobileAgent[$i] )){
            $userdevice = $mobileAgent[$i];
            break;
        }
    }
    
    echo $userdevice;
    

    This can get a just device name.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)