Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.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
首次在PHP中学习使用Cookie_Php_Cookies - Fatal编程技术网

首次在PHP中学习使用Cookie

首次在PHP中学习使用Cookie,php,cookies,Php,Cookies,我是全新的PHP,我有点卡住和沮丧!我希望这个简单的小站点在用户每次重新加载页面时都显示不同的图像-使用php中的cookies 我知道我的代码远非正确,但我真的希望在如何实现目标的正确方向上得到推动 这是我的密码: <?php function random_image(){ $rdmimg = array(); array_push($rdmimg, "/php/rollingpin.jpg"); array_push($rdmimg, "/php/candlestick.jpg")

我是全新的PHP,我有点卡住和沮丧!我希望这个简单的小站点在用户每次重新加载页面时都显示不同的图像-使用php中的cookies

我知道我的代码远非正确,但我真的希望在如何实现目标的正确方向上得到推动

这是我的密码:

<?php 
function random_image(){
$rdmimg = array();
array_push($rdmimg, "/php/rollingpin.jpg");
array_push($rdmimg, "/php/candlestick.jpg");
array_push($rdmimg, "/php/table.jpg");
array_push($rdmimg, "/php/table2.jpg");
$output = rand(0, count($rdmimg) - 1);
echo $rdmimg[$output];
};
$a = random_image();
setcookie("check image", $checkimage);

$_COOKIE = $a;

if(isset($checkimage)
if ($_COOKIE == $a){
    $i = $i + 1;
            //I know $i isn't set, thinking of implementing somehow
}

?>

<html>
<head>
<title>JFQ Turnings</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<img src="jfqturnings.gif" alt="JFQ Turnings, coming soon." width="864" height="100">
<br /><img src="<?php echo $a ?>" alt="" width="864" height="567">
</body>
</html>

上面的代码中似乎使用了两种方法:

  • 使用
    $output=rand(0,count($rdmimg)-1)从数组中选择一个随机元素
  • 递增一个变量以跟踪显示的图像
    $i=$i+1
  • 我想展示这两种方法是如何用大量注释的代码实现的

  • 如果选择随机元素,唯一的问题是确保同一图像不会出现两次

    function random_image() {
        $rdmimg = array();
        array_push($rdmimg, "/php/rollingpin.jpg");
        array_push($rdmimg, "/php/candlestick.jpg");
        array_push($rdmimg, "/php/table.jpg");
        array_push($rdmimg, "/php/table2.jpg");
        $output = $rdmimg[rand(0, count($rdmimg) - 1)]; //obtain the path of the image from the array
        return $output; //return instead of echoing
    }
    
    $checkimage = random_image();
    
    if (isset($_COOKIE['check_image'])) //if a cookie has been set
        while ($checkimage == $_COOKIE['check_image']) //keep attempting to get a random image that is not the current one
            $checkimage = random_image();
    
    setcookie("check_image", $checkimage);
    
    //make sure the below code is echo $checkimage NOT echo $a.
    
  • 如果您想循环浏览图像,可以尝试以下方法:

    <?php
    
    $rdmimg = array();
    array_push($rdmimg, "/php/rollingpin.jpg");
    array_push($rdmimg, "/php/candlestick.jpg");
    array_push($rdmimg, "/php/table.jpg");
    array_push($rdmimg, "/php/table2.jpg");
    
    $count = intval($_COOKIE['check_image']); //convert to a number, strings return 0
    $count++; //increment
    
    if ($count < 0 || $count > count($rdmimg) - 1) //if out of range, reset
        $count = 0;
    
    setcookie("check_image", $count);
    
    //make sure the code below is echo $rdmimg[$count] (replace it with the existing echo $a)
    

    您应该返回
    $rdmimg[$output]没有回应。哦,是的,这让老师在这个代码之前从未设置过cookie…我应该将它设置为$a吗?非常感谢您的回复!它们工作完美无瑕,我想你真的帮助我理解了饼干设置的基本原理,我一直在努力