我目前正在開發驗證碼系統。為此,我正在創建帶有一些基本數學問題的隨機圖片。用戶在提交表單之前必須給出正確的答案。這是代碼:<?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 為每個客戶端取出信號量。
- 1 回答
- 0 關注
- 150 瀏覽
添加回答
舉報
0/150
提交
取消