php - Why does the variable $token5 change 3 times when I run this script? -
attempting email address confirmation script. attempt contains 3 files: (email_token_test.php)
require_once (gen_email_token.php) require (phpmailer-master/phpmailerautoload.php)
i expecting variable $token5 contain 5 character string , string remain same through whole script. best of limited knowledge calling token generation function 1 time, doing several var_dumps on $token5 shows $token5 changing script progresses.
i know standard way of email address confirmation user registeration , database population, in instance user aren't signing in or registering. submitting email address quote request.
what doing wrong , how correct it? [php]
require_once "random_compat-2.0.10/lib/random.php"; include "password_compat-1.0.4/lib/password.php"; function generateemailtoken() { try { $string = random_bytes(32); } catch (typeerror $e) { // well, it's integer, unexpected. die("an unexpected error has occurred"); } catch (error $e) { // unexpected because 32 reasonable integer. die("an unexpected error has occurred"); } catch (exception $e) { // if message, csprng failed hard. die("could not generate random string. our os secure?"); } $myrandomstring = (bin2hex($string)); if($myrandomstring = (bin2hex($string))){ $randomtoken = password_hash($myrandomstring, password_bcrypt); //return $randomtoken.'<br>'; } if($randomtoken = password_hash($myrandomstring, password_bcrypt)){ $newrandomtoken = substr($randomtoken, 7); //return $newrandomtoken.'<br>'; } global $token5; if($newrandomtoken = substr($randomtoken, 7)){ $token5 = substr($newrandomtoken, -5); //return $token5.'<br>'; } if($token5 = substr($newrandomtoken, -5)){ return $token5; } } require_once "gen_email_token.php"; require "phpmailer-master/phpmailerautoload.php"; global $token5; global $newemailtoken; $newemailtoken = generateemailtoken(); var_dump($token5); if(isset($_post['submit'])){ print_r($_post); $postedemail = null; if($_post['req-email']){ $postedemail = $_post['req-email']; } if(strlen($postedemail) === 0){ $errors[] = "you must enter email address."; } if(strlen($postedemail)<3){ $errors[] = "you have entered invalid email address."; } // make sure "from" email address doesn't have nasty stuff in $pattern = '/^(?!(?:(?:\x22?\x5c[\x00-\x7e]\x22?)|(?:\x22?[^\x5c\x22]\x22?)){255,})(?!(?:(?:\x22?\x5c[\x00-\x7e]\x22?)|(?:\x22?[^\x5c\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2a\x2b\x2d\x2f-\x39\x3d\x3f\x5e-\x7e]+)|(?:\x22(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|(?:\x5c[\x00-\x7f]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2a\x2b\x2d\x2f-\x39\x3d\x3f\x5e-\x7e]+)|(?:\x22(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|(?:\x5c[\x00-\x7f]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:ipv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:ipv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/id'; if (preg_match($pattern, trim(strip_tags($postedemail)))) { $cleanedemail = trim(strip_tags($postedemail)); //echo '<span style="color:green;">success'.' '.'</span>'.'<b>'.$cleanedemail.'</b>'.'<span style="color:green;">'.' '.'is valid email.'.'<br>'.'</span>'; } else { $errors[] = "the email address entered invalid. please try again!"; } $email = $cleanedemail; if(!empty($errors)){ echo '<h1>error(s)!</h1>'; foreach($errors $errormessage){ echo '<span style="color:red;">'.$errormessage .'</span>'; } } if(empty($errors)){ $message = '<html><body>'; $message .= '<img src="http://res.cloudinary.com/dx1tairmq/image/upload/v1499127233/miles%20away%20travel/newmatlogo_small.png" alt="miles away travel">'; $message .= 'miles away travel has sent email verification code.'; $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">'; $message .= "<tr><td><strong> verification code: </strong></td><td>" . $token5 ."</td></tr>"; $message .= "</table>"; $message .= '<p>please enter code in email verification form</p>'; $message .= "</body></html>"; $mail = new phpmailer; //$mail->smtpdebug = 1; // enable verbose debug output $mail->issmtp(); // set mailer use smtp $mail->host = 'smtp.gmail.com'; // specify main , backup smtp servers $mail->smtpauth = true; $mail->username = 'xxxxxxxxx@gmail.com'; // set gmail email id $mail->password = 'password'; // gmail email password $mail->smtpsecure = 'tls'; // enable tls encryption, `ssl` accepted $mail->port = 587; // tcp port connect $mail->setfrom('kentest894@gmail.com', 'miles away travel'); $mail->addaddress($email, 'ken smith'); // add recipient //$mail->addaddress('kenneth894@hotmail.com' , 'k-man'); // name optional $mail->addreplyto('friend_one_email_addess', 'information'); $mail->ishtml(true); // set email format html $mail->subject = 'email verification code'; $mail->body = $message; $mail->altbody = 'this body in plain text non-html mail clients'; if(!$mail->send()) { echo 'message not sent.'; echo 'mailer error: ' . $mail->errorinfo; } else { echo 'message has been sent'.'<br>'; } var_dump($token5); function buildform(){ echo '<h3>your email address must verified quote submission.</h3> <form name="e-verify" method="post" action="email_token_test.php" id="form2"> <h3>email verification form</h3><br> <label for="req-codefield">enter code here:</label> <input type="text" name="req-codefield" max length="5"/> <input type="submit" name="e-verify-submit" value="verify"/> </form>'; } function show_form(){ echo'<!doctype html> <html lang="en"> <head> <link rel="stylesheet" href="form_show.css" type="text/css" media="all" /> </head> <body> <div id="innerdiv"> <?php buildform(); ?> </div> </body> </html>'; } buildform(); show_form(); } var_dump($token5); } if(isset($_post['e-verify-submit'])){ print_r($_post); $errors = array(); $postedcode = null; if($_post['req-codefield']){ $postedcode = $_post['req-codefield']; $postedcode = trim(stripslashes($postedcode)); } if(strlen($postedcode) === 0){ $errors[] = "you must enter verification code."; } if (strlen($postedcode) < 5 || strlen($postedcode) > 5) { $errors[] ='verification code should contain 5 characters.'; } if ($postedcode === $token5){ echo '<span style="color:green;">success email has been verified</span>'; }else{ echo '<span style="color:red;">email verification has failed. please contact phone.</span>'; } var_dump($token5); }[/php]
[html]
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="form_show.css" type="text/css" media="all" /> </head> <body> <h1>email token test</h1> <form name="e-mail" action="email_token_test.php" method="post" id="form1"> <label for="req-email">email*:</label> <input type="email" id="req-email" name="req-email" required="required" minlength="3" value="" /> <br> <br> <input type="submit" name="submit" value="submit"/> </form> </body> </html> [/html
Comments
Post a Comment