Jump to content

[SOLVED] Random info applied to active page


Clarisse

Recommended Posts

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': 1234
User Enters Number 'A': 1234
FAILURE--page reloads

Random Number 'B': 5678
User Enters Number 'A': 1234
SUCCESS

Random Number 'C': 0123
User Enters Code 'B': 5678
SUCCESS

ETC....
Link to comment
Share on other sites

[quote author=Jenk link=topic=106321.msg425104#msg425104 date=1156985300]
[quote author=Barand link=topic=106321.msg425076#msg425076 date=1156982640]
User enters number
submit form
get random number
compare 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?


Link to comment
Share on other sites

This the approach I had in mind

[code]
<?php
session_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]
Link to comment
Share on other sites

[quote author=Barand link=topic=106321.msg425561#msg425561 date=1157053325]
This the approach I had in mind

[code]
<?php
session_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'
Link to comment
Share on other sites

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

2. Page-A processes Page-B that includes a script that unsets any earlier code and randomly selects an image and a code from db

3. 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.

Link to comment
Share on other sites

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]<?php
session_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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.