Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正确读取Cookie值的问题-php_Php_Cookies - Fatal编程技术网

正确读取Cookie值的问题-php

正确读取Cookie值的问题-php,php,cookies,Php,Cookies,我试图根据php变量设置要更改的图像的路径,该变量是通过读取window.innerWidth(在文件头中设置)的cookie确定的 问题是浏览器在第一次渲染时没有正确读取cookie,我必须刷新浏览器x2以获得正确的图像 有人能给我指一下正确的方向吗?在php中有没有一种方法可以让cookie在第一次读取时总是正确的——目前它看起来像是被缓存了,或者类似的东西 多谢各位 示例如下: html格式如下: <!DOCTYPE HTML> <html> <head>

我试图根据php变量设置要更改的图像的路径,该变量是通过读取window.innerWidth(在文件头中设置)的cookie确定的

问题是浏览器在第一次渲染时没有正确读取cookie,我必须刷新浏览器x2以获得正确的图像

有人能给我指一下正确的方向吗?在php中有没有一种方法可以让cookie在第一次读取时总是正确的——目前它看起来像是被缓存了,或者类似的东西

多谢各位

示例如下:

html格式如下:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>document.cookie = "device_dimensions=" + window.innerWidth;</script>


<link rel="stylesheet" type="text/css" href="css/img-styles.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<title>Untitled Document</title>

</head>
<body>
<?php require_once('deliver-images.php'); ?>

<img class="ri" src="<?php echo $imgPath ?>the-landscape.jpg" alt="the landscape">

<img class="ri" src="<?php echo $imgPath ?>the-landscape-b.jpg" alt="the landscape">

document.cookie=“设备尺寸=”+window.innerWidth;
无标题文件
风景。jpg“alt=”风景“>
the-scape-b.jpg“alt=”the scape“>

php脚本(deliver images.php)如下:

<?php


$device_width = 0;
$imgPath='';

// Read the device viewport dimensions
    if (isset($_COOKIE['device_dimensions'])) {

        $device_width = $_COOKIE['device_dimensions'];
        echo($device_width);
    }

if ($device_width > 0) {

            // Low resolution image
      if ($device_width <= 480) {

        $imgPath = 'images/mobile/';

      } 

      // Medium resolution image
      else if ($device_width <= 1024 && $device_width > 480) {

        $imgPath = 'images/tablet/';
      }

      else {

        $imgPath = 'images/desktop/';  
      }
    } else {

    $imgPath = 'images/desktop/';   
}





?>

下面这一行设置cookie

<script>document.cookie = "device_dimensions=" + window.innerWidth;</script>
document.cookie=“设备尺寸=”+window.innerWidth;
在这个php文件中,您可以检查这个cookie来决定从哪个文件夹提供图像

<?php require_once('deliver-images.php'); ?>

看看你的代码,你似乎是这样想的

  • 首先运行脚本标记并设置cookie
  • 然后你的PHP使用上一步中设置的cookie进行一些处理
  • 但这是错误的,PHP首先在服务器上处理,然后页面被发送到浏览器并运行脚本


    结论:第一次打开页面时没有设置cookie,因为脚本将在php代码运行后运行。

    我猜第一次访问URL cookie时只是设置了cookie。刷新此IF语句后为TRUE:IF(isset($\u COOKIE['device\u dimensions']){因此设置$\u COOKIE['device\u dimensions']在此之前,如果没有设置cookie,那么它应该第一次工作。或者添加另一个,如果没有设置cookie,那么设置它,否则只需从cookie读取数据。编码器不会显示cookie的设置位置。因此,除了Studio Arena的建议外,我们可以提供的帮助不多。谢谢Ankit-这很有帮助-是的,我是这样想的-我可以从另一个角度看问题现在就找到解决办法。