GD是一個非常流行的圖像處理庫,可以在PHP中使用,可用于創建、編輯、和操縱圖像。其中一個主要的應用就是生成圖像驗證碼。
MySQL是一個開源的關系型數據庫管理系統,是PHP最流行的數據庫模塊之一,可用于儲存和操作數據。
// 應用GD庫創建驗證碼圖片 $width = 100; $height = 40; $length = 4; $img = imagecreatetruecolor($width, $height); $bgColor = imagecolorallocate($img, rand(200,250), rand(200,250), rand(200,250)); imagefill($img, 0, 0, $bgColor); $codeSet = '0123456789'; $code = ''; for ($i = 0; $i< $length; $i++) { $code .= $codeSet[rand(0, strlen($codeSet)-1)]; } for ($i = 0; $i< rand(10, 20); $i++) { $lineColor = imagecolorallocate($img, rand(150,200), rand(150,200), rand(150,200)); imageline($img, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $lineColor); } for ($i = 0; $i< rand(4, 7); $i++) { $fontFile = './fonts/' . rand(1, 3) . '.ttf'; $fontSize = rand(18, 24); $x = $i * ($width / $length) + ($width / $length - $fontSize) / 2; $y = rand($height/2, $height - $fontSize); $fontColor = imagecolorallocate($img, rand(0,100), rand(0,100), rand(0,100)); $text = substr($code, $i, 1); imagettftext($img, $fontSize, rand(-10, 10), $x, $y, $fontColor, $fontFile, $text); } header('Content-Type:image/png'); imagepng($img); imagedestroy($img); // 連接和查詢數據庫 $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // 創建連接 $conn = mysqli_connect($servername, $username, $password, $dbname); // 檢查連接是否成功 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // 查詢數據 $sql = "SELECT * FROM Students WHERE Age >18"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) >0) { while($row = mysqli_fetch_assoc($result)) { echo "Name: " . $row["Name"]. " - Age: " . $row["Age"]. "
"; } } else { echo "0 results"; } mysqli_close($conn);