Security type code...

CannonBallGuyCannonBallGuy ModeratorShared Hoster
Basically, somone enters two (1 digit) numbers in separate fields (call them A and B). Then they choose to add, minus, times or divide these 2 numbers.
The add, minus, times and divide each have a code number (call this C).
The code number chosen is multiplied by one of 3 other numbers chosen at random (call this M).
Then this is displayed on the screen like this:
A (C times M) B
So, if you enter the numbers 3 and 7 and you choose times (*) for which the code number is 2. The other number (M) chosen is 2. 5 times 2 = 8
So this is displayed:
3 8 7
Assuming you understand this...
How can I do it in PHP?

I may be able to offer some DDF$... ;)

Comments

  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    It's quite simple, I'll get something out for you when I get off work tonight.

    No need for your forum $$$ :)

    Two questions though:
    1. Does the user need to chose M, or is M randomly selected by the script?
    2. What are the code numbers (C) for the +, -, * and \ ?
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    CBG,

    Attached is a single PHP that you can upload directly to your server to test by calling through your browser. I've tested it and it works but I may not have understood your requirements correctly...

    What I have done is:
    a. User keys in 1st variable
    b. User keys in 2nd variable
    c. User selects operator - "+, -, * or /". I've set + to be 1, - to be 2, * to be 3 and / to be 4
    d. User selects 1 of 3 "random" numbers
    e. User clicks submit

    The following will be displayed:
    a c*d b

    Regards.
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    i just realized that i forgot to remove one redundant line in the script - the srand statement... you can either remove it or just leave it in there (it won't cause any harm)...
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Ok. What you have done works awesome. Thanks.
    But I would like the number M or you called it d, to be chosen randomly...

    And then, if the code could be sent in an email as oppossed to being displyed on screen?
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    will work on it and post it up the next time i login... it's kinda late now where i am... :)
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Thats great.
    Thanks loads!! :)
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    Alrite CBG, here goes...

    Copy everything below and paste it into a file ending with .php - it now uses a PHP generated random number instead of relying on the user to select a number...

    Appreciate you can tell me if it's OK in testing (I've tested it myself) ...

    ============================================




    <html>

    <head>
    <title>Fun with alphabets and numbers</title>
    </head>

    <body>

    <?php

    ////////////////////////////////////////////////////////////////
    // form design portion
    // variable $form1 is used to store design
    ////////////////////////////////////////////////////////////////

    $form1 = "
    <form name=\"input\" action=\"rand.php\" method=\"post\">
    <input type=\"hidden\" name=\"seen\" value=\"y\"><br>

    <b>First Variable:</b><br>
    First Variable: <input type=\"text\" name=\"firstvar\" value=\"$firstvar\"><br>
    <br>
    <b>Second Variable:</b><br>
    Second Variable: <input type=\"text\" name=\"secondvar\" value=\"$secondvar\"><br>

    <br>

    <b>Select Operator:</b><br>
    <select name=\"ops\">
    <option value=\"1\">+</option>
    <option value=\"2\">-</option>
    <option value=\"3\">*</option>
    <option value=\"4\">/</option>
    </select>

    <br><br><br>

    <input type =\"submit\" value =\"Submit!\">
    <input type =\"reset\" value =\"Reset!\">

    </form>";

    ////////////////////////////////////////////////////////////////
    // logic portion
    ////////////////////////////////////////////////////////////////

    if($seen != "y") {
    print "$form1";
    }
    else {
    srand((double)microtime()*1000000);
    print "$firstvar" . " " . ($ops * rand()) . " " . "$secondvar";
    }

    ?>


    </body>
    </html>
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    A few things before I test this...
    I'de like it to choose a random number out of 3 that are defined in the script...Not just 3 random number... I didn't make that clear...

    And then it sends the little number code thing out in an email... It shouldn't display it on screen at all....
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Your going to change the script so it will do that?
    Or does the script already do that?
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    hey cbg,

    sorry for being missing in action for the past month - i had to juggle time between my mba exams and my project at work.

    in any event, the script doesn't do what you require. do you still need it? just reply and i'll get it going for you.

    once again, sorry for not closing this off before i disappeared.

    regards.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    hey cbg,

    finally managed to squeeze some time to look into this...

    1. copy everything into a file named rand.php - if you want to change this file name, you'll have to reflect the change in the script too - only 1 instance
    2. the codes for +,-,*,/ is defined in the script - it's easy for you to change the values - currently, i've used "1" for +, "2" for -, "3" for *, and "4" for /
    3. the 3 random numbers are also defined in the script - it's easy for you to change the values - currently, i've used $randomno1 = 7, $randomno2 = 8 and $randomno3 = 9
    4. the script will send the results by email to a specified address - you'll have to change 3 variables in the script - $to = "recipient@domain.com", $subject = "The Subject" and $from = "From:sender@domain.com";

    it should work fine. do give me a shout if you need anything changed - i won't be able to respond immediately as i don't drop by everyday... :)

    <html>
    
    <head>
       <title>Fun with alphabets and numbers</title>
    </head>
    
    <body>
    
    <?php
    
    ////////////////////////////////////////////////////////////////
    // form design portion
    // variable $form1 is used to store design
    ////////////////////////////////////////////////////////////////
    
    $form1 = "
    <form name=\"input\" action=\"rand.php\" method=\"post\">
    <input type=\"hidden\" name=\"seen\" value=\"y\"><br>
    
    <b>First Variable:</b><br>
    First Variable: <input type=\"text\" name=\"firstvar\" value=\"$firstvar\"><br>
    <br>
    <b>Second Variable:</b><br>
    Second Variable: <input type=\"text\" name=\"secondvar\" value=\"$secondvar\"><br>
    
    <br>
    
    <b>Select Operator:</b><br>
    <select name=\"ops\">
    <option value=\"1\">+</option>
    <option value=\"2\">-</option>
    <option value=\"3\">*</option>
    <option value=\"4\">/</option>
    </select>
    
    <br><br><br>
    
    <input type =\"submit\" value =\"Submit!\">
    <input type =\"reset\" value =\"Reset!\">
    
    </form>";
    
    ////////////////////////////////////////////////////////////////
    // logic portion
    ////////////////////////////////////////////////////////////////
    
    if($seen != "y") {
       print "$form1";
    }
    else {
    
       // admin defined random numbers
       $randomno1 = 7; 
       $randomno2 = 8;
       $randomno3 = 9;
    
       // send email
       $to = "recipient@domain.com";
       $subject = "The Subject";
       $from = "From:sender@domain.com";
    
       // renerate random number
       srand((double)microtime()*1000000);
       $systemrandomno = rand();
    
       if ($systemrandomno % 3 == 0) {
          $emailcontent = "$firstvar" . " " . ($ops * $randomno1) . " " . "$secondvar";
       }
       else {
          if ($systemrandomno % 3 == 1) {
             $emailcontent = "$firstvar" . " " . ($ops * $randomno2) . " " . "$secondvar";
          }
          else {
             $emailcontent = "$firstvar" . " " . ($ops * $randomno3) . " " . "$secondvar";
          }
       }
    
       // send email out
       mail($to, $subject, $emailcontent, $from);
    }
    
    ?>
    
    
    </body>
    </html>
    
    
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Thanks very much!!
    I think its working but theres a few things...
    1. Does the from email address need to be real or can I just put anything there?
    2. Can you make a quick change so that if everything goes well it will say so?

    Cheers!
    CBG
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    cbg,

    in the mail command,
    1. $to needs to be a real address - otherwise the email doesn't get sent
    2. $from can be a fake address

    3. to tell the user that everything went well, just add in the following line (including the ";") after the mail($to, $subject, $emailcontent, $from); command.

    print "everything went well. email sent.";
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    okay. cheers.
    :)
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    I hate to say it, but it doesn't seem to be working.
    I get the "Everything worked" message but I ahve tried loads of times and have recieved no emails. I have tried with 2 different email addresses too.

    I changed what you said and this is the code I'm using:

    [php]<html>

    <head>
    <title>Fun with alphabets and numbers</title>
    </head>

    <body>

    <?php

    ////////////////////////////////////////////////////////////////
    // form design portion
    // variable $form1 is used to store design
    ////////////////////////////////////////////////////////////////

    $form1 = "
    <form name=\"input\" action=\"rand.php\" method=\"post\">
    <input type=\"hidden\" name=\"seen\" value=\"y\"><br>

    <br>
    A
    <br>

    <b>First Variable:</b><br>
    First Variable: <input type=\"text\" name=\"firstvar\" value=\"$firstvar\"><br>
    <br>
    <b>Second Variable:</b><br>
    Second Variable: <input type=\"text\" name=\"secondvar\" value=\"$secondvar\"><br>

    <br>

    <b>Select Operator:</b><br>
    <select name=\"ops\">
    <option value=\"1\">+</option>
    <option value=\"2\">-</option>
    <option value=\"3\">*</option>
    <option value=\"4\">/</option>
    </select>

    <br><br><br>

    <input type =\"submit\" value =\"Submit!\">
    <input type =\"reset\" value =\"Reset!\">

    </form>";

    ////////////////////////////////////////////////////////////////
    // logic portion
    ////////////////////////////////////////////////////////////////

    if($seen != "y") {
    print "$form1";
    }
    else {

    // admin defined random numbers
    $randomno1 = 7;
    $randomno2 = 8;
    $randomno3 = 9;

    // send email
    $to = "cannonballguy@hotmail.com";
    $subject = "xxx security code test xxx";
    $from = "cannonballguy@hotmail.com";

    // renerate random number
    srand((double)microtime()*1000000);
    $systemrandomno = rand();

    if ($systemrandomno % 3 == 0) {
    $emailcontent = "$firstvar" . " " . ($ops * $randomno1) . " " . "$secondvar";
    }
    else {
    if ($systemrandomno % 3 == 1) {
    $emailcontent = "$firstvar" . " " . ($ops * $randomno2) . " " . "$secondvar";
    }
    else {
    $emailcontent = "$firstvar" . " " . ($ops * $randomno3) . " " . "$secondvar";
    }
    }

    // send email out
    mail($to, $subject, $emailcontent, $from);
    print "everything went well. email sent.";
    }

    ?>


    </body>
    </html>[/php]
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    cbg,

    i copied whatever you had in your post and just chaned $to to my own email address... then i loaded it up to my host... and it worked - no problems at all. i dun know why it didn't work for you. can someone else try it out at their server too?

    also, if you can try replacing:
    1. mail($to, $subject, $emailcontent, $from);
    with
    2. print $emailcontent;
    and try it out. instead of sending an email, the contents of the email will be printed to the screen. if this works, then i suspect something is wrong with your server running the mail command...
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    cbg, not sure how to proceed with this as it works fine on my side... i do hope to close it off properly though...
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    The print $mailcontent works fine...
    But still no emails.
    I have no idea what is up....
    If it is my server not running the mail command right, what can I do about it?
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    you have:
    $to = "cannonballguy@hotmail.com";
    $subject = "xxx security code test xxx";
    $from = "cannonballguy@hotmail.com";

    is cannonballguy@hotmail.com a valid email address? can you try using different values for $to and $from?
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    That is a valid email address...
    I ahve tried other addresses with no luck... :(
Sign In or Register to comment.