Jump to content

[SOLVED] strstr() Problem


ghostlab

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();
  }
?>

Link to comment
Share on other sites

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();
}
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.