Javascript 方法刷新PNG文件而不刷新整个页面

Javascript 方法刷新PNG文件而不刷新整个页面,javascript,php,html,Javascript,Php,Html,使用imagestring创建验证码简单的事情: <img src="yoursource" id="captchaimage" onclick="actualiser()"/> 因此,当您单击图像时,它将仅重新加载图像添加相同的源以始终重新加载相同的图像 编辑 如果不想单击,还可以设置一个计时器,每隔x秒重新加载一次,需要另一行代码简单的事情: <img src="yoursource" id="captchaimage" onclick="actualiser()"/&g

使用imagestring创建验证码简单的事情:

<img src="yoursource" id="captchaimage" onclick="actualiser()"/>
因此,当您单击图像时,它将仅重新加载图像添加相同的源以始终重新加载相同的图像

编辑

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

简单的事情:

<img src="yoursource" id="captchaimage" onclick="actualiser()"/>
因此,当您单击图像时,它将仅重新加载图像添加相同的源以始终重新加载相同的图像

编辑


如果你不想点击,你也可以设置一个计时器,每x秒重新加载一次,需要另一行代码,正如LPK所指出的,你必须通过JavaScript更改图像的来源,以便浏览器刷新它

我不知道你为什么会有问题,正如你在LPK的答案的评论中指出的那样,但是也许你忘记了在HTML中包含onclick属性

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

const captchaImage=document.getElementById'captchaImage'; //1秒后更改验证码图像。 设置超时=>{ captchaImage.src=http://placehold.it/125x125'; }, 1000; //单击即可更改它。 captchaImage.onclick==>{ captchaImage.src=http://placehold.it/200x200'; }; //当另一个按钮单击时更改它。 const testBtn=document.getElementById'testBtn'; testBtn.onclick==>{ captchaImage.src=http://placehold.it/150x150'; }; a{ 光标:指针; 背景:E5; 填充:0.5雷姆; }
单击以更改captcha图像正如LPK指出的,您必须通过JavaScript更改图像源,以便浏览器刷新图像

我不知道你为什么会有问题,正如你在LPK的答案的评论中指出的那样,但是也许你忘记了在HTML中包含onclick属性

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

const captchaImage=document.getElementById'captchaImage'; //1秒后更改验证码图像。 设置超时=>{ captchaImage.src=http://placehold.it/125x125'; }, 1000; //单击即可更改它。 captchaImage.onclick==>{ captchaImage.src=http://placehold.it/200x200'; }; //当另一个按钮单击时更改它。 const testBtn=document.getElementById'testBtn'; testBtn.onclick==>{ captchaImage.src=http://placehold.it/150x150'; }; a{ 光标:指针; 背景:E5; 填充:0.5雷姆; }
单击更改验证码图像通常我不会编写这么多代码,因为stackoverflow不是一个编码服务,但在评论中看到您的代码截图后,我确信您做了一个公平的尝试,但方向错误,因此下面的代码是验证码代码应如何使用PHP和阿贾克斯

第一个文件是名为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>

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

通常我不会写这么多代码,因为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>

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

JavaScript或Ajax可能是您所需要的。仅使用phpif是不可能的如果您使用JavaScript更改图像的src属性,那么浏览器应该尝试重新加载响应的iThanks,就像JavaScript上的任何diea一样?我已经尝试了document.getElementById'captchaimage'.src+'?'+新日期;但是没有luck@VoxVola分享您使用JavaScription所做的尝试您是否正在尝试制作字幕图像?如果是这样,请更好地回答这个问题,以获得更明确的答案JavaScript或Ajax可能是您所需要的。只有你,这是不可能的

使用JavaScript更改图像的src属性,然后浏览器应尝试重新加载响应的iThanks,就像JavaScript上的任何diea一样?我已经尝试了document.getElementById'captchaimage'.src+'?'+新日期;但是没有luck@VoxVola分享您使用JavaScription所做的尝试您是否正在尝试制作字幕图像?如果是这样,请更好地回答这个问题,以获得更明确的答案您好,谢谢您的代码,但是插入后,图像将不可读取。函数实现器{document.getElementByIdcaptchaimage.src=captcha_image.png}@LPK,这是代码您好,谢谢您的代码,但是插入后,图像无法读取。函数实现器{document.getElementByIdcaptchaimage.src=captcha_image.png}@LPK,这是代码<这是html代码<我的javascript代码,但在我运行它之后,按钮是可单击的,但不会更改任何内容。。由于我的文件的图像src是在我生成的文件中创建的<这是html代码<我的javascript代码,但是在我运行它之后,按钮是可单击的,但不会更改任何内容。。由于我的文件的图像src是在我生成的文件中创建的你好,感谢您花时间编写代码,我已经在我的网站上实现了代码,但不知怎么的,它似乎坏了,<我还包含了一些代码来使用字体'Arial.ttf',你知道哪里会出错吗?请注意:因为刷新按钮在图像中看不到,因为当前正在尝试首先执行验证码,如果它工作,则只执行刷新按钮。对于我添加的背景图像,并且不工作。你必须指定自己的字体文件,图像替换为您自己的图像位置参见更新的代码;更正了最初显示为脚本tagshello的样式标记,感谢您花时间编写代码,我已经在我的网站上实现了代码,但不知何故,它似乎已损坏,<我还包含了一些代码以使用“Arial.ttf”字体,你知道哪里会出错吗?请注意:因为刷新按钮在图像中看不到,因为当前正在尝试首先执行验证码,如果它工作,则只执行刷新按钮。对于我添加的背景图像,并且不工作。你必须指定自己的字体文件,图像替换为您自己的图像位置参见更新的代码;更正了最初显示为脚本标记的样式标记