Jump to content
xisto Community
khalilov

Activation Code

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 :mellow:

Share this post


Link to post
Share on other sites

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, Edited by TavoxPeru (see edit history)

Share this post


Link to post
Share on other sites

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,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.