使用PHP重定向到移动网站 [英] redirect to mobile website using PHP

查看:98
本文介绍了使用PHP重定向到移动网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用生成以下代码:

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);
?>

但是唯一的指导是复制并粘贴此代码".嗯..复制并粘贴到哪里?我需要创建一个新的php文件吗?这是index.php吗?如果我已经有一个index.html文件怎么办?

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?

我了解我将mobile_device_detect.php放在mydomain.com的根中.我的问题是将上面的php代码放在哪里.

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.

推荐答案

将其复制并粘贴到要检测其设备访问者的基于PHP的页面的开头.如果您的服务器将HTML文件解析为PHP(我对此表示怀疑),那么也将其添加到您的HTML文件中.如果您只是在构建网站,则可以,您需要在PHP引擎解析的文件中使用此文件,例如:.php". 如果将其粘贴到HTML而不是服务器未解析的页面中,您将看到与输出相同的代码,这将无济于事.为了使其正常工作,您需要在PHP文件中使用它. 如果您的脚本写得很好并且结构合理,则可能只需要在一个地方包含它即可.这完全取决于您网站的结构.

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 ------

为什么不应该使用此类?它有一个特殊的许可证,它不是绝对免费的. 相反,您可以使用以下简单类: https://github.com/serbanghita/Mobile-Detect

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. 下载 Mobile_Detect.php
  2. 在您要检查设备的PHP页面顶部包含文件:

  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;
}

  • 创建使用html扩展名的重写规则. 如果您仍想使用'.html'作为扩展名,只需创建重写规则,即可将.php重写为.html.否则,请说创建your_page_name.php并在其中添加PHP代码.在同一DIR中创建.htaccess文件,并添加以下行:

  • 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]
    

  • 保存,关闭!现在,您应该可以使用带有.html扩展名的php页面了.要立即访问您的页面,只需键入: http://yourdomain.com/your_page_name.html 就那么简单! 建议:如果我是我,则将重写规则添加到Web服务器的配置文件中.它将更快,更安全.但这是另一堂课.如果您决定使用此方法,则只需搜索堆栈.

    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.

    这篇关于使用PHP重定向到移动网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆