argg...Useragent redirect

Discussion in 'PHP' started by Dollar, Mar 4, 2008.

  1. #1
    <?php
    $url = $_SERVER['REQUEST_URI'];
    $ip  = $_SERVER['REMOTE_ADDR'];
    $allowonly = array('firefox','msie');//fill out as required
    $useragent=strtolower($_SERVER['HTTP_USER_AGENT']);
    foreach($allowonly as $v){
    	if(strstr($useragent,$v)=== false){
    		header("HTTP/1.0 403 Forbidden",true,403);
    		echo "<title>403 Forbidden</title><h1>Forbidden</h1><br>You are not permitted to use this service<br>Your client does not have permission to get url $url <br>(Client Ip address: $ip)<br> If you belive this in error, please email us at <script language=\"javascript\">document.write( unescape( '%3C%61%20%68%72%65%66%3D%22%6D%' ) );</script> to report your problem.";
    		exit();
    	}
    }
    ?>
    Code (markup):
    I've tried !== false === true !== true
    can get this too work >.<
    It appears to do exactly the opposite the every time. Blocking firefox and msie and allowing the ones I want to block..;)
    I put this at the top of my redirection script. I only want firefox and I.E to have access to the script. If those useragent are not present then it will say forbidden 403 your client does not have permission to use visit this url. Can anyone help? :p
     
    Dollar, Mar 4, 2008 IP
  2. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This may be a silly question, but have you tried if(!strstr($useragent,$v)) { etc }. I'm not sure "===" is the best syntax to be using in this situation.

    OK, found your problem.

    You're looping through an entire array. So, if the user is using FireFox, it will return true for Firefox but false for ie.

    $true 	=	0;
    foreach($allowonly as $v){
    	if ($true == 0) {
    		if(!strstr($useragent,$v)){
    			echo "<title>403 Forbidden</title><h1>Forbidden</h1><br>You are not permitted to use this service<br>Your client does not have permission to get url $url <br>(Client Ip address: $ip)<br> If you belive this in error, please email us at [your javacript] to report your problem.";
    		} else {
    			$true = 1;
    		}
    	} else {
    		return true;
    	}
    }
    Code (markup):
    There's probably a cleaner way to do this.
     
    Altari, Mar 4, 2008 IP
  3. Dollar

    Dollar Active Member

    Messages:
    2,598
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    90
    #3
    hey thanks, are you sure that worked?
    Because I'm getting now with any useragent.
    It appears that its putting 2 forbiddens and the header error is becuase the it sends http/1.x 403 twice.

     
    Dollar, Mar 4, 2008 IP
  4. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I used it on my server it and it worked just fine. I'm looking again.

    In the mean time, try echoing something at each step, including your agent variable.You may also need to take out the "return true;" section.
     
    Altari, Mar 4, 2008 IP
  5. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OK, try this. Sorry, I'm having a slow day on error checking.
    <?php
    $url = $_SERVER['REQUEST_URI'];
    $ip  = $_SERVER['REMOTE_ADDR'];
    $allowonly = array('firefox','msie');//fill out as required
    // $useragent=strtolower($_SERVER['HTTP_USER_AGENT']);
    // force a user agent to check, insert junk or one of our allowed keys
    $useragent = "nlgjndflgn";
    print $useragent;
    
    // create $true
    $true 	=	0;
    foreach($allowonly as $v){
    	// if an allowed match hasn't already been found, continue
    	if ($true!=-1) {
    		if(!strstr($useragent,$v)){
    			// if the useragent isn't an allowed match, increment true
    			$true++;
    		} else {
    			// if the useragent isn't found, set true
    			$true = -1;
    		}
    	} 
    }
    // if we had an incremented count, they aren't allowed'
    if($true > 0) {
    	echo "<title>403 Forbidden</title><h1>Forbidden</h1><br>You are not permitted to use this service<br>Your client does not have permission to get url $url <br>(Client Ip address: $ip)<br> If you belive this in error, please email us at to report your problem.";
    	exit;
    } 
    // if we didn't have an incremented count, they are allowed
    echo("Success!");
    ?>
    Code (markup):
     
    Altari, Mar 4, 2008 IP
  6. roy.laurie

    roy.laurie Peon

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Just a side-note, in this instance you'd want to use:
    if (strpos([...]) === FALSE)
    PHP:
    It's a little faster, since you don't need the sub-string.
     
    roy.laurie, Mar 4, 2008 IP
  7. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Another version... tested and working

    
    $url = $_SERVER['REQUEST_URI'];
    $ip  = $_SERVER['REMOTE_ADDR'];
    $allowonly = array('firefox','msie');//fill out as required
    $useragent=strtolower($_SERVER['HTTP_USER_AGENT']);
    $GoodBrowser = 0;
    foreach($allowonly as $v){
    		if(strpos($useragent,$v)!==false ){
    			$GoodBrowser = 1;
    			exit();
    	}
    }
    if($GoodBrowser==0){
    	header("HTTP/1.0 403 Forbidden",true,403);
    	echo "<title>403 Forbidden</title><h1>Forbidden</h1><br>You are not permitted to use this service<br>Your client does not have permission to get url $url <br>(Client Ip address: $ip)<br> If you belive this in error, please email us at <script language=\"javascript\">document.write( unescape( '%3C%61%20%68%72%65%66%3D%22%6D%' ) );</script> to report your problem.";
    }
    
    PHP:
     
    imvain2, Mar 5, 2008 IP
    Dollar likes this.