为了账号安全,请及时绑定邮箱和手机立即绑定

同时为多个用户保存文件

同时为多个用户保存文件

PHP
婷婷同学_ 2023-12-15 16:10:21
我目前正在开发验证码系统。为此,我正在创建带有一些基本数学问题的随机图片。用户在提交表单之前必须给出正确的答案。这是代码:<?php    function randExer() {        //Creating random (simple) math problem        $arr = array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten");        $item1 = $arr[array_rand($arr)];        $item2 = $arr[array_rand($arr)];        $random = $item1 . " + " . $item2;        //Saving created math problem for later        file_put_contents("../sites/exercise.txt", $random);        //Creates a black picture with width=200 and height = 50        $img = imagecreatetruecolor(200, 50);        //uses RGB-values to create a useable color        $white = imagecolorallocate($img, 255, 255, 255);        $silver = imagecolorallocate($img, 192, 192, 192);                //Adds random lines to the images        for($i = 0; $i < 5; $i++) {            imagesetthickness($img, rand(1, 3));            $x1 = rand(0, 100);            $y1 = rand(0, 25);            $x2 = $x1 + rand(0, 100);            $y2 = $y1 + rand(0, 25);            imageline($img, $x1, $x2, $x2, $y2, $silver);        }        //Adds white-colored text        $var = imagestring($img, 5, 18, 18, $random . " = ?", $white);        $rotate = imagerotate($img, 10, 0);        //Save image        imagejpeg($rotate, "../sites/exercise.png", -1);        };?>正如我在代码审查中的问题的评论中所指出的,如果超过一个,系统将无法工作客户端同时使用表单,因为图像将保存在同一个地方。如何在不使用不同文件名保存每张图片的情况下解决这个问题(我真的不希望我的服务器充满图像)?
查看完整描述

1 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

有几种方法可以处理这个问题。

您可以为每个用户创建一个唯一的文件名 - 即每个地址和端口,使用 $_SERVER['REMOTE_ADDR'] 和 $_SERVER['REMOTE_PORT'] 变量来创建文件名(可能沿着随着时间)。

注意:如果套接字关闭,端口将会更改,因此如果您的客户端页面没有通过 js 保持套接字打开,您将需要维护一个会话 ID。会话 ID 需要在服务器端生成并存储在客户端,直到客户端准备好将其与验证一起返回。

您可以让 php 为每个客户端取出信号量。


查看完整回答
反对 回复 2023-12-15
  • 1 回答
  • 0 关注
  • 53 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信