Link to home
Start Free TrialLog in
Avatar of burnedfaceless
burnedfaceless

asked on

Use php to detect mobile device period

I was wondering is there a way to use php to detect a mobile device period?

I fixed something with the iPhone and I need to just be able to detect any mobile device.

Thank you,
Brian
Avatar of gplana
gplana
Flag of Spain image

This doesn't seem to be possible, as you can't access to client properties from server. However, you can pass some kind of parameter on the URL. For example you can make your Android device call this URL:

http://myurl.com/myfile.php?device=Android

and make the call from your iPhone device this way:

http://myurl.com/myfile.php?device=iPhone

Or maybe just use one and assume that if this argument isn't present then assume the other.

Then on PHP you should do something like this:

if ($_GET['device']=='iPhone) {
   // Put the code for iPhone case here
}
else
{
   // Put the code for Android case here
}

Open in new window

Hope it helps. Regards.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you can't access to client properties from server
Actually you get whatever the client sends in the request.  This usually includes some very useful information which is present in $_ENV and/or $_SERVER arrays.  PHP creates these arrays before your script gets control.  You can see what is in there with var_dump($_SERVER);
Avatar of burnedfaceless
burnedfaceless

ASKER

I was wondering if there was an easier way.

The iPhone form distortion was brought about by css media queries I think they don't interact with bootcamp well on iPhone.

9/10 Android devices run it properly but for that one user we're a bush-league site.

So I will use header or whatever and have separate mobile and desktop pages.

Unless there's a better route.
Ultimately css media queries are limited so I'm agreeing with Ray on using php.

The bootstrap template is forgiving but if you work out of it don't add other css queries. Android is too fragmented and the iPhone has its own issues.
You can use header() to redirect to another script or you can just load the script directly in PHP, perhaps with include() and run the script without redirection.  Something conceptually like this...

if (iphone) include('iphone_script.php')
elseif (android) include('android_script.php')
elseif (desktop) include('desktop_script.php')
else trigger_error('UNKNOWN BROWSER", E_USER_ERROR);

Open in new window