-2

I have this function where it checks which divice you are on. What I want now it that it will redirect to a page if the if statement is true I know how to return a message but I don't know how to redirect instant when if statement is true. for example if($android){ it needs to redirect to google.com

I want it to redirect to https://itunes.apple.com/us/app/bezorgland/id1364919752?mt=8 but that isnt working I get an error then

function user_agent(){
        $iPod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
        $iPhone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
        $iPad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
        $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");

        if($iPad||$iPhone||$iPod){
            return 'ios';
        }else if($android){
            return 'android';
        }else{
            return 'pc';
        }
    }

1 Answers1

1

Try this:

function user_agent(){
    $iPod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
    $iPhone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    $iPad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
    $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");

    function redirect($url){
      if (!headers_sent()){    
        header('Location: '.$url);
        exit;
      } else {  
        echo '<script type="text/javascript">';
        echo 'window.location.href="'.$url.'";';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
        echo '</noscript>'; exit;
      }
   }

   if($iPad||$iPhone||$iPod){
      $url = "http://yoururl.com/ios";
      redirect($url);
   } elseif($android){
      $url = "http://yoururl.com/android";
      redirect($url);
   } else {
      $url = "http://yoururl.com/pc";
      redirect($url);
   }

}
Grant
  • 2,413
  • 2
  • 30
  • 41