Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make an Image random with php 2

Status
Not open for further replies.

egmweb

Programmer
Mar 19, 2004
147
EU
Hello all,

How can I make an script to generate a random picture with numbers inside and with a form check enter the numbers of this picture and let me continue?

For example:

I enter to a site and appear a picture with numbers which I have to introduce in a text fiend to continue to the next page..

Thanks you
 
Hi,
This is very simple!

1. Generate a random string and store it in a session-variable
2. Write that string to an temp image
3. Display that image to the user
4. Use validation, to check if $_POST['secret_pass'] == $_SESSION['secret_pass']

Ps. if not, regenerate the secret pass!

You can also make some confusing things, so it's even harder for a computer to scan the image.

Olav Alexander Mjelde
Admin & Webmaster
 
Hello DaButcher thanks a lot. But, please would you be more detailed?

Thanks you very much

Eduard
 
foo.php
Code:
<?php
session_start();
if (isset($submit)) {
  if ($submit == "logout") {
      unset($_SESSION['key']);
    }
  }

if ($_SESSION['key'] != $_POST['key'] || (!($_SESSION['key']))) {
  $_SESSION['key'] = substr(md5(time()), 0, 6);
  echo " 
  <form action=\"\" method=\"post\">
  <img src=\"test.php\" />
  <input type=\"text\" name=\"key\" />
  <input type=\"submit\" name=\"submit\" value=\"submit\" />
  </form>";
  }
else {
  echo " 
  <form action=\"\" method=\"post\">
  Access OK
  <input type=\"submit\" name=\"submit\" value=\"logout\" />
  </form>";
  }
?>

test.php
Code:
<?php
session_start();

if ($_SESSION['key']) {
  header("Content-type: image/png");
  $string = $_SESSION['key'];
  $im    = imagecreatefrompng("images/bg.png");
  $orange = imagecolorallocate($im, 220, 210, 60);
  $px    = (imagesx($im) - 7.5 * strlen($string)) / 2;
  $h = (imagesy($im) - 7.5) / 2;
  imagestring($im, 3, $px, $h, $string, $orange);
  imagepng($im);
  imagedestroy($im);
  }
?>

you then need an image in images/

working example:

ps. this is just a framework..

you owe me some stars for this :p as I made it dedicated for you!

Olav Alexander Mjelde
Admin & Webmaster
 
Hello all.

Thanks you Very much!! I will try to understand that to make an code like this by myself...

Thanks you again!!

Eduard
 
egarc1a:
You can use the code I provided above..
eg. it will be like a login-system for your registration-system.

simply add your current registrationsystem inside there where it says: "Access OK".

remove the form there, and put:
require("yourpage.php"); where yourpage.php is your page :p

ps it will only work if you dont use header() or something like that in your page!

if you do, you might want to simply integrate both of the scripts together.

you might also want some alternative for the visual impaired, as they can not see images! You can not show it in text, as then the bots can simply look at the source.

You could however make an alt tag, where you ask the people to mail you, if they are having problems with this system.

ps. you can also add in the <form action:
?<?=SID?>

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top