无需刷新整个页面即可刷新PNG文件的方法 [英] Method to refresh PNG file without refreshing the whole page

查看:76
本文介绍了无需刷新整个页面即可刷新PNG文件的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用imagestring创建验证码

Created a captcha using imagestring

推荐答案

通常我不会编写这么多代码,因为stackoverflow不是编码服务但是在评论中看到你的代码的截图后,我确信你做了一个公平的尝试,但是方向错误,因此下面的代码是一个示例指导,说明了如何使用PHP和AJAX。

Normally i won't write this much code as stackoverflow isn't a coding service but after seeing screenshots of your code in the comments am convinced you have made a fair attempt but in the wrong direction hence the following piece of code is an example guide line of how a captche code should be using PHP and AJAX.

第一个文件是名为 captche_image.php 的图像文件,应该是单独的,因为将对它进行ajax调用:

This first file is the image file it is named captche_image.php and should be separate as ajax calls will be made to it:

<?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();

?>

这个其他文件应该是你的captche页面,它是PHP和HTML的组合页面,我添加了最少的CSS使其可见。

This other file should be your captche page it a combined page of PHP and HTML and i have added minimal CSS to make it visible.

    <?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>

注意:在captche成功时用户被重定向到的页面应该有一种验证方式已经正确输入了captche,否则用户可以将自己重定向到所述页面。

Note: The page which the user is been redirected to on success of captche should have a way of verifying that the captche was entered correctly else user can just redirect herself to said page.

这篇关于无需刷新整个页面即可刷新PNG文件的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆