Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 10-23-2010, 03:48 PM rand() problem
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
PHP Code:
<?php
function verification($foo){
    
$x rand(1,10);
    
$y rand(1,10);
    
    if(
$foo == 'question'){
        echo 
"$x + $y";
    }
    
    if(
$foo == 'answer'){
        echo 
$x $y;
    }
}
?>
$x and $y are different with each usage. Why?
__________________
Think in code; Dream in digital.

<?php if($helpfull == true){ $talkupation++; } ?>
moatist is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-23-2010, 04:29 PM Re: rand() problem
Crimson's Avatar
Skilled Talker

Posts: 56
Name: Connor
Location: United States
Trades: 0
Looking at the code, it appears you have to call this function twice, once to output the question and once to output the answer. The problem is that the first two lines of code...

Code:
$x = rand(1,10);
$y = rand(1,10);
...run both times, both for the question and the answer. So when you call verification('answer'), a new set of random numbers is generated.

I can think of two solutions: You could rework the verification script so that it only needs to call this function once. Or you could store $x and $y in global scope, and move the rand() calls inside the if($foo == 'question'){...} block. (This assumes both functions calls are done by the same script on the same page--if that's not true, you'll need to work with sessions.)

However, a much simpler (and more secure) option would be to not write this code yourself in the first place. I'm not sure what you're trying to verify, but Google's reCAPTCHA service might work for you: http://www.google.com/recaptcha
__________________

Please login or register to view this content. Registration is FREE

I solve code problems, browser compatibility (including IE 6), Wordpress trouble, the works.

Last edited by Crimson; 10-23-2010 at 04:32 PM..
Crimson is offline
Reply With Quote
View Public Profile Visit Crimson's homepage!
 
Old 10-23-2010, 07:02 PM Re: rand() problem
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
I went the global path and now the script works perfectly. Thanks!
__________________
Think in code; Dream in digital.

<?php if($helpfull == true){ $talkupation++; } ?>
moatist is offline
Reply With Quote
View Public Profile
 
Old 10-25-2010, 02:56 AM Re: rand() problem
Junior Talker

Posts: 3
Name: SydneySuperyacht Services
Trades: 0
thanks for script....
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
sydneysuperyach is offline
Reply With Quote
View Public Profile Visit sydneysuperyach's homepage!
 
Old 10-25-2010, 05:27 AM Re: rand() problem
Banned

Posts: 408
Name: mushget
Trades: 0
Code:
<?php 
$x = 0;
$y = 0;
function verification($foo){ 
       
    if($foo == 'question')
{ 
  $x = rand(1,10); 
    $y = rand(1,10); 

        echo "$x + $y"; 
    } 
     
    if($foo == 'answer'){ 
        echo $x + $y; 
    } 
} 
?> 
mushget is offline
Reply With Quote
View Public Profile Visit mushget's homepage!
 
Old 10-25-2010, 02:01 PM Re: rand() problem
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
I ended up with the following code:

PHP Code:
<?php
$x 
rand(1,10);
$y rand(1,10);

function 
verification($foo){
    global 
$x$y;
    if(
$foo == 'question'){
        echo 
"$x + $y";
    }
    
    if(
$foo == 'answer'){
        echo 
$x $y;
    }
}
?>
__________________
Think in code; Dream in digital.

<?php if($helpfull == true){ $talkupation++; } ?>
moatist is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to rand() problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.71229 seconds with 12 queries