在 PHP 会话中预加载图像
我正在使用会话来跟踪用户数据。我有许多页面访问同一组图像。有没有办法将图像预加载到会话中,以便在会话的其余部分加载它们?
-麦克风
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一次加载图像后,浏览器会自动保留图像的副本,因此无需再次下载。
Browsers automatically keep a copy of images once they have been loaded the first time so they don't have to be downloaded again.
当脚本启动时,您可以运行以下命令:
然后,每当您需要访问图像集时,请使用
$_SESSION['images']
。When your script starts, you can run the following:
Then, use
$_SESSION['images']
whenever you need to access the set of images.为什么要在会话中保留图像?
您可以使用 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.
在登录/会话建立后的第一页上,只需将所有图像加载到隐藏的 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:
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.