doufu9947 2015-10-31 11:48
浏览 148

Cookie - 注意:未定义[重复]

I have following code a unfortunately it doesn't work. I get notice Undefined index: imagerotate. I can't solve this problem. Can someone help me please? Thanks.

 <?php
$imgArray = array("header_1000x150_basnenaprani_3_o.png", "header_1000x150_basnenaprani_1_m.png", "header_1000x150_basnenaprani_1-zlatá.png");
// Path to where your images are stored
$hostDir = "http://myweb.com/wp-content/uploads/2015/10/"; // Don't forget that ending '/'

$imgArraySize = count($imgArray); // Store the size of the array (starts at 1)
$imgToShow = 0; // We'll default to always show the first image.

// Check to see if the cookie has already been created.
if ($_COOKIE['imagerotate'] != "") {
  // The cookie existed
  $imgToShow = $_COOKIE['imagerotate'];
  if($imgToShow + 1 >= $imgArraySize) {
    // The image we were supposed to show next would have been out of bounds in the array.
    $imgToShow = 0;  // We'll show the first image
  }
  else  // The image to be shown is not out of bounds.
    $imgToShow++;  // Increment the image counter
}

// Write the new cookie with the new image we are showing
setcookie ("imagerotate", $imgToShow, time()+5);

//Print the image from the directory.
echo "<img src=\"".$hostDir.$imgArray[$imgToShow]."\" alt=\"An Image\" />";

?>
</div>
  • 写回答

1条回答 默认 最新

  • 「已注销」 2015-10-31 13:05
    关注

    try this: use "isset" on if statement (tested on phptester.net):

    <?php
      $imgArray = array("header_1000x150_basnenaprani_3_o.png","header_1000x150_basnenaprani_1_m.png", "header_1000x150_basnenaprani_1-zlatá.png");
      // Path to where your images are stored
      $hostDir = "http://myweb.com/wp-content/uploads/2015/10/"; // Don't forget that ending '/'
    
      $imgArraySize = count($imgArray); // Store the size of the array (starts at 1)
      $imgToShow = 0; // We'll default to always show the first image.
    
      // Check to see if the cookie has already been created.
      if (isset($_COOKIE['imagerotate']) != "") {
       // The cookie existed
        $imgToShow = $_COOKIE['imagerotate'];
        if($imgToShow + 1 >= $imgArraySize) {
        // The image we were supposed to show next would have been out of bounds in the array.
        $imgToShow = 0;  // We'll show the first image
      }
      else  // The image to be shown is not out of bounds.
        $imgToShow++;  // Increment the image counter
      }
    
     // Write the new cookie with the new image we are showing
     setcookie ("imagerotate", $imgToShow, time()+5);
    
     //Print the image from the directory.
     echo '<img src="'.$hostDir.$imgArray[$imgToShow].'" alt="An Image" />';
    

    Also, change this line:

    echo "<img src=\"".$hostDir.$imgArray[$imgToShow]."\" alt=\"An Image\" />";
    

    to this:

    echo '<img src="'.$hostDir.$imgArray[$imgToShow].'" alt="An Image" />';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算