简体   繁体   English

在Magento中禁用PHP服务器变量的缓存

[英]Disable caching for PHP Server variables in Magento

I'm trying to use the data from the $_SERVER['HTTP_USER_AGENT'] php variable for some custom functionality in Magento. 我试图将$_SERVER['HTTP_USER_AGENT'] php变量中的数据用于Magento中的某些自定义功能。

However, I'm not sure if this variable is cached because it seems to act like a constant. 但是,我不确定是否缓存此变量,因为它看起来像一个常量。

I'm using Chrome, and when I use two different tabs, one with mobile emulation, and one without, I see the same user agent string. 我使用的是Chrome,当我使用两个不同的标签时,一个带有移动设备仿真选项卡,另一个没有移动设备时,则看到相同的用户代理字符串。 When I do the same thing on a test script on my local PHP, it shows me the user agent string for the specific tab ie mobile user agent string on mobile emulator, and a desktop one otherwise. 当我在本地PHP的测试脚本上执行相同操作时,它将显示特定选项卡的用户代理字符串,即移动模拟器上的移动用户代理字符串,否则显示桌面。

I also tried this using two different browsers (Chrome and Firefox) and after a cache refresh, I opened Chrome using the iPhone emulator and see a iPhone user agent string, but when I open Firefox I see the same iPhone user agent string. 我还使用两种不同的浏览器(Chrome和Firefox)进行了尝试,刷新缓存后,我使用iPhone模拟器打开了Chrome,并看到了iPhone用户代理字符串,但是当我打开Firefox时,我看到了相同的iPhone用户代理字符串。

I'm not sure what's going on but I'm not enjoying this. 我不确定发生了什么,但是我不喜欢这个。 Is there a way to disable caching permanently for PHP $_SERVER variables in Magento? 有没有办法永久禁用Magento中的PHP $ _SERVER变量缓存? Or is there some foolproof way of getting a non-cached user agent string? 还是有某种万无一失的方法来获取未缓存的用户代理字符串? I've tried getenv but it does the same thing. 我已经尝试过getenv但是它做同样的事情。

Out of the box, PHP won't cache the values in $_SERVER . 开箱即用,PHP不会在$_SERVER缓存值。 It's far more likely your production Magento system is using some sort of output/full-page-caching that doesn't take user agent strings into account. 您的生产Magento系统很有可能正在使用某种不考虑用户代理字符串的输出/全页缓存。

I have the same problem of setting cache expire without defining if it's public or private, but solved it by turning cache-control to private . 我有一个相同的问题,即设置缓存过期时没有定义它是公共的还是私有的,但是通过将缓存控制设置为private解决了它。

header("Cache-Control: private, must-revalidate");

with this, PHP will use more memory and CPU because because it's not using shared memory. 这样,PHP将使用更多的内存和CPU,因为它没有使用共享内存。

In magento 2 FPC is biggest problem. 在magento 2中,FPC是最大的问题。 It will caching $_SERVER['HTTP_USER_AGENT'] variables too. 它将也缓存$ _SERVER ['HTTP_USER_AGENT']变量。 i also faced this issue when i am giving support to mobile app developers. 在为移动应用程序开发人员提供支持时,我也遇到了这个问题。

To fix this issue, try the foll 要解决此问题,请尝试以下方法

protected $httpHeader;


public function __construct(
   ...
    \Magento\Framework\HTTP\Header $httpHeader
   ...
) {
    $this->httpHeader = $httpHeader;
}

public function getUserAgent(){
  return $this->httpHeader->getHttpUserAgent();        
}

this file Magento\\Framework\\HTTP\\Header returns clean value of user agent. 此文件Magento \\ Framework \\ HTTP \\ Header返回用户代理的净值。 so no caching problem. 因此没有缓存问题。

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

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