The following describes howPHPThe 21 function code segments that are frequently used during development can be directly used when we use them.
1. PHP can read random strings
This Code creates a readable string that is closer to a word in the dictionary and is practical and password-verified.
- /**************
- *@length – length of random string (must be a multiple of 2)
- **************/
- function readable_random_string($length = 6){
- $conso=array(“b”,”c”,”d”,”f”,”g”,”h”,”j”,”k”,”l”,
- “m”,”n”,”p”,”r”,”s”,”t”,”v”,”w”,”x”,”y”,”z”);
- $vocal=array(“a”,”e”,”i”,”o”,”u”);
- $password=”";
- srand ((double)microtime()*1000000);
- $max = $length/2;
- for($i=1; $i<=$max; $i++)
- {
- $password.=$conso[rand(0,19)];
- $password.=$vocal[rand(0,4)];
- }
- return $password;
- }
2. PHP generates a random string
If you do not need readable strings, use this function instead to create a random string as your random password.
- /*************
- *@l – length of random string
- */
- function generate_rand($l){
- $c= “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789″;
- srand((double)microtime()*1000000);
- for($i=0; $i<$l; $i++) {
- $rand.= $c[rand()%strlen($c)];
- }
- return $rand;
- }
3. PHP-encoded email address
This code can be used to encode any email address as an html character entity to prevent being collected by spam programs.
- function encode_email($email=’info@domain.com’, $linkText=’Contact Us’,
- $attrs =’class=”emailencoder”‘ )
- {
- // remplazar aroba y puntos
- $email = str_replace(‘@’, ‘@’, $email);
- $email = str_replace(‘.’, ‘.’, $email);
- $email = str_split($email, 5);
- $linkText = str_replace(‘@’, ‘@’, $linkText);
- $linkText = str_replace(‘.’, ‘.’, $linkText);
- $linkText = str_split($linkText, 5);
- $part1 = ‘<a href=”ma’;
- $part2 = ‘ilto:’;
- $part3 = ‘” ‘. $attrs .’ >’;
- $part4 = ‘</a>’;
- $encoded = ‘<script type=”text/javascript”>’;
- $encoded .= “document.write(‘$part1′);”;
- $encoded .= “document.write(‘$part2′);”;
- foreach($email as $e)
- {
- $encoded .= “document.write(‘$e’);”;
- }
- $encoded .= “document.write(‘$part3′);”;
- foreach($linkText as $l)
- {
- $encoded .= “document.write(‘$l’);”;
- }
- $encoded .= “document.write(‘$part4′);”;
- $encoded .= ‘</script>’;
- return $encoded;
- }
4. PHP verification email address
Email verification may be the most common web form verification. In addition to verifying the email address, this code can also choose to check MX records in the DNS to which the email domain belongs, making email verification more powerful.
- function is_valid_email($email, $test_mx = false)
- {
- if(eregi(“^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,4})$”, $email))
- if($test_mx)
- {
- list($username, $domain) = split(“@”, $email);
- return getmxrr($domain, $mxrecords);
- }
- else
- return true;
- else
- return false;
- }
5. PHP listing directory content
- function list_files($dir)
- {
- if(is_dir($dir))
- {
- if($handle = opendir($dir))
- {
- while(($file = readdir($handle)) !== false)
- {
- if($file != “.” && $file != “..” && $file != “Thumbs.db”)
- {
- echo ‘<a target=”_blank” href=”‘.$dir.$file.’”>’.$file.’</a><br>’.”n”;
- }
- }
- closedir($handle);
- }
- }
- }
1