View Full Version : Conditional Statement in PHP
jp182
July 23rd, 2007, 05:42 PM
Alright, So I'm trying to use PHP to look at the users browser and if they have IE6 or below it shows them one page and everything else is shown another.
I did an online search for the correct code but none of the browsers read the code properly:
<?php
if(browser_detection('browser') == 'ie6')
{
include 'ienavi.html';
}
else{
include 'ffnavi.html';
}
?>
evildrummer
July 23rd, 2007, 05:45 PM
Where is the function 'browser_detection()' defined? That's a non-standard PHP function so you may have missed that part,
Neolumi
July 23rd, 2007, 06:20 PM
ok the function listed doesnt exist, although this page details usage of get_browser and also mentions $_SERVER['HTTP_USER_AGENT']:
http://uk.php.net/manual/en/function.get-browser.php
Btw if your wanting to work around the bugs in IE then its better to go with CSS & conditional statements like:
http://www.quirksmode.org/css/condcom.html
jp182
July 23rd, 2007, 10:38 PM
neolumi i thought about that but it doesn't appear to work with php includes at all. IE6 ends up showing both. I'm not sure why.
so i went to that php link and while i got his little thing to tell you what it sees:
www.thespeedlounge.com/tv_2006a.php
however, it doesnt say how i can tell an IF statement to look at what browser and version someone is using and display a particular page. i'm getting the feeling that i can't get this to work.
OH and for those who say "use CSS", the problem is i'm trying to display an image map and i'm pretty sure u can't do that in CSS.
and i'm trying to load it up as a page so i can use this for a navigation on multiple pages. Trying to eliminate alot of manual work.
Refined
July 24th, 2007, 01:17 PM
Hi jp182,
According to the link Neolumi posted regarding get_browser, you should be able to do this because it returns an assosciative array:
<?php
/**
* the first argument can be a user
* agent if you want to analyse
* a specific one, otherwise leave
* as null to analyse current user's
* user agent. The second argument returns
* an array if true, an object if false
*/
$browserInfo = get_browser(null, true);
$browserType = $browserInfo['browser'];
$browserVersion = $browserInfo['version'];
// process this info however you want eg
switch ($browserType)
{
case 'Internet Explorer':
if ($browserVersion == 5)
{
// take to correct page
}
elseif ($browserVersion == 6)
{
// etc
}
break;
case 'Firefox':
if ($browserVersion == 1.0)
{
// etc
}
break;
}
?>
Hope it helps.
jp182
July 24th, 2007, 02:05 PM
thanks! i'll give it a shot; hopefully soon and I'll let you know how it came out.
jp182
July 24th, 2007, 04:02 PM
alright well this is what i have:
<?php
// Loads the class
require 'Browscap/Browscap.php';
// Creates a new Browscap object (loads or creates the cache)
$bc = new Browscap('Browscap/cache');
// Gets information about the current browser's user agent
$current_browser = $bc->getBrowser();
$browserType = $browserInfo['browser'];
$browserVersion = $browserInfo['version'];
// process this info however you want eg
switch ($browserType)
{
case 'Internet Explorer':
if ($browserVersion == 6)
{
include '/ienavi.html';// take to correct page
}
elseif ($browserVersion == 7)
{
include '/ffnavi.html';// take to correct page
}
break;
case 'Firefox':
if ($browserVersion == 2.0)
{
include '/ffnavi.html';// etc
}
break;
}
?>
this is what i have but it's still not loading up the pages :shifty:
Refined
July 25th, 2007, 05:26 AM
Hi jp182,
Where did these bits come from?
// Loads the class
require 'Browscap/Browscap.php';
// Creates a new Browscap object (loads or creates the cache)
$bc = new Browscap('Browscap/cache');
// Gets information about the current browser's user agent
$current_browser = $bc->getBrowser();
I don't know that class so I can't help you use it. All I can tell you is now that you've changed the way you're doing it these lines won't work
$browserType = $browserInfo['browser'];
$browserVersion = $browserInfo['version'];
// onwards
Because $browserInfo is no longer defined using $browserInfo = get_browser(null, true);
Regards
jp182
July 25th, 2007, 12:25 PM
i had to add those other 3 lines in to keep from getting Browsercap.ini failures (i can't install Browscap on the server directly).
Charleh
July 25th, 2007, 12:28 PM
Yeah but the point is removing the
$browserInfo = get_browser(null, true);
line is going to make the code that refined posted fail - can you not put this back in but still leave the browscap stuff above it?
Refined
July 25th, 2007, 12:47 PM
Thanks Charleh I was about to reply with the same thing.
jp182, I'm not sure what Browsercap is, but Browsercap.ini seems to be used to hold information regarding different browser types and custom headers describing their capabilities. You should be able to get all of this information (and maybe more) using the get_browser() function.
Regards
jp182
July 25th, 2007, 12:49 PM
good question, are you saying i should remove the $browserInfo line? If I do that, how will it look at the viewers browser information?
btw, i edited the code to this and now i'm just getting an error saying it can't find the get_browser() function
<?php
// Loads the class
require 'Browscap/Browscap.php';
// Creates a new Browscap object (loads or creates the cache)
$bc = new Browscap('Browscap');
// Gets information about the current browser's user agent
$browserType = $browserInfo['browser'];
$browserVersion = $browserInfo['version'];
// process this info however you want eg
switch ($browserType)
{
case 'Internet Explorer':
if ($browserVersion == 5)
{
include 'ienavi.html';
}
elseif ($browserVersion == 6)
{
include 'ffnavi.html';
}
break;
case 'Firefox':
if ($browserVersion == 1.0)
{
include 'ffnavi.html';
}
break;
}
?>
jp182
July 25th, 2007, 12:52 PM
Thanks Charleh I was about to reply with the same thing.
jp182, I'm not sure what Browsercap is, but Browsercap.ini seems to be used to hold information regarding different browser types and custom headers describing their capabilities. You should be able to get all of this information (and maybe more) using the get_browser() function.
Regards
well the browsercap.ini file is there basically because just using get_browser() doesn't work for me. I'm on a shared server so the only way to get a function like get_browser()....at least according to this link:
http://garetjax.info/projects/browscap/
thanks for helping me through this guys. i think i'm getting closer though. I'm not getting errors anymore, the page it is supposed to show just isn't coming up
Refined
July 25th, 2007, 01:10 PM
Hi jp182,
Let's try this, click here (http://uk2.php.net/manual/en/function.get-browser.php#70641) and copy the function written by that ever so nice guy. Make sure in the function you specify where your browsercap.ini file is located here:
$brows=parse_ini_file("php_browscap.ini",true);
Using that function, try this:
<?php
// returns assosciative array of cient's browser information
$browserInfo = php_get_browser();
$browserType = $browserInfo['browser'];
$browserVersion = $browserInfo['version'];
// process this info however you want eg
switch ($browserType)
{
case 'Internet Explorer':
if ($browserVersion == 5)
{
// take to correct page
}
elseif ($browserVersion == 6)
{
// etc
}
break;
case 'Firefox':
if ($browserVersion == 1.0)
{
// etc
}
break;
}
?>
Hope it works,
Regards.
jp182
July 25th, 2007, 01:50 PM
nope no dice.
Parse error: syntax error, unexpected $end in /mnt/Target01/331254/www.thespeedlounge.com/web/content/tv_2006b.php on line 188
I think the problem with that guy's code is that i don't have a ini file I think. That Browscap thing I was using before Here's what my code looks like:
<?php
function php_get_browser($agent = NULL){
$agent=$agent?$agent:$_SERVER['HTTP_USER_AGENT'];
$yu=array();
$q_s=array("#\.#","#\*#","#\?#");
$q_r=array("\.",".*",".?");
$brows=parse_ini_file("Browscap/browscap.ini",true);
foreach($brows as $k=>$t){
if(fnmatch($k,$agent)){
$yu['browser_name_pattern']=$k;
$pat=preg_replace($q_s,$q_r,$k);
$yu['browser_name_regex']=strtolower("^$pat$");
foreach($brows as $g=>$r){
if($t['Parent']==$g){
foreach($brows as $a=>$b){
if($r['Parent']==$a){
$yu=array_merge($yu,$b,$r,$t);
foreach($yu as $d=>$z){
$l=strtolower($d);
$hu[$l]=$z;
}
}
}
}
}
break;
}
}
return $hu;
// returns assosciative array of cient's browser information
$browserInfo = php_get_browser();
$browserType = $browserInfo['browser'];
$browserVersion = $browserInfo['version'];
// process this info however you want eg
switch ($browserType)
{
case 'Internet Explorer':
if ($browserVersion == 5)
{
// take to correct page
}
elseif ($browserVersion == 6)
{
// etc
}
break;
case 'Firefox':
if ($browserVersion == 1.0)
{
// etc
}
break;
}
?>
if i download the browscap.ini file and just upload it in the same directory as the .php file, will that work?
jp182
July 25th, 2007, 02:20 PM
nope no dice.
Parse error: syntax error, unexpected $end in /mnt/Target01/331254/www.thespeedlounge.com/web/content/tv_2006b.php on line 188
I think the problem with that guy's code is that i don't have a ini file I think. That Browscap thing I was using before Here's what my code looks like:
<?php
function php_get_browser($agent = NULL){
$agent=$agent?$agent:$_SERVER['HTTP_USER_AGENT'];
$yu=array();
$q_s=array("#\.#","#\*#","#\?#");
$q_r=array("\.",".*",".?");
$brows=parse_ini_file("Browscap/browscap.ini",true);
foreach($brows as $k=>$t){
if(fnmatch($k,$agent)){
$yu['browser_name_pattern']=$k;
$pat=preg_replace($q_s,$q_r,$k);
$yu['browser_name_regex']=strtolower("^$pat$");
foreach($brows as $g=>$r){
if($t['Parent']==$g){
foreach($brows as $a=>$b){
if($r['Parent']==$a){
$yu=array_merge($yu,$b,$r,$t);
foreach($yu as $d=>$z){
$l=strtolower($d);
$hu[$l]=$z;
}
}
}
}
}
break;
}
}
return $hu;
// returns assosciative array of cient's browser information
$browserInfo = php_get_browser();
$browserType = $browserInfo['browser'];
$browserVersion = $browserInfo['version'];
// process this info however you want eg
switch ($browserType)
{
case 'Internet Explorer':
if ($browserVersion == 5)
{
// take to correct page
}
elseif ($browserVersion == 6)
{
// etc
}
break;
case 'Firefox':
if ($browserVersion == 1.0)
{
// etc
}
break;
}
?>
if i download the browscap.ini file and just upload it in the same directory as the .php file, will that work?
what's weird is that even though i'm getting that error, the if statement seems to work with this code:
<?php
// Loads the class
require 'Browscap/Browscap.php';
// Creates a new Browscap object (loads or creates the cache)
$bc = new Browscap('Browscap');
// Gets information about the current browser's user agent
$ua = get_browser ();
if ( ( $ua->browser == "Internet Explorer" ) && ( $ua->version < 7 ) ) {
include 'ienavi.html';
}
else {
include 'ffnavi.html';
}
?>
Refined
July 25th, 2007, 06:33 PM
Hello,
Firstly, use this as the function:
function php_get_browser($agent = NULL)
{
$agent = $agent ? $agent : $_SERVER['HTTP_USER_AGENT'];
$yu = array();
$q_s = array("#\.#","#\*#","#\?#");
$q_r = array("\.",".*",".?");
$brows = parse_ini_file("nameofinifile.ini", true);
foreach ($brows as $k=>$t)
{
if (fnmatch($k,$agent))
{
$yu['browser_name_pattern'] = $k;
$pat = preg_replace($q_s, $q_r, $k);
$yu['browser_name_regex'] = strtolower("^$pat$");
foreach ($brows as $g=>$r)
{
if ($t['Parent'] == $g)
{
foreach ($brows as $a=>$b)
{
if ($r['Parent']==$a)
{
$yu = array_merge($yu, $b, $r, $t);
foreach ($yu as $d=>$z)
{
$l = strtolower($d);
$hu[$l] = $z;
}
}
}
}
}
}
break;
}
return $hu;
}
The one you were using was missing a closing parenthesis on the deepest foreach loop and the return $hu; line was outside of the function. An 'unexpected $end' error means there's a missing ending parenthesis on a function/loop somewhere.
Yes this won't work unless you have the browser ini file, this function is meant for people who have the browser ini file but do not have access to the php configuration file to tell php where to find it. This function acts exactly the same as the native get_browser(); function.
Download the ini file, either from where you did before or I'd reccomend from this link (http://browsers.garykeith.com/downloads.asp), download the third file, named 'php_browscap.ini'.
Stick it in the same place as the php file with the function and rest of the code and change this line:
$brows = parse_ini_file("nameofinifile.ini", true);
Where 'nameofinifile' is obviously the name of your ini file.
Hope it works.
Regards
Refined
July 26th, 2007, 05:05 AM
Hi jp182,
Sorry missed that lost post of yours. So it seems to be working ok now?
Regards
jp182
July 29th, 2007, 12:14 AM
First let me thank everyone including Refined most of all. I really got this working and i'm really happy about it. Only problem is that IE6 isn't following th econditions properly for some reason:
http://www.thespeedlounge.com/features.php
here's the code:
if ( ( $ua->browser == "Internet Explorer" ) && ( $ua->version == "6" ) ) {
include 'ienavi.html';
}
else {
include 'ffnavi.html';
IE6 still keeps thinking that doesn't match the criteria for some reason.
jp182
July 30th, 2007, 10:37 AM
anyone else have that issue?
Charleh
July 30th, 2007, 10:41 AM
anyone else have that issue?
You tried echoing the variables $ua->browser and $ua->version to output to see what they are when you are using IE6?
jp182
July 30th, 2007, 02:30 PM
hmmm never thought of that, let me give that a try.
jp182
July 30th, 2007, 02:47 PM
alright, i through the
echo $ua->browser;
in there and it returned nothing. Actually its still on that page but its not displaying any information which leads me to believe that the function is not capturing any information. However, if I change the version number to 7, the conditional statement works fine.
jp182
July 30th, 2007, 11:24 PM
breakthrough!
alright so i went with a different browser detector and got it to work!
http://www.thespeedlounge.com/tv_2006b.php
The only problem is now i'm having some other issues that aren't related to conditional statements.
Thanks fellas! I learned alot!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.