I'm trying to figure out the visitor's OS is either a Windows, Mac or Linux using PHP(I don't need the version, distro info.. etc). There's several methods out there however they look a bit too complicated for this simple requirement.
Are there any simple ways that could provide this sort of information yet still being quite reliable?
Thanks in advance.
<?php
$agent = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('/Linux/',$agent)) $os = 'Linux';
elseif(preg_match('/Win/',$agent)) $os = 'Windows';
elseif(preg_match('/Mac/',$agent)) $os = 'Mac';
else $os = 'UnKnown';
echo $os;
?>
For an easy solution have a look here.
The user-agent header might reveal some OS information, but i wouldn't count on that.
For your use case i would do an ajax call using javascript from the client side to inform your server of the client's OS. And do it waterproof.
Here is an example.
Javascript (client side, browser detection + ajax call ):
window.addEvent('domready', function() {
if (BrowserDetect) {
var q_data = 'ajax=true&browser=' + BrowserDetect.browser + '&version=' + BrowserDetect.version + '&os=' + BrowserDetect.OS;
var query = 'record_browser.php'
var req = new Request.JSON({url: query, onComplete: setSelectWithJSON, data: q_data}).post();
}
});
PHP (server side):
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$session = session_id();
$user_id = isset($user_id) ? $user_id : 0;
$browser = isset($_POST['browser']) ? $_POST['browser'] : '';
$version = isset($_POST['version']) ? $_POST['version'] : '';
$os = isset($_POST['os']) ? $_POST['os'] : '';
// now do here whatever you like with this information
}
use the Net_UserAgent package
docu is here: http://pear.php.net/package/Net_UserAgent_Detect/docs/latest/Net_UserAgent/Net_UserAgent_Detect.html#methodgetOSString
get the php file here:
package/Net_UserAgent_Detect/docs/latest/__filesource/fsource_Net_UserAgent__Net_UserAgent_Detect-2.5.1Detect.php.html
Related
Google Geocode use to work for me but does not anymore. Everything I've tried returns false. It doesn't even give a clue as to why. I've tried changing the API key, I've tried different methods to connect (I prefer using simplexml_load_file). It does work if I type the URL directly into the a browser window but not from my website or even from my localhost. Does anyone have suggestions on what to try next or an alternative to Googles Geocode service?
Here is some code that I've been using that Json / XML results.
//URL SAVE ADDRESS
date_default_timezone_set("America/Chicago");
$address = "1600 Amphitheatre Parkway, Mountain View, CA 94043";
$address = urlencode($address);
// GOOGLE URL
$url = "https://maps.googleapis.com/maps/api/geocode/xml?address=". $address ."&sensor=false&key=AIzaSyDrGe_qr4ofea8WhZFhnPGsXNQtTiQwGhw";
echo $url; //TEMP
$xml = simplexml_load_file($url) OR $ex['tblx'] = "Unable to load XML from Googleapis.";
if(empty($xml)) {
$ex['tblx'] = "Googleapis return no values.";
} elseif("" == $xml) {
$ex['tblx'] = "Geocode problem with address, revise and retry.";
} elseif("OK" != $xml->status) {
$ex['tblx'] = "Geocode: " . ("ZERO_RESULTS" == $xml->status? "Not Found": substr($xml->status,0,30));
} else{
$dvtby0 = $uca['y0'] = (double)$xml->result->geometry->location->lat; //x
$dvtbx0 = $uca['x0'] = (double)$xml->result->geometry->location->lng;
$dvtby1 = $uca['y1'] = (double)$xml->result->geometry->viewport->southwest->lat; //a
$dvtbx2 = $uca['x1'] = (double)$xml->result->geometry->viewport->southwest->lng;
$dvtbx1 = $uca['x2'] = (double)$xml->result->geometry->viewport->northeast->lng; //b
$dvtby2 = $uca['y2'] = (double)$xml->result->geometry->viewport->northeast->lat;
}//endif
if($ex) {
var_dump($ex);
}
die("did it work?");
geocoder.ca for north america
opencage or geocode.xyz for the world
pelias as standalone
there are lots of options in 2018
The problem here turned out to be the version of PHP. It's really weird and hard to troubleshoot. simplexml_load_file($url) was not returning an object. It wasn't even returning false. The only clue that it was something other that code problem. Upgrade to version 5.6 and it works ok.
The code I am using is:
$url = "example.com";
$code = file_get_contents("http://www.".$url);
if (!$code) {
$code = file_get_contents("https://www.".$url);
}
I don't know whether each URL starts with http or https. I have 1,000 URLs saved in my database. Any suggestions?
This answer is somewhat relevant. Given that your database is rather incomplete in that the URLs aren't fully qualified, I would be inclined to write a short routine to update the database for future use.
<?php
$url = 'example.com';
$variations[] = 'http://'.$url;
$variations[] = 'https://'.$url;
$variations[] = 'http://www.'.$url;
$variations[] = 'https://www'.$url;
foreach($variations as $v){
// Check variation with function from answer linked above
if(urlOK($v)){
// code to update database row
// or if you don't want to do that
$code = file_get_contents($v);
}
}
Does anyone know how to check in php if a browser has webgl or not and display a true or false value? I know if I use:
$_SERVER['HTTP_USER_AGENT'] . "\n\n";
$browser = get_browser(null, true);
print_r($browser);
http://php.net/manual/en/function.get-browser.php
displays all the attributes/features of a browser, but does it do webgl? Please advise.
Thanks
You can use javascript to check it for you, then you can make it send the value to php.
JavaScript:
if (Modernizr.webgl) {
var webgl = "True";
window.location.href = "myphpfile.php?webgl =" + webgl ;
} else {
var webgl = "False";
window.location.href = "myphpfile.php?webgl =" + webgl ;
}
PHP:
$value = $_GET['name'];
I am trying to see if JQuery CDN exists or not via PHP
essentially what Paul Irish did here but with PHP instead.
http://paulirish.com/2010/the-protocol-relative-url/
I am trying following but it's not working. Is this possible without http/https?
This is based on How can I check if a URL exists via PHP?
$jquery_cur = '1.9.1'; // JQuery Version
$jquery_cdn = '//code.jquery.com/jquery-'.$jquery_cur.'.min.js';
$jquery_local = '/assets/js/libs/jquery-'.$jquery_cur.'.min.js';
$jquery_ver = $jquery_cdn; //Load the Jquery CDN version by default
$cdn_headers = #get_headers($jquery_ver);
if(strpos($cdn_headers[0], '404 Not Found')) {
$jquery_ver = $jquery_cdn;
}
else {
$jquery_ver = $jquery_local;
}
Hi check this solution you were check header without any protocol you need to add http or https to check files. test it with or without your internet connection.
$jquery_cur = '1.9.1'; // JQuery Version
$jquery_cdn = '//code.jquery.com/jquery-'.$jquery_cur.'.min.js';
$jquery_local = '/assets/js/libs/jquery-'.$jquery_cur.'.min.js';
$jquery_ver = $jquery_cdn; //Load the Jquery CDN version by default
$jquery_url = ( $_SERVER['SERVER_PORT'] == 443 ? "https:" : "http:").$jquery_cdn;
$test_url = #fopen($jquery_url,'r');
if($test_url === False) {
$jquery_ver = $jquery_local;
}
echo $jquery_ver;
with get_header
$headers = #implode('', #get_headers($jquery_url));
$test_url = (bool)preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers);
if($test_url === False) {
$jquery_ver = $jquery_local;
}
echo $jquery_ver;
I am using javascript to connect websocket:
<script>
var socket;
var host = "ws://localhost:8000/socket/server/startDaemon.php";
var socket = new WebSocket(host);
</script>
I got the error:
Can't establish a connection to the server at
var host = "ws://localhost:8000/socket/server/startDaemon.php";
var socket = new WebSocket(host);
How can I solve this issue?
NOTE : I enabled websocket in mozilla to support web socket application.
and when i run in chrome i got error:
can't establish a connection to the server at ws://localhost:8000/socket/server/startDaemon.php. var socket = new WebSocket(host);
Apparently firefox 4 has websockets disabled because of vulnerabilities. To quote From this article:
WebSocket disabled in Firefox 4
Recent discoveries found that the protocol that Websocket works with is vulnerable to attacks. Adam Barth demonstrated some serious attacks against the protocol that could be used by an attacker to poison caches that sit in between the browser and the Internet.
I solved my error by following code through this link
http://www.flynsarmy.com/2010/05/php-web-socket-chat-application/
and created socketWebSocketTrigger.class.php file for response message where code as
class socketWebSocketTrigger
{
function responseMessage($param)
{
$a = 'Unknown parameter';
if($param == 'age'){
$a = "Oh dear, I'm 152";
}
if($param == 'hello'){
$a = 'hello, how are you?';
}
if($param == 'name'){
$a = 'my name is Mr. websocket';
}
if($param == 'today'){
$a = date('Y-m-d');
}
if($param == 'hi'){
$a = 'hi there';
}
return $a;
}
}
and added code in send function of 'WebSocketServer.php' for calling 'responseMessage' function which response request message
public function send($client, $msg){
$this->say("> ".$msg);
$messageRequest = json_decode($msg,true);
// $action=$messageRequest[0];
$action = 'responseMessage';
$param = $messageRequest[1]['data'];
if( method_exists('socketWebSocketTrigger',$action) ){
$response = socketWebSocketTrigger::$action($param);
}
$msg = json_encode(
array(
'message',
array('data' => $response)
)
);
$msg = $this->wrap($msg);
socket_write($client, $msg, strlen($msg));
}
it's working great.
Are you trying to run the client in Firefox? According to the documentation:
As of Feb/10 the only browsers that
support websockets are Google Chrome
and Webkit Nightlies. Get it from here
http://www.google.com/chrome
Try running it in Chrome and see if that works for you.
First of all your mistake is using php function with javascript require_once 'WebSocket.php'; and secondly go through the tutorial as in the link below.
http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/
it's working fine.
Thanks,