在 PHP 会话中预加载图像

发布于 2024-09-13 12:20:59 字数 81 浏览 1 评论 0原文

我正在使用会话来跟踪用户数据。我有许多页面访问同一组图像。有没有办法将图像预加载到会话中,以便在会话的其余部分加载它们?

-麦克风

I am using sessions to keep track of user data. I have many pages that access the same set of images. Is there a way to preload the images into a session so that they are loaded for the rest of the session?

-Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丑疤怪 2024-09-20 12:20:59

第一次加载图像后,浏览器会自动保留图像的副本,因此无需再次下载。

Browsers automatically keep a copy of images once they have been loaded the first time so they don't have to be downloaded again.

时光清浅 2024-09-20 12:20:59

当脚本启动时,您可以运行以下命令:

if (!isset($_SESSION['images']))
{
  $_SESSION['images'] = get_my_images();
}

然后,每当您需要访问图像集时,请使用 $_SESSION['images']

When your script starts, you can run the following:

if (!isset($_SESSION['images']))
{
  $_SESSION['images'] = get_my_images();
}

Then, use $_SESSION['images'] whenever you need to access the set of images.

蹲在坟头点根烟 2024-09-20 12:20:59

为什么要在会话中保留图像?

您可以使用 lighttpd 或通过 CDN 提供图像等资源。

如果您正在谈论返回图像链接,您可以使用 memcached 等缓存链接。

why do you want to keep image in the session?

you can serve resources like images with lighttpd or via CDN.

if you are talking about returning image links, you can cache the links with memcached etc.

寻找一个思念的角度 2024-09-20 12:20:59

在登录/会话建立后的第一页上,只需将所有图像加载到隐藏的 div 中,此时浏览器将为您缓存它们:

<div style="display: none">
   <img src="img1.jpg" />
   <img src="img2.jpg" />
   ...
</div>

第一页加载时服务器上会出现点击,但是在浏览器缓存期间没有任何内容,或者最多是“304”检查更新类型请求之后。

On the first page after login/session establishment, just load all the images into a hidden div, at which point the browser will take care of cacheing them for you:

<div style="display: none">
   <img src="img1.jpg" />
   <img src="img2.jpg" />
   ...
</div>

There'll be a hit on the server for that first page load, but after either nothing for the duration of the browser cache, or at most a '304' check-if-newer type request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文