How to save captcha image string onto the session for validation?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am currently working on a registration form that will require a captcha string validation in order to proceed to the next step. I have managed to make a working captcha generator but I am unsure as how to save the generated string as it's own and then saving it onto the users sessions.



The following code is the PHP code for the captcha generator



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


The following code under is the code used for the registration form.



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









share|improve this question































    0















    I am currently working on a registration form that will require a captcha string validation in order to proceed to the next step. I have managed to make a working captcha generator but I am unsure as how to save the generated string as it's own and then saving it onto the users sessions.



    The following code is the PHP code for the captcha generator



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


    The following code under is the code used for the registration form.



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









    share|improve this question



























      0












      0








      0








      I am currently working on a registration form that will require a captcha string validation in order to proceed to the next step. I have managed to make a working captcha generator but I am unsure as how to save the generated string as it's own and then saving it onto the users sessions.



      The following code is the PHP code for the captcha generator



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


      The following code under is the code used for the registration form.



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









      share|improve this question
















      I am currently working on a registration form that will require a captcha string validation in order to proceed to the next step. I have managed to make a working captcha generator but I am unsure as how to save the generated string as it's own and then saving it onto the users sessions.



      The following code is the PHP code for the captcha generator



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


      The following code under is the code used for the registration form.



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






      php html html5






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 21:25







      zeearebee

















      asked Nov 16 '18 at 15:51









      zeearebeezeearebee

      75




      75
























          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341261%2fhow-to-save-captcha-image-string-onto-the-session-for-validation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341261%2fhow-to-save-captcha-image-string-onto-the-session-for-validation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python