adam87 Posted March 25, 2009 Share Posted March 25, 2009 Hey I'm currently working on a project an have been asked to store which browser the user is using. I was wondering if any one can point me in the way of a tutorial. I have searched but came back with nothing Quote Link to comment https://forums.phpfreaks.com/topic/151110-users-browser/ Share on other sites More sharing options...
lonewolf217 Posted March 25, 2009 Share Posted March 25, 2009 http://www.google.com/search?source=ig&hl=en&rlz=&=&q=php+detect+browser&btnG=Google+Search&aq=f Quote Link to comment https://forums.phpfreaks.com/topic/151110-users-browser/#findComment-793829 Share on other sites More sharing options...
wildteen88 Posted March 25, 2009 Share Posted March 25, 2009 You could use the variable, $_SERVER['HTTP_USER_AGENT'] . Which returns the webbrowsers version and OS the client is using. Quote Link to comment https://forums.phpfreaks.com/topic/151110-users-browser/#findComment-793836 Share on other sites More sharing options...
Maq Posted March 25, 2009 Share Posted March 25, 2009 An example straight from the manual: function get_user_browser() { $u_agent = $_SERVER['HTTP_USER_AGENT']; $ub = ''; if(preg_match('/MSIE/i',$u_agent)) { $ub = "ie"; } elseif(preg_match('/Firefox/i',$u_agent)) { $ub = "firefox"; } elseif(preg_match('/Safari/i',$u_agent)) { $ub = "safari"; } elseif(preg_match('/Chrome/i',$u_agent)) { $ub = "chrome"; } elseif(preg_match('/Flock/i',$u_agent)) { $ub = "flock"; } elseif(preg_match('/Opera/i',$u_agent)) { $ub = "opera"; } return $ub; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151110-users-browser/#findComment-793846 Share on other sites More sharing options...
adam87 Posted March 25, 2009 Author Share Posted March 25, 2009 ah awesome guys, im off out so aint got a chance to proper look at them, but they look like the type of thing im looking for. I will let ya know how i get along! thanks Quote Link to comment https://forums.phpfreaks.com/topic/151110-users-browser/#findComment-793859 Share on other sites More sharing options...
adam87 Posted March 30, 2009 Author Share Posted March 30, 2009 Instead of starting a new topic, basically I had this working and storing it to mysql satabase, but working on it today and some how managed to stuff it up! <? include 'dbc.php'; $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']); // Identify the browser. Check Opera and Safari first in case of spoof. Let Google Chrome be identified as Safari. if (preg_match('/opera/', $userAgent)) { $name = 'opera'; } elseif (preg_match('/webkit/', $userAgent)) { $name = 'safari'; } elseif (preg_match('/msie/', $userAgent)) { $name = 'ie'; } elseif (preg_match('/mozilla/', $userAgent) && !preg_match('/compatible/', $userAgent)) { $name = 'firefox'; } else { $name = 'unrecognized'; } $query = "INSERT INTO browser (browser) VALUES ('$name')"; mysql_query ($query, $linkme) or die ("Could not add the data to table"); ?> It saying "Could not add the data to table", but I had it previously working and cannot remember what I changed! Quote Link to comment https://forums.phpfreaks.com/topic/151110-users-browser/#findComment-797127 Share on other sites More sharing options...
Maq Posted March 30, 2009 Share Posted March 30, 2009 Change this line to (it will give a more descriptive error message) : mysql_query ($query, $linkme) or die (mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/151110-users-browser/#findComment-797130 Share on other sites More sharing options...
adam87 Posted March 30, 2009 Author Share Posted March 30, 2009 Right got it to work, basically was a problem with mysql table, but now I have run in to another error, basically I've got a a session which is started when the user as logged in, this get the users_id. bascially i want to get that user Id and store it to my database, below is the code im trying: <? include 'dbc.php'; $user_id = $_REQUEST['uid']; $page = strtolower($_SERVER['PHP_SELF']); $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']); // Identify the browser. Check Opera and Safari first in case of spoof. Let Google Chrome be identified as Safari. if (preg_match('/opera/', $userAgent)) { $name = 'opera'; } elseif (preg_match('/webkit/', $userAgent)) { $name = 'safari'; } elseif (preg_match('/msie/', $userAgent)) { $name = 'ie'; } elseif (preg_match('/mozilla/', $userAgent) && !preg_match('/compatible/', $userAgent)) { $name = 'firefox'; } else { $name = 'unrecognized'; } $query = "INSERT INTO browser (browser, page_visted, user_id) VALUES ('$name', '$page', '$user_id')"; mysql_query ($query, $linkme) or die (mysql_error()); ?> uid is the name of my session that get the user’s ID, I’m using the following code to do this $_SESSION['uid'] = $assoc['id']; Quote Link to comment https://forums.phpfreaks.com/topic/151110-users-browser/#findComment-797165 Share on other sites More sharing options...
Maq Posted March 30, 2009 Share Posted March 30, 2009 uid is the name of my session that get the user’s ID, I’m using the following code to do this $_SESSION['uid'] = $assoc['id']; Where? I don't see any trace of sessions in your code. Couple things I noticed: 1) Don't use short tags () always use <?php 2) Make sure you put "session_start();" at the top of your page before you assign the id from the session. Quote Link to comment https://forums.phpfreaks.com/topic/151110-users-browser/#findComment-797183 Share on other sites More sharing options...
adam87 Posted March 30, 2009 Author Share Posted March 30, 2009 Right made the adjustments (cant believe i forgot the session tags) I am now not getting any error message but its not adding to the database. Below is the current code I have. <?php session_start(); include 'dbc.php'; $user_id = $_REQUEST['uid']; $page = strtolower($_SERVER['PHP_SELF']); $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']); // Identify the browser. Check Opera and Safari first in case of spoof. Let Google Chrome be identified as Safari. if (preg_match('/opera/', $userAgent)) { $name = 'opera'; } elseif (preg_match('/webkit/', $userAgent)) { $name = 'safari'; } elseif (preg_match('/msie/', $userAgent)) { $name = 'ie'; } elseif (preg_match('/mozilla/', $userAgent) && !preg_match('/compatible/', $userAgent)) { $name = 'firefox'; } else { $name = 'unrecognized'; } $query = "INSERT INTO browser (browser, page_visted, user_id) VALUES ('$name', '$page', '$user_id')"; mysql_query ($query, $linkme) or die (mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/151110-users-browser/#findComment-797197 Share on other sites More sharing options...
Maq Posted March 30, 2009 Share Posted March 30, 2009 Right made the adjustments (cant believe i forgot the session tags) I am now not getting any error message but its not adding to the database. Below is the current code I have. What does the error message say? uid is the name of my session that get the user’s ID, I’m using the following code to do this $_SESSION['uid'] = $assoc['id']; I'm confused... In the statement above you claimed you were using sessions, but in your code you don't use them. If you're referring to this line, that's not using sessions. $user_id = $_REQUEST['uid']; Quote Link to comment https://forums.phpfreaks.com/topic/151110-users-browser/#findComment-797209 Share on other sites More sharing options...
adam87 Posted March 30, 2009 Author Share Posted March 30, 2009 What does the error message say? There isn't one. Just dont add to the database I'm confused... In the statement above you claimed you were using sessions, but in your code you don't use them. If you're referring to this line, that's not using sessions. I've declared the session when the user logs in this is on the home page. I will post my homepage hopefully that'll clear up what I'm tryna explain Loginpage/hompage <?php session_start(); include 'dbc.php'; include 'browser.php'; $user_name = mysql_real_escape_string($_POST['uname']); if ($_POST['Submit']=='Login') { $md5pass = md5($_POST['pwd']); $sql = "SELECT id,user_name FROM users WHERE user_name = '$user_name' AND user_pwd = '$pwd' AND user_activated='1'"; $result = mysql_query($sql) or die (mysql_error()); $num = mysql_num_rows($result); $assoc = mysql_fetch_assoc($result); if ( $num != 0 ) { $_SESSION['user']= $user_name; $_SESSION['uid'] = $assoc['id']; list($user_id,$user_name) = mysql_fetch_row($result); if (isset($_GET['ret']) && !empty($_GET['ret'])) { header("Location: $_GET[ret]"); } else { header("Location: index.php"); } //echo "Logged in..."; exit(); } header("Location: index.php?msg=Invalid Login"); //echo "Error:"; exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151110-users-browser/#findComment-797221 Share on other sites More sharing options...
adam87 Posted March 31, 2009 Author Share Posted March 31, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/151110-users-browser/#findComment-797681 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.