redirect to mobile website using PHP

。_饼干妹妹 提交于 2019-12-25 05:59:17

问题


I have use this to generate this code:

<?php
    require_once('mobile_device_detect.php');
    mobile_device_detect(true,false,true,true, true,true,true,'http://m.mydomain.com',false);
?>

But the only directions are to "copy and paste this code". Um.. copy and paste where? Do I need to create a new php file? Is this index.php? What if I already have an index.html file?

EDIT: I understand that I put mobile_device_detect.php in the root of mydomain.com. My question is where to put the above php code.


回答1:


Copy and paste this at the beginning of your PHP based pages that you want to detect the visitors for their device. If your server parses HTML files as PHP which I doubt then add this in your HTML files as well. If you're just building the website then yes you need this in files which are parsed by the PHP engine for example: ".php". If you paste this in page that is HTML and not parsed by the server you'll see this same code as output which will do nothing. In order to have it working you need it in PHP files. If your script is well written and well structured you may need to include it in only one place. It all depends how your website is structured.

------ UPDATE ------

Why you shouldn't be using this class? It have a special license which is not absolutely free. Instead you can use this simple class: https://github.com/serbanghita/Mobile-Detect

  1. Download Mobile_Detect.php
  2. Include the file at the top in your PHP page where you want the device to be checked:

    // Include the mobile device detect class
    include 'Mobile_Detect.php';
    // Init the class
    $detect = new Mobile_Detect();
    // And here is the magic - checking if the user comes with a mobile device
    if ($detect->isMobile()) {
        // Detects any mobile device.
        // Redirecting
        header("Location: http://your_redirected_url.com"); exit;
    }
    
  3. Creating rewrite rules for using html extension. If you still want to use '.html' as extension just create rewrite rule that will rewrite your .php as .html. Or otherwise said create your_page_name.php and add the PHP code there. Create .htaccess file in the same DIR and add the following lines:

    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^your_page_name.html/?$ your_page_name.php [L]
    

Save, close! Now you should be able to use your php page with .html extension. To access your page now just type: http://yourdomain.com/your_page_name.html Simple as that! Suggestion: If I was you I'd add the rewrite rules in the web server's config file. It will be faster and more secure. But that's another lesson. If you decide to use this method just search the Stack.




回答2:


Copy and paste the code anywhere you want. Just make sure the function is defined on any page that needs it.




回答3:


You should either buy the script mobile_device_detect.php from the site or use a free method called pay with a tweet option.. Go to the download page and you will see them there..




回答4:


ok, in case this helps someone, here are the details of what's working for me:

Create an index.php file with just this:

<?php

require_once('mobile_device_detect.php');
$mobile = mobile_device_detect();

// redirect all mobiles to mobile site and all other browsers to desktop site
if($mobile==true){
  header('Location:http://m.yourdomain.com/');
}else{
  header('Location:http://yourdomain.com/index.html');
}
exit;

?>

Drop the mobile_device_detect.php file in the root of your site.

Then, add this line to your .htaccess file:

DirectoryIndex index.php index.html


来源:https://stackoverflow.com/questions/12534342/redirect-to-mobile-website-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!