简体   繁体   English

使用PHP重定向到移动网站

[英]redirect to mobile website using 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);
?>

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? 我需要创建一个新的php文件吗? Is this index.php ? 这是index.php吗? What if I already have an index.html file? 如果我已经有了index.html文件怎么办?

EDIT: I understand that I put mobile_device_detect.php in the root of mydomain.com . 编辑:我了解我将mobile_device_detect.php放在mydomain.com的根目录中。 My question is where to put the above php code. 我的问题是将上述php代码放在哪里。

Copy and paste this at the beginning of your PHP based pages that you want to detect the visitors for their device. 将其复制并粘贴到要检测其设备访问者的基于PHP的页面的开头。 If your server parses HTML files as PHP which I doubt then add this in your HTML files as well. 如果您的服务器将HTML文件解析为PHP(我对此表示怀疑),那么也将其添加到HTML文件中。 If you're just building the website then yes you need this in files which are parsed by the PHP engine for example: ".php". 如果您只是在构建网站,则可以,您需要在PHP引擎解析的文件中使用此文件,例如:“。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. 如果将其粘贴到HTML而不是服务器未解析的页面中,您将看到与输出相同的代码,这将无济于事。 In order to have it working you need it in PHP files. 为了使其正常工作,您需要在PHP文件中使用它。 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 相反,您可以使用此简单的类: https : //github.com/serbanghita/Mobile-Detect

  1. Download Mobile_Detect.php 下载Mobile_Detect.php
  2. Include the file at the top in your PHP page where you want the device to be checked: 在您要检查设备的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; } 
  3. Creating rewrite rules for using html extension. 创建使用html扩展名的重写规则。 If you still want to use '.html' as extension just create rewrite rule that will rewrite your .php as .html. 如果您仍然想使用“ .html”作为扩展名,只需创建重写规则即可将您的.php重写为.html。 Or otherwise said create your_page_name.php and add the PHP code there. 否则,请说创建your_page_name.php并在其中添加PHP代码。 Create .htaccess file in the same DIR and add the following lines: 在同一DIR中创建.htaccess文件,并添加以下行:

     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. 现在,您应该可以使用带有.html扩展名的php页面。 To access your page now just type: http://yourdomain.com/your_page_name.html Simple as that! 要立即访问您的页面,只需输入:http: //yourdomain.com/your_page_name.html就这么简单! Suggestion: If I was you I'd add the rewrite rules in the web server's config file. 建议:如果您是我,则将重写规则添加到Web服务器的配置文件中。 It will be faster and more secure. 它将更快,更安全。 But that's another lesson. 但这是另一堂课。 If you decide to use this method just search the Stack. 如果您决定使用此方法,只需搜索堆栈。

Copy and paste the code anywhere you want. 将代码复制并粘贴到所需的任何位置。 Just make sure the function is defined on any page that needs it. 只要确保在需要此功能的任何页面上定义了该功能即可。

您应该从网站上购买脚本mobile_device_detect.php ,或使用一种名为pay的免费方法,并带有tweet选项。.转到下载页面,您将在此处看到它们。

ok, in case this helps someone, here are the details of what's working for me: 好的,以防万一,这是对我有用的细节:

Create an index.php file with just this: 使用以下命令创建一个index.php文件:

<?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. 将mobile_device_detect.php文件拖放到网站的根目录中。

Then, add this line to your .htaccess file: 然后,将此行添加到您的.htaccess文件中:

DirectoryIndex index.php index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM