使用PHP重定向到移动网站

sol*_*eil 0 html php mobile redirect

我用来生成这段代码:

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

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

编辑:我明白我把它放在mobile_device_detect.php了根mydomain.com.我的问题是在哪里放上面的PHP代码.

GTo*_*rov 5

将这个复制并粘贴到您想要检测其设备访问者的基于PHP的页面的开头.如果您的服务器将HTML文件解析为PHP,我怀疑它也会在HTML文件中添加它.如果您只是构建网站,那么您需要在PHP引擎解析的文件中使用它,例如:".php".如果您将此粘贴在HTML页面中并且未被服务器解析,您将看到与输出相同的代码,它将不执行任何操作.为了让它工作,你需要在PHP文件中.如果您的脚本编写得很好并且结构合理,则可能只需要将其包含在一个位置.这完全取决于您的网站的结构.

------更新------

为什么你不应该使用这个课程?它有一个特殊的许可证,并非完全免费.相反,你可以使用这个简单的类:https://github.com/serbanghita/Mobile-Detect

  1. 下载Mobile_Detect.php
  2. 在PHP页面的顶部包含要检查设备的文件:

    // 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;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 为使用html扩展创建重写规则.如果你仍然想使用'.html'作为扩展名,只需创建重写规则,将.php重写为.html.或者说创建your_page_name.php并在那里添加PHP代码.在同一个DIR中创建.htaccess文件并添加以下行:

    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^your_page_name.html/?$ your_page_name.php [L]
    
    Run Code Online (Sandbox Code Playgroud)

保存,关闭!现在你应该能够使用扩展名为.html的php页面了.要立即访问您的页面,请输入:http://yourdomain.com/your_page_name.html这很 简单! 建议:如果我是你,我会在Web服务器的配置文件中添加重写规则.它会更快,更安全.但这是另一个教训.如果您决定使用此方法,只需搜索堆栈.