Jump to content

how to detect a mobile browser


gwolff2005

Recommended Posts

Hi guys, I need your help, I am very desperate.

I want people visitng my site with a mobile browser seeing a window saying, would you liek to switch to mobile view? - and the redirect.

 

I found this code and I tried to implement it in my php fiel but nothing changes.

Mobile borwser goes immediately to mobile view without window asking first,

*
What am I missing or doing wrong. you can test it out on guntmarwolffdotcom

 

Thanks for your help!

    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <?php
if(isset($_GET['mVar'])){
    session_start();
    $_SESSION['views']=1; // Declaring a session to track the visits
}


if(!$_SESSION['views'] == 1){ // Checking the session and doing the redirection
    if ($_SERVER["QUERY_STRING"] == null){
        echo "
        <script type=\"text/javascript\">
        if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
            window.location.replace('http://m.guntmarwolff.com');}
        </script>
                ";
    }
}
?>  
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.7.2.js"></script>

<!-- Add fancyBox main JS and CSS files -->
<script type="text/javascript"
src="http://www.guntmarwolff.com/fancyapps-fancyBox-v2.1.5-0-ge2248f4/source/jquery.fancybox.js"></script>
<link rel="stylesheet" type="text/css"
href="http://www.guntmarwolff.com/fancyapps-fancyBox-v2.1.5-0-ge2248f4/source/jquery.fancybox.css"
media="screen" />
<script type="text/javascript"     
    jQuery(document).ready(function () {
    jQuery.fancybox({
            'width': '75%', //Use percentage to maintain responsiveness
            'height': '75%',
            'autoScale': true,
            'transitionIn': 'fade',
            'transitionOut': 'fade',
            'type': 'iframe',
            'href': 'popupbox.htm'
     });
    }); </script>
Link to comment
https://forums.phpfreaks.com/topic/286123-how-to-detect-a-mobile-browser/
Share on other sites

if(preg_match("/chrome/",strtolower($_SERVER['HTTP_USER_AGENT'])))
{
//use a different css maybe
}
elseif(preg_match("/msie/",strtolower($_SERVER['HTTP_USER_AGENT'])))
{
//use a different css maybe
}
elseif(preg_match("/opera/",strtolower($_SERVER['HTTP_USER_AGENT'])))
{
//use a different css maybe
}
elseif(preg_match("/firefox/",strtolower($_SERVER['HTTP_USER_AGENT'])))
{
//use a different css maybe
}
elseif(preg_match("/safari/",strtolower($_SERVER['HTTP_USER_AGENT'])))
{
//use a different css maybe
}

then add some if statements

 

 

Thanks for your input

@ ginerjm
How would I start the session first, if I want to detect First if it is mobile or not and if yes redirect. If that is not necessary how owuld I code it then?

@Mancent:
My problem is that the window does not pop up in my code which it is supposed to do.


<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<?php
if(isset($_GET['mVar'])){
session_start();
$_SESSION['views']=1; // Declaring a session to track the visits
}
else
{
session_start();
$_SESSION['views']=0;
}


if(!$_SESSION['views'] == 1){ // Checking the session and doing the redirection
if ($_SERVER["QUERY_STRING"] == null){
echo "
<script type=\"text/javascript\">
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
window.location.replace('http://m.guntmarwolff.com');}
</script>
";
}
}
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.7.2.js"></script>

<!-- Add fancyBox main JS and CSS files -->
<script type="text/javascript"
src="http://www.guntmarwolff.com/fancyapps-fancyBox-v2.1.5-0-ge2248f4/source/jquery.fancybox.js"></script>
<link rel="stylesheet" type="text/css"
href="http://www.guntmarwolff.com/fancyapps-fancyBox-v2.1.5-0-ge2248f4/source/jquery.fancybox.css"
media="screen" />
<script >
jQuery(document).ready(function () {
jQuery.fancybox({
'width': '75%', //Use percentage to maintain responsiveness
'height': '75%',
'autoScale': true,
'transitionIn': 'fade',
'transitionOut': 'fade',
'type': 'iframe',
'href': 'popupbox.htm'
});
}); </script>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.