5 Replies - 284 Views - Last Post: 29 January 2013 - 06:38 PM Rate Topic: -----

#1 ceptic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-January 13

Problem parsing values

Posted 29 January 2013 - 04:42 PM

i have a small problem with my script i took a abit of source code from php.net

its to record the users browser and os

so i can record what browsers are mainly being used.

now what im wanting to do is parse the data into a database so i can pull the details and show it in a nice little pretty graph but i have a problem... the data isnt going into the database its just posting blank values...

now ive only modified the bottom section where its asking for a db connection and the mysql query



<?php
function getBrowser() 
{ 
    $u_agent = $_SERVER['HTTP_USER_AGENT']; 
    $bname = 'Unknown';
    $platform = 'Unknown';
    $version= "";

    //First get the platform?
    if (preg_match('/linux/i', $u_agent)) {
        $platform = 'linux';
    }
    elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
        $platform = 'mac';
    }
    elseif (preg_match('/windows|win32/i', $u_agent)) {
        $platform = 'windows';
    }
    
    // Next get the name of the useragent yes seperately and for good reason
    if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) 
    { 
        $bname = 'Internet Explorer'; 
        $ub = "MSIE"; 
    } 
    elseif(preg_match('/Firefox/i',$u_agent)) 
    { 
        $bname = 'Mozilla Firefox'; 
        $ub = "Firefox"; 
    } 
    elseif(preg_match('/Chrome/i',$u_agent)) 
    { 
        $bname = 'Google Chrome'; 
        $ub = "Chrome"; 
    } 
    elseif(preg_match('/Safari/i',$u_agent)) 
    { 
        $bname = 'Apple Safari'; 
        $ub = "Safari"; 
    } 
    elseif(preg_match('/Opera/i',$u_agent)) 
    { 
        $bname = 'Opera'; 
        $ub = "Opera"; 
    } 
    elseif(preg_match('/Netscape/i',$u_agent)) 
    { 
        $bname = 'Netscape'; 
        $ub = "Netscape"; 
    } 
    
    // finally get the correct version number
    $known = array('Version', $ub, 'other');
    $pattern = '#(?<browser>' . join('|', $known) .
    ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    if (!preg_match_all($pattern, $u_agent, $matches)) {
        // we have no matching number just continue
    }
    
    // see how many we have
    $i = count($matches['browser']);
    if ($i != 1) {
        //we will have two since we are not using 'other' argument yet
        //see if version is before or after the name
        if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
            $version= $matches['version'][0];
        }
        else {
            $version= $matches['version'][1];
        }
    }
    else {
        $version= $matches['version'][0];
    }
    
    // check if we have a number
    if ($version==null || $version=="") {$version="?";}
    
    return array(
        'userAgent' => $u_agent,
        'name'      => $bname,
        'version'   => $version,
        'platform'  => $platform,
        'pattern'    => $pattern
    );
} 

// now try it
$ua=getBrowser();
$yourbrowser= "Your browser: " . $ua['name'] . " " . $ua['version'] . " on " .$ua['platform'] . " reports: <br >" . $ua['userAgent'];



include ("./db_connect.php");

mysql_query("INSERT INTO browser (useragent,browsername,version,platform,pattern)
VALUES ('$u_agent','$bname','$version','$platform','$pattern')");

print_r($yourbrowser);

?>




if you can help that would be brilliant



thanks in advance

ceptic

Is This A Good Question/Topic? 0
  • +

Replies To: Problem parsing values

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3253
  • View blog
  • Posts: 10,908
  • Joined: 12-December 12

Re: Problem parsing values

Posted 29 January 2013 - 05:01 PM

You have this line at the bottom:

print_r($yourbrowser);

Does it print out the correct data? View the source of your page to see it more clearly.

The data that you want to input to your database (as displayed with $yourbrowser) is contained within the $ua array. So, this:

VALUES ('$u_agent','$bname','$version','$platform','$pattern')");

should refer to
VALUES ('{$ua['userAgent']}', etc..

I'm surprised you didn't receive any errors; have you turned on error reporting fully?

error_reporting(E_ALL);
ini_set('display_errors', '1');

This post has been edited by andrewsw: 29 January 2013 - 05:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3253
  • View blog
  • Posts: 10,908
  • Joined: 12-December 12

Re: Problem parsing values

Posted 29 January 2013 - 05:08 PM

That custom function was written 2 years ago and may require a little updating.

Personally, I would use the PHP built-in get_browser() function firstly - it may have been improved since the guy wrote his custom version. If the official function doesn't behave then, by all means, I would look at incorporating that custom version.
Was This Post Helpful? 0
  • +
  • -

#4 ceptic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-January 13

Re: Problem parsing values

Posted 29 January 2013 - 05:25 PM

thanks for the replies guys, ive tried your updated version of the code which i used initialy but i copied and pasted and just changed the name of the value its calling, im afraid to say this isnt parsing the information to the database either here is updated code.


i have trye the php standard method but for some reason it wasnt picking up chrome hence why im using this modified version :)/>, thanks though

ps. php error loggin is on no errors displayed info displays perfectly

http://www.jt-repair...min/example.php

^^/> as you can see from the link above

which makes me wonder what the hell is going on here :S


<?php
function getBrowser() 
{ 
    $u_agent = $_SERVER['HTTP_USER_AGENT']; 
    $bname = 'Unknown';
    $platform = 'Unknown';
    $version= "";

    //First get the platform?
    if (preg_match('/linux/i', $u_agent)) {
        $platform = 'linux';
    }
    elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
        $platform = 'mac';
    }
    elseif (preg_match('/windows|win32/i', $u_agent)) {
        $platform = 'windows';
    }
    
    // Next get the name of the useragent yes seperately and for good reason
    if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) 
    { 
        $bname = 'Internet Explorer'; 
        $ub = "MSIE"; 
    } 
    elseif(preg_match('/Firefox/i',$u_agent)) 
    { 
        $bname = 'Mozilla Firefox'; 
        $ub = "Firefox"; 
    } 
    elseif(preg_match('/Chrome/i',$u_agent)) 
    { 
        $bname = 'Google Chrome'; 
        $ub = "Chrome"; 
    } 
    elseif(preg_match('/Safari/i',$u_agent)) 
    { 
        $bname = 'Apple Safari'; 
        $ub = "Safari"; 
    } 
    elseif(preg_match('/Opera/i',$u_agent)) 
    { 
        $bname = 'Opera'; 
        $ub = "Opera"; 
    } 
    elseif(preg_match('/Netscape/i',$u_agent)) 
    { 
        $bname = 'Netscape'; 
        $ub = "Netscape"; 
    } 
    
    // finally get the correct version number
    $known = array('Version', $ub, 'other');
    $pattern = '#(?<browser>' . join('|', $known) .
    ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    if (!preg_match_all($pattern, $u_agent, $matches)) {
        // we have no matching number just continue
    }
    
    // see how many we have
    $i = count($matches['browser']);
    if ($i != 1) {
        //we will have two since we are not using 'other' argument yet
        //see if version is before or after the name
        if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
            $version= $matches['version'][0];
        }
        else {
            $version= $matches['version'][1];
        }
    }
    else {
        $version= $matches['version'][0];
    }
    
    // check if we have a number
    if ($version==null || $version=="") {$version="?";}
    
    return array(
        'userAgent' => $u_agent,
        'name'      => $bname,
        'version'   => $version,
        'platform'  => $platform,
        'pattern'    => $pattern
    );
} 

// now try it
$ua=getBrowser();
$yourbrowser= "Your browser: " . $ua['name'] . " " . $ua['version'] . " on " .$ua['platform'] . " reports: <br >" . $ua['userAgent'];



include ("./db_connect.php");

mysql_query("INSERT INTO browser (useragent,browsername,version,platform,pattern)
VALUES ('('{$ua['userAgent']}','{$ua['name']}','{$ua['version']}','{$ua['platform']}','{$ua['pattern']}')");

print_r($yourbrowser);






?>

This post has been edited by Dormilich: 30 January 2013 - 09:56 AM
Reason for edit:: removed quote

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3253
  • View blog
  • Posts: 10,908
  • Joined: 12-December 12

Re: Problem parsing values

Posted 29 January 2013 - 05:59 PM

Your SQL statement has an extra bracket and comma (' so, again, I'm surprised you have no errors reported.

What was the output of print_r?

ALSO
The mysql library is deprecated - use mysqli or PDO instead.
Use prepared statements.

This post has been edited by andrewsw: 29 January 2013 - 06:02 PM

Was This Post Helpful? 1
  • +
  • -

#6 ceptic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-January 13

Re: Problem parsing values

Posted 29 January 2013 - 06:38 PM

thanks i didnt notice the exta ( and ', that was the problem its never good to code on tired eyes haha,

thanks very much for your help.

This post has been edited by Dormilich: 30 January 2013 - 09:55 AM
Reason for edit:: removed quote

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1