在不刷新整个页面的情况下刷新 PNG 文件的方法



使用图像字符串创建了一个验证码

imagestring($image, 5,  5, 30, $text, $text_color);
imagepng($image,"captcha_image.png"); 
imagepng($image,"captcha_image.png");

以上只是代码的一部分。

<img src="captcha_image.png" id="captchaimage"/>

上面的代码是我的 HTML 代码

如何在不按 F5 的情况下刷新我的 PNG 文件(验证码 + 会话事物(

通常我不会写这么多代码,因为 stackoverflow 不是编码服务,但在评论中看到代码的屏幕截图后,我确信您已经做出了公平的尝试,但方向错误,因此以下代码段是验证码应该如何使用 PHP 和 AJAX 的示例指南。

第一个文件是它被命名为captche_image.php的映像文件,并且应该分开,因为将对它进行 ajax 调用:

<?php
session_start();
function captche_generator()
{
function ct_rand($lenght=6)
{
$characters = '0123456789'; $tumble="";
for ($i=0; $i < $lenght ; $i++) {$tumble .= $characters[mt_rand(0, strlen($characters)-1)];} return $tumble;
}
//font file, font size, image text and save text to session
$fontfile   ='../fonts/JustMeAgainDownHere.ttf';
$fontsize   =50;
$text       =ct_rand();
$_SESSION['captche'] = $text;
//image size, backgroundcolor, transparent background, textcolor
$captche_image = imagecreate(180, 50);
$background_color = imagecolorallocate($captche_image, 255, 255, 255);
imagecolortransparent($captche_image, $background_color);
$text_color = imagecolorallocate($captche_image, 0, 0, 0);
//a loop to create scrambled line in image
for ($xy=0; $xy <=50 ; $xy++)
{ 
$x1= rand(1,1000);
$y1= rand(1,1000);
$x2= rand(1,100);
$y2= rand(1,100);
imageline($captche_image, $x1, $y1, $x2, $y2, $text_color);
}
//create image in .png extension
imagettftext($captche_image, $fontsize, 0, 15, 30, $text_color, $fontfile, $text);
imagepng($captche_image);
//set header and return created image
header("Content-type: image/png");
return $captche_image;
}
captche_generator();
?>

这个另一个文件应该是你的验证码页面,它是PHP和HTML的组合页面,我添加了最小的CSS来使其可见。

<?php
ob_start();
session_start();
if(isset($_GET["captche_input"]) && filter_var($_GET["captche_input"], FILTER_VALIDATE_INT))
{
if($_SESSION['captche'] === $_GET["captche_input"])
{
session_destroy();
ob_flush();
header("location:./login.php"); //redirect to the login page or any other page you wish
}
else
{
session_destroy();
ob_flush();
echo "<center><p style='padding: 5px;background-color:red;'> Code is Incorrect. Please try Again.</p></center>";
echo "<script type='text/javascript'> alert('Code is Incorrect. Please try Again.'); </script>";
}
}
?>
<!DOCTYPE html>
<html>
<style>
body{
background-image: url("../images/captche_bg.jpg");
background-repeat: no-repeat;
background-size: cover;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-position: right;
background-attachment: fixed;
}
.captcheBoard{
position: relative;
display: flex;
align-items: center;
flex-basis: 100%;
flex-direction: column;
margin-top: 15%;
text-align: center;
}
.captcheBack{
position: relative;
height: 90px;
width: 272px;
background-image: url('../images/captche_mini.jpg');
background-repeat: no-repeat;
background-size: 100%;
-webkit-background-size: center;
-moz-background-size: center;
-o-background-size: center;
background-position: center;
border: 0.10em solid coral;
border-radius: 0.03em;
}
.captcheFront{
position: relative;
margin-top: 8%;
}
.captcheInputBar{
position: relative;
margin: 3% 0%;
border: 0.10em solid coral;
border-radius: 0.03em;
font-size: 24px;
text-align: center;
}  
</style>
<body>
<div class="container captcheBoard">
<div class="captcheBack">
<div class="captcheFront"><!--captche image is shown here--></div>
</div>
<form action="" method="GET">
<input type="number" class="captcheInputBar" required name="captche_input" pattern="[0-9]{0,}" placeholder="Enter Captche Here" />
<br>
<input type="submit" class="Button" name="captche_check" value="Submit" />
</form>
<input type="button" class="Button" name="captche_refresh" value="Refresh" onclick="reload_captche()"/>
</div>
<script type="text/javascript">
function reload_captche()
{
var xhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhttp.open("POST", "./captche_image.php", true);
xhttp.send();
xhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
document.getElementsByClassName("captcheFront")[0].innerHTML = '<img src="./captche_image.php" />';
}
}
}
window.load = reload_captche();
</script>
</body>
</html>

注意:验证码成功后用户被重定向到的页面应该有一种方法来验证码是否正确输入,否则用户可以将自己重定向到所述页面。

正如LPK指出的那样,您必须通过JavaScript更改图像的来源,以便浏览器刷新它。

正如您在LPK答案的评论中指出的那样,我不确定为什么您会遇到问题,但也许您忘记在HTML中包含onclick属性。

下面是一个示例,其中包含一个代码段,该代码段显示了如何在计时器、图像单击和正在单击的锚元素上执行此操作。同样如LPK的回答所指出的,只需将src属性设置为相同的内容即可重新加载相同的图像。

const captchaImage = document.getElementById('captchaimage');
// Change the captcha image after 1 second.
setTimeout(() => {
captchaImage.src = 'http://placehold.it/125x125';

}, 1000);
// Change it on click.
captchaImage.onclick = () => {
captchaImage.src = 'http://placehold.it/200x200';

};
// Change it when another button clicked.
const testBtn = document.getElementById('testBtn');
testBtn.onclick = () => {
captchaImage.src = 'http://placehold.it/150x150';

};
a {
cursor: pointer;
background: #e5e5e5;
padding: 0.5rem;
}
<img src="http://placehold.it/100x100" id="captchaimage"/>
<a id='testBtn'>Click to change captcha image</a>

有关该主题的其他来源,请查看此链接到W3学校页面,您可以查看"自己尝试"链接以查看另一个实际示例。

简单的事情:

<img src="yoursource" id="captchaimage" onclick="actualiser()"/>

然后:

function actualiser() {
document.getElementById("captchaimage").src="yoursource"}

因此,当您单击图像时,它将仅重新加载图像(添加相同的源以始终重新加载相同的图像(

编辑

如果你不想点击,你也可以设置一个计时器,每x秒重新加载一次(需要另一行代码(

最新更新