user254197
user254197

Reputation: 881

Captchapng nodejs

I got this example working(at least I can see the image), but how can I check against the users input ?When I send user innput to the server against what should I check ?

This what I got so far:

    router.get('/captcha.png', function (req, res, next) {
    // res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);
    var captchapng = require('captchapng');

    var p = new captchapng(80, 30, parseInt(Math.random() * 9000 + 1000)); // width,height,numeric captcha
    p.color(0, 0, 0, 0);  // First color: background (red, green, blue, alpha)
    p.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)

    var img = p.getBase64();
    var imgbase64 = new Buffer(img, 'base64');
    res.writeHead(200, {
        'Content-Type': 'image/png'
    });
    res.end(imgbase64);

});

Upvotes: 0

Views: 508

Answers (1)

Kenneth Cheung
Kenneth Cheung

Reputation: 63

Set a global variable "captchaNum", change the code as below, and you are able to check the user's input by "captchaNum". I hope it works.

  router.get('/captcha.png', function (req, res, next) {
  // res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);
  var captchapng = require('captchapng');



  //---------changed--------
  captchaNum = parseInt(Math.random() * 9000 + 1000);
  var p = new captchapng(80, 30, captchaNum); // width,height,numeric captcha
  //------------------------

  p.color(0, 0, 0, 0);  // First color: background (red, green, blue, alpha)
  p.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)
  var img = p.getBase64();
  var imgbase64 = new Buffer(img, 'base64');
  res.writeHead(200, {
    'Content-Type': 'image/png'
  });
  res.end(imgbase64);

});

Upvotes: 1

Related Questions