ghostlab Posted January 18, 2008 Share Posted January 18, 2008 Hi, I'm creating a mobile site and I want to redirect/block non-mobile visitors. I put this at the top of the index. <? $userAgent=strtolower($_SERVER['HTTP_USER_AGENT']); if(strstr($userAgent, 'MSIE','firefox')) { header("Location: http://www.thesite.com/blocked.php"); exit(); } ?> But it has this error. Warning: Wrong parameter count for strstr() on line 3. I can put if(strstr($userAgent, 'firefox')) And it works, But how to add more useragents to this without having a error? THanks. Quote Link to comment https://forums.phpfreaks.com/topic/86667-solved-strstr-problem/ Share on other sites More sharing options...
Stooney Posted January 18, 2008 Share Posted January 18, 2008 You need to call strstr() for each browser you wish to filter out. The 3rd parameter strstr() takes is for when you wish to have it return the data before $needle. Basically you should probably use an if statement for each browser you wish to block, or a switch statement. Quote Link to comment https://forums.phpfreaks.com/topic/86667-solved-strstr-problem/#findComment-442909 Share on other sites More sharing options...
ghostlab Posted January 18, 2008 Author Share Posted January 18, 2008 THanks, So Like this? Seems a bit redundant this way though <? $userAgent=strtolower($_SERVER['HTTP_USER_AGENT']); if(strstr($userAgent, 'firefox')) header("Location: http://www.site.com/blocked.php"); exit(); { if(strstr($userAgent, 'MSIE')) header("Location: http://www.site.com/blocked.php"); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86667-solved-strstr-problem/#findComment-442918 Share on other sites More sharing options...
Stooney Posted January 18, 2008 Share Posted January 18, 2008 Pretty much. Maybe if you used something like preg_match() to find the browser, then if $browser = any of the blocked browsers, redirect them. Quote Link to comment https://forums.phpfreaks.com/topic/86667-solved-strstr-problem/#findComment-442929 Share on other sites More sharing options...
GingerRobot Posted January 18, 2008 Share Posted January 18, 2008 A better method would be to stick all of the unwanted user agents into an array, and loop through: <?php $block = array('MSIE','firefox');//fill out as required $useragent=strtolower($_SERVER['HTTP_USER_AGENT']); foreach($block as $v){ if(strstr($useragent,$v)){ header("Location: http://www.thesite.com/blocked.php"); exit(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86667-solved-strstr-problem/#findComment-442930 Share on other sites More sharing options...
ghostlab Posted January 18, 2008 Author Share Posted January 18, 2008 Crap I have another problem there is this <?xml version"1.0" encoding=\UTF-8"?> in the index.php (its a mix of html and php) The PHP engine is parsing it thinking its php becuase it has <? ?> How to fix that ??? A better method would be to stick all of the unwanted user agents into an array, and loop through: oh that looks good, I was thinking an Array would be a better idea also. Quote Link to comment https://forums.phpfreaks.com/topic/86667-solved-strstr-problem/#findComment-442935 Share on other sites More sharing options...
GingerRobot Posted January 18, 2008 Share Posted January 18, 2008 Two options. 1.) Have php echo the above line 2.) Turn off the short_open_tags setting in your php.ini, and replace all your use of the short tag(<?) with the full tag(<?php) - that'd be my prefered method, since i don't liket the short opening tags because we don't get syntax highlighting when we place the code in short tags on the forum Quote Link to comment https://forums.phpfreaks.com/topic/86667-solved-strstr-problem/#findComment-442938 Share on other sites More sharing options...
ghostlab Posted January 18, 2008 Author Share Posted January 18, 2008 Eh thanks guys its working great you can see here http://www.mobileproxies.com/ Except its not blocking the "MSIE" useragent. It must be called something else now in I.E 7. Quote Link to comment https://forums.phpfreaks.com/topic/86667-solved-strstr-problem/#findComment-442951 Share on other sites More sharing options...
GingerRobot Posted January 18, 2008 Share Posted January 18, 2008 It should be fine $_SERVER['HTTP_USER_AGENT'] returns 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)' when i'm using IE7 Quote Link to comment https://forums.phpfreaks.com/topic/86667-solved-strstr-problem/#findComment-442971 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.