HOME       >>       Programming

Activation Code


khalilov

Whats the php function that generates a random activation code then sends it to the email the user used to sign up. Also how do you check if the inputed email has corrected format? meaning its xxx@something.com


toby

You could do something like a salted hash for the checking address, and regex (preg match) for checking email, though this is probably better done in javascript.


yordan

Also how do you check if the inputed email has corrected format? meaning its xxx@something.com

there are functions in the mail syntax for testing if the address is valid. Returns "true" if the address is valid, and "false" if the address is not valid.

Unfortunately I don't know these functions for php, you will have to read the php documentation for that. But this feature is inside all the mailer servers and the mailer clients, so you simply have to find it for php.


pyost

You could do something like a salted hash for the checking address, and regex (preg match) for checking email, though this is probably better done in javascript.

If you eventually decide on using JavaScript to validate user input, never forget that it can easily be bypassed! JavaScript can make your web site more user-friendly, but server-side verification, through PHP, is absolutely necessary.

 

There are numerous functions that validate e-mail addresses, and you will have to find the one that you find the best. I've bumped into this one: http://www.phpit.net/rg-erdr.php?_rpo=t. If you use PHP5 (or so it seems), you can use an in-built PHP filter: http://www.w3schools.com/php/filter_validate_email.asp

 

As for the validation string, it can really be anything... You can combine time() with a randomly generated number, and then md5() it


TavoxPeru

Whats the php function that generates a random activation code then sends it to the email the user used to sign up. Also how do you check if the inputed email has corrected format? meaning its xxx@something.com

You have a lot of choices for the first one, the following code is a very simple function that i use:

 

<?php /*** Generate an activation code* @param int $length is the length of the activation code to generate* @return string*/function generateCode($length = 10){ $password=""; $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; srand((double)microtime()*1000000); for ($i=0; $i<$length; $i++) { $password .= substr ($chars, rand() % strlen($chars), 1); } return $password; } ?>
For the second one, you can use the is_valid_email() function to verify the format of any email, also, i strongly recommend you to review the following topic Preventing Spam When Using Php's Mail Function because there you can find a complete review of this and the original article where i got the code.

 

<?php function is_valid_email($email) { return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $email);}?>
Other topics that i think can be helpul:

Safety

Need Help With A PHP - MySQL Registration Script

PHP Tutorial: Form Verification And Simple Validation

Best regards,

khalilov

k i got the email verification (got both java and php) and the random code generator, but i still don't know how to auto-email it :/


Mordent

Linky

 

Tadaa! I'll leave it to someone else to expand on that, as I haven't the time. Hope that helps, however.


TavoxPeru

While searching my pc for some code that i need i found this two php functions that you can use, the first one for generating your activation code and the second for validate email addresses.

 

generatePassword(): With this function you can set the length and strength of the generated password.

 

<?phpfunction generatePassword($length=9, $strength=0) { $vowels = 'aeuy'; $consonants = 'bdghjmnpqrstvz'; if ($strength & 1) { $consonants .= 'BDGHJLMNPQRSTVWXZ'; } if ($strength & 2) { $vowels .= "AEUY"; } if ($strength & 4) { $consonants .= '23456789'; } if ($strength & 8) { $consonants .= '@#$%'; } $password = ''; $alt = time() % 2; for ($i = 0; $i < $length; $i++) { if ($alt == 1) { $password .= $consonants[(rand() % strlen($consonants))]; $alt = 0; } else { $password .= $vowels[(rand() % strlen($vowels))]; $alt = 1; } } return $password;}?>
isValidEmail(): Simple case-insensitive regular expression for email validation that returns TRUE if valid and FALSE if not.

<?phpfunction isValidEmail($email){ return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);}?>
Best regards,


VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.