Get Unique web browser ID in PHP

2019-02-11 00:58发布

问题:

I am looking a way to store unique ID of web browsers in PHP? I searched everywhere..but couldn't find any useful things. First of all, does web browsers have a unique ID or something like that to identify them??? Like the computers have a unique IP address, does browsers have anything like that?? I am trying this so that I can store user details into my database. So, we can find out a user used which browser to access my PHP website. Can I store such an ID to a php variable?

for example, $x= ID of the web browser.

回答1:

When a user visits your site you know his IP, browser, OS and other stuff. You need to know if the next time the same user visits you (same IP, same login perhaps) is using the same browser. This can be achieved in two ways:

a) Create a cookie, store it in the browser and in your DB, then compare every visitor that has that cookie against your existing cookie table to identify him. If the cookie has a very long expiration time it won't be lost across sessions, and will be unique to each browser. But it will be lost if the user clears cookies.

b) Store a string in the browser's local storage. This works pretty much the same as the cookie method but there's no expiration date for the data, and it's thougher to remove it than to clear cookies.

There are other methods, like combining different kind of cookies to provide redundancy. I remember a project called Evercookie that did something like that. I believe it's pretty much abandoned now.



回答2:

You can use the browscap library and then make an md5 of the browser information:

Uses GaretJax/phpbrowscap

$bc = new \phpbrowscap\Browscap('data/');
$current_browser = $bc->getBrowser(null,true);
$str = json_encode($current_browser);
$browser_id = md5($str);