www.webdeveloper.com
Results 1 to 9 of 9

Thread: Mobile Site Redirect Script

  1. #1
    Join Date
    Dec 2010
    Posts
    52

    Mobile Site Redirect Script

    Would Like suggestions and some help on what code is used with the following Mobile Redirect .php script. What do I Put on my homepage for code with this .php script (any better scripts with html code much appreciated)

    Code:
    <?php

    $mobile_browser = '0';

    if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
    $mobile_browser++;
    }

    if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
    $mobile_browser++;
    }

    $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
    $mobile_agents = array(
    'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
    'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
    'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
    'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
    'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
    'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
    'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
    'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
    'wapr','webc','winw','winw','xda ','xda-');

    if (in_array($mobile_ua,$mobile_agents)) {
    $mobile_browser++;
    }

    if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) {
    $mobile_browser++;
    }

    if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) {
    $mobile_browser = 0;
    }

    if ($mobile_browser > 0) {
    // do something
    }
    else {
    // do something else
    }

    ?>

  2. #2
    Join Date
    May 2003
    Posts
    599
    I havnt looked at the actual script to see if it will work to begin with, but assuming it does and you mean to ask what goes in the if, else to make it function as a redirect page..., you could do something like this:

    PHP Code:
    if ($mobile_browser 0) {
    // go to the mobile page
    header('Location: my_mobile_page.php');
    exit;
    }
    else {
    // you could either do a statement like the one above to go to the standard site page
    //or you could do away with the else part altogether and proceed to load the standard site //HTML

    We do precision guesswork

  3. #3
    Join Date
    Dec 2010
    Posts
    52
    thanks for the help much appreciated, beginner scripter lol, as u can tell. Would i past that in my index (hompage) header or above my <head> ?
    Also I have my Mobile page im in a directory folder Mobile/products.html. How is the script going to redirect there? I have a standard site then i need it to read mobile header then redirect if necessary.

    Thanks
    Last edited by Slimpick404; 01-31-2011 at 08:23 AM. Reason: More Info Provided

  4. #4
    Join Date
    May 2003
    Posts
    599
    Quote Originally Posted by Slimpick404 View Post
    Would i past that in my index (hompage) header or above my <head> ?
    Anytime you try to redirect a header you have to execute that code before ANY text or "echoing" occurs ...so if you are going to execute a redirect, include the code before any HTML

    PHP Code:
    <?php
    //very beginning of script page, make sure there are no blank lines or spaces prior to declaring script tag

    if(blah){
         
    header('Location: blah.php');
         exit;
    }

    //if they are where they need to be, then do html....
    ?>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    </body>
    </html>
    Last edited by hastx; 01-31-2011 at 10:43 AM.
    We do precision guesswork

  5. #5
    Join Date
    Dec 2010
    Posts
    52
    Thanks much appreciated I will try to upload tonight and let you know if it worked. Whats still racking my brain, is in the long php i have that should read the mobile header with different phone types, how will it go to my mobile version of my site.

  6. #6
    Join Date
    May 2003
    Posts
    599
    All that code that you have only has 1 purpose ...find out if the person browsing is on a mobile device...

    You are aware that the code does not do any type of conversion for you, right? you still have to design your "mobile" site before you can redirect people to it. The code I gave just illustrates how to get someone to the mobile page you designed (if you detect they are using a mobile device).
    We do precision guesswork

  7. #7
    Join Date
    Dec 2010
    Posts
    52
    Thats what i mean also, I have a test page built as html, but scaled for mobile. So how do i use either script to redirect there when is detects mobile?

  8. #8
    Join Date
    May 2003
    Posts
    599
    That is what the header function is for...it does the same thing as clicking on a link, but it does it automatically.

    Have you ever done a meta refresh in html? Same concept.
    We do precision guesswork

  9. #9
    Join Date
    Dec 2010
    Posts
    52
    thanks ill try it tonight!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles