Jump to content


Photo

[SOLVED] Random info applied to active page


  • Please log in to reply
10 replies to this topic

#1 Clarisse

Clarisse
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 30 August 2006 - 10:14 PM

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


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 31 August 2006 - 12:04 AM

User enters number
submit form
get random number
compare with user input
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 31 August 2006 - 12:48 AM

User enters number
submit form
get random number
compare with user input

Don't forget to unset the random number once approved.

#4 Clarisse

Clarisse
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 31 August 2006 - 02:23 PM

User enters number
submit form
get random number
compare with user input

Don't forget to unset the random number once approved.


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?




#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 31 August 2006 - 07:42 PM

This the approach I had in mind

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 Clarisse

Clarisse
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 01 September 2006 - 03:20 PM

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

#7 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 01 September 2006 - 04:52 PM

This the approach I had in mind

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

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'

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 01 September 2006 - 07:36 PM

I agree with you on the 'captcha' image bit but I was just trying to keep it simple while illustrating the code sequence.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 Clarisse

Clarisse
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 01 September 2006 - 10:04 PM

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.



#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 01 September 2006 - 10:44 PM

Save this as "simplecaptcha.php"
<?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);
?>

As you want a new random number each time I altered my original code slightly

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 Clarisse

Clarisse
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 07 September 2006 - 09:06 PM

Thank you for your help.

I ended up having to cache the information.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users