Clarisse Posted August 30, 2006 Share Posted August 30, 2006 Hi,I have a code which gets a random number and that is to be matched by the user. It is working well but it compares the user entered number with a *new* random number when the page is submitted--not the one appearing on the screen. The user must enter the first number for a second time, after the page reloads an ignore the instructions new number has appeared. In order to get the number right on the first try the user has to be psychic and one step behind to get it correct any other time. Does anyone know of a way around this, to get the number to apply to the page on which it appears ?EXAMPLE-------------------------------------------Random Number 'A': 1234User Enters Number 'A': 1234FAILURE--page reloadsRandom Number 'B': 5678User Enters Number 'A': 1234SUCCESSRandom Number 'C': 0123User Enters Code 'B': 5678SUCCESSETC.... Quote Link to comment Share on other sites More sharing options...
Barand Posted August 31, 2006 Share Posted August 31, 2006 User enters numbersubmit formget random numbercompare with user input Quote Link to comment Share on other sites More sharing options...
Jenk Posted August 31, 2006 Share Posted August 31, 2006 [quote author=Barand link=topic=106321.msg425076#msg425076 date=1156982640]User enters numbersubmit formget random numbercompare with user input[/quote]Don't forget to unset the random number once approved. Quote Link to comment Share on other sites More sharing options...
Clarisse Posted August 31, 2006 Author Share Posted August 31, 2006 [quote author=Jenk link=topic=106321.msg425104#msg425104 date=1156985300][quote author=Barand link=topic=106321.msg425076#msg425076 date=1156982640]User enters numbersubmit formget random numbercompare with user input[/quote]Don't forget to unset the random number once approved.[/quote]Okay. I'm doing that--I think.However, the only way I can see to have the random number saved is to make a "useless" page that the user must pass through before getting to the page where the number needs to be entered by the user, verified, and unset. Is there any simpler way? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 31, 2006 Share Posted August 31, 2006 This the approach I had in mind[code]<?phpsession_start();if (isset($_POST['usernum'])) { if ($_SESSION['randomnumber']==$_POST['usernum']) { // process form data here echo "<p>Number OK<br>Processing</p>"; // redirect to other page echo "<p>Redirecting</p>"; exit; } else { echo "<p>Invalid input</p>"; }}else { //generate random code $_SESSION['randomnumber'] = rand(11111,99999); }echo "Enter this number : {$_SESSION['randomnumber']} ";?><FORM method='POST'>Code <input type="text" name="usernum" size="5"><br><input type="submit" name="submit" value="Submit"></FORM>[/code] Quote Link to comment Share on other sites More sharing options...
Clarisse Posted September 1, 2006 Author Share Posted September 1, 2006 Thank you, Barand. So I will need a 2nd page.I haven't gotten it going yet as I am trying to incorporated it into a pre-existing form--along with being burnt-out and weepy (one year tomorrow since I started working on this *#$!* site). Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 1, 2006 Share Posted September 1, 2006 [quote author=Barand link=topic=106321.msg425561#msg425561 date=1157053325]This the approach I had in mind[code]<?phpsession_start();if (isset($_POST['usernum'])) { if ($_SESSION['randomnumber']==$_POST['usernum']) { unset($_SESSION['randomnumber']); session_regenerate_id(true); // process form data here echo "<p>Number OK<br>Processing</p>"; // redirect to other page echo "<p>Redirecting</p>"; exit; } else { echo "<p>Invalid input</p>"; }}else { //generate random code $_SESSION['randomnumber'] = rand(11111,99999); }echo "Enter this number : {$_SESSION['randomnumber']} ";?><FORM method='POST'>Code <input type="text" name="usernum" size="5"><br><input type="submit" name="submit" value="Submit"></FORM>[/code][/quote]added unset() and session_regenerate_id()and simply echoing it is a bad idea.. spam bots can just 'copy and paste' it, which is why most captcha's use images - but I get that the above is just 'pseudo' Quote Link to comment Share on other sites More sharing options...
Barand Posted September 1, 2006 Share Posted September 1, 2006 I agree with you on the 'captcha' image bit but I was just trying to keep it simple while illustrating the code sequence. Quote Link to comment Share on other sites More sharing options...
Clarisse Posted September 1, 2006 Author Share Posted September 1, 2006 I still can't get it to work. ???I know that my method of image/code creation as well the confirmation is working but I can't seem to get them working with the Page-A to Page-B info transfer. The form on which the codes are collected is made up of a PHP script with a HTML/tpl file.Could someone please tell me if I am missing a step. =================1. User arrives at Page-A2. Page-A processes Page-B that includes a script that unsets any earlier code and randomly selects an image and a code from db3. Image is displayed and the code from Page-B is posted onto Page-A 4. User enters code 5. If both user-entered code and posted code from Page-B match, page is proccesed 6. If user-entered code and posted code from Page-B do not match page is reloaded which reprocesses Page-B making it create a new code and image. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 1, 2006 Share Posted September 1, 2006 Save this as "simplecaptcha.php"[code]<?php$im = imagecreate(100, 25);$num = $_GET['n'];$bg = imagecolorallocate($im, 0xE0, 0xE0, 0xE0);$red = imagecolorallocate($im, 0xFF, 0x00, 0x00);for ($i=0; $i<5; $i++) { $fn = rand(1,5); $c = $num{$i}; $x = 15*($i+1); $y = 6 + 2 - rand(0,4); imagestring($im, $fn, $x, $y, $c, $red);}header("content-type: image/png");imagepng ($im);imagedestroy($im);?>[/code]As you want a new random number each time I altered my original code slightly[code]<?phpsession_start();if (isset($_POST['usernum'])) { if ($_SESSION['randomnumber']==$_POST['usernum']) { // process form data here echo "<p>Number OK<br>Processing</p>"; // redirect to another page to continue echo "<p>Redirecting</p>"; exit; } else { echo "<p>Invalid input</p>"; }}$n = rand(11111,99999);$_SESSION['randomnumber'] = $n;echo "<img src='simplecaptcha.php?n=$n'>";?><FORM method='POST'>Enter above code <input type="text" name="usernum" size="5"><br><input type="submit" name="submit" value="Submit"></FORM>[/code] Quote Link to comment Share on other sites More sharing options...
Clarisse Posted September 7, 2006 Author Share Posted September 7, 2006 Thank you for your help.I ended up having to cache the information. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.