仅选择一次随机值 [英] choose random value only once

查看:84
本文介绍了仅选择一次随机值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面每次加载时都会加载随机背景图片:

My page loads a random background image every time it loads:

<?php
    $input = array(1, 2, 3, 4, 5, 6, 7, 8);
    $num = array_rand($input, 1);
    $bg = "img/bg" . $num . ".jpg";
?>

但是我希望它只能这样做一次.我希望图像在访问者提交表单后保持不变,例如,从而重新加载页面.

But I want it to do that only once. I want the image to remain the same after a visitor submits a form, for example, thereby reloading the page.

我尝试过if($ bg = NULL)等,但无济于事-我猜$ bg每次页面重新加载时都会再次为null.

I've tried if($bg=NULL) etc, but to no avail - I guess $bg becomes null again every time the page reloads.

非常感谢!

推荐答案

您可以使用会话变量 cookies 如果未设置,则选择背景,如果设置,您什么都不做

You can use Session variable or cookies if is not set, you choose a background, if is set, you do nothing

这篇关于仅选择一次随机值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆