如何将验证码图片字符串保存到会话中以进行验证?

时间:2018-11-16 15:51:16

标签: php html html5

我当前正在处理一个注册表格,该表格需要验证码字符串才能进行下一步。我设法制作了一个验证码生成器,但不确定如何将生成的字符串保存为原始字符串,然后将其保存到用户会话中。

以下代码是验证码生成器的PHP代码

<?php
# Read background image
$image = ImageCreateFromPng ("captcha100x40.png");

# Randomise the text colour
$red = rand(80,130);
$green = rand(80,130);
$blue = 320 -$red - $green;
$textColour = ImageColorAllocate($image, $red, $green, $blue);

# Randomly select a character string
$charArray = array('a','b','c','d','e','f','g','h','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','T','U','V','W','X','Y','Z','2','3','4','6','7','8','9');
shuffle($charArray);
$captchaString = $charArray[0];
for ($i=1; $i<5; $i++) $captchaString .= ' ' . $charArray[$i];

# Edit the image
ImageString($image, 5, 10, 10, $captchaString, $textColour);

# Enlarge the image
$bigImage = imagecreatetruecolor(200, 80);
imagecopyresized($bigImage, $image, 0, 0, 0, 0, 200, 80, 100, 40);

# Output the image as a low quality JPEG
header("Content-Type: image/jpeg");
Imagejpeg($bigImage, NULL, 8);

# clean up
ImageDestroy($image);
ImageDestroy($bigImage);
?>

下面的代码是用于注册表的代码。

<?php

session_start();


$_SESSION['message'] == '';

$mysqli = new mysqli('*IPADDRESS*', '*USERNAME*', '*PASSWORD*',  '*DATABASENAME*');

 if ($_SERVER['REQUEST_METHOD'] == 'POST')   {

     //two passwords are equal to each other
     if ($_POST['password'] == $_POST['confirmpassword']) {

         $username = $mysqli->real_escape_string($_POST['username']);
         $email = $mysqli->real_escape_string($_POST['email']);
         $skills = $mysqli->real_escape_string($_POST['skills']);
         $password = md5($_POST['password']); //md5 hash password security

     }

 } 

?>

<!DOCTYPE html>
<html>
<head>
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="registrationForm.css">

  <style>body {
font-family: 'Roboto', sans-serif;
font-size: 20px;
}</style>


  <title>Registration Form</title>
</head>
<body>
<form class="form" action="registrationForm.php" method="post" style="border:1px solid #ccc">
    <div class="alert error"><?= $_SESSION['message'] ?></div>
  <div class="container">
    <h1>Registration</h1>
    <p>Please fill in this form to create an account.</p>
    <hr>

    <label for="username"><b>Username</b></label>
    <input type="text" placeholder="Enter Username" name="username" required>
    <br>

    <label for="email"><b>Email</b></label>
    <input type="text" placeholder="Enter Email" name="email" required>
    <br>

    <label for="password"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="password" required>
    <br>

    <label for="confirmpassword"><b>Repeat Password</b></label>
    <input type="password" placeholder="Repeat Password" name="confirmpassword" required>
    <br>

    <label for="skills"><b>Skills</b></label>
    <input type="text" placeholder="E.G., plumbing,teaching, programming, etc" name="skills" required>
    <br>


    <label for="captcha"><b>Complete CAPTCHA</b></label>
    <br>
    <div class="captcha">
<img src="captcha.php" alt="CAPTCHA image"/>
</div>
    <br>
      <input type="text" name=captcha required>


    <label>
      <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me
    </label>

    <p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms & Privacy</a>.</p>

    <div class="clearfix">
      <button type="button" class="cancelbtn">Cancel</button>
      <button type="submit" class="signupbtn">Sign Up</button>
    </div>
  </div>
</form>
</body>
</html>

0 个答案:

没有答案