Showing posts with label PHP rand. Show all posts
Showing posts with label PHP rand. Show all posts

Friday, 5 June 2015

PHP: Create a random password

<?php

/** * The letter l (lowercase L) and the number 1

* have been removed, as they can be mistaken

* for each other. */

function createRandomPassword(){   

$chars = "abcdefghijkmnopqrstuvwxyz023456789";   
$i = 0;   
$pass = '' ; 
while ($i <= 7) {
$num = rand() % 33;     
$tmp = substr($chars, $num, 1);     
$pass = $pass . $tmp;       
$i++;   

return $pass;
}

// Usage$password = createRandomPassword();
echo "Your random password is: $password";
?>

Wednesday, 3 June 2015

PHP: File Uploader

<?php
# @function upload_file
# 
# @param $field  string  the name of the file upload form field
# @param $dirPath string  the relative path to which to store the file (no trailing slash)
# @param $maxSize int   the maximum size of the file (in bytes)
# @param $allowed array  an array containing all the "allowed" file mime-types
#
# @return mixed  the files' stored path on success, false on failure.
function upload_file($field = '', $dirPath = '', $maxSize = 100000, $allowed = array())
{
 foreach ($_FILES[$field] as $key => $val)
  $$key = $val; 

 if ((!is_uploaded_file($tmp_name)) || ($error != 0) || ($size == 0) || ($size > $maxSize))
  return false; // file failed basic validation checks

 if ((is_array($allowed)) && (!empty($allowed)))
  if (!in_array($type, $allowed))  
   return false; // file is not an allowed type

 do $path = $dirPath . DIRECTORY_SEPARATOR . rand(1, 9999) . strtolower(basename($name));
 while (file_exists($path));

 if (move_uploaded_file($tmp_name, $path))
  return $path;

 return false;
}

// DEMO
/*
if (array_key_exists('submit', $_POST))
{
 if ($filepath = upload_file('music_upload', 'music_files', 700000, array('audio/mpeg','audio/wav')))
  echo 'File uploaded to ' . $filepath;
 else
  echo "An error occurred uploading the file... please try again.";
}
echo '   <form method="post" action="' .$_SERVER['PHP_SELF']. '" enctype="multipart/form-data">
   <input type="file" name="music_upload" id="music_upload" />
   <input type="submit" name="submit" value="submit" />
  </form>
 '; 
print_r($_FILES);  // for debug purposes
*/ 

?>
 
Refer to the commented section of code under the function labeled DEMO for usage. 

Tuesday, 2 June 2015

PHP: Generate an authentication code

<?php # This particular code will generate a random string 
# that is 25 charicters long 25 comes from the number 
# that is in the for loop 
$string "abcdefghijklmnopqrstuvwxyz0123456789"; 
for(
$i=0;$i<25;$i++){ 
    
$pos rand(0,36); 
    
$str .= $string{$pos}; 
} 
echo 
$str; # If you have a database you can save the string in  
# there, and send the user an email with the code in 
# it they then can click a link or copy the code 
# and you can then verify that that is the correct email 
# or verify what ever you want to verify 

?>

Monday, 23 February 2015

PHP: Generate an alpha-numeric password salt

<?php
/**
 * This function generates an alpha-numeric password salt (with a default of 32 characters)
 * @param $max integer The number of characters in the string
 *
 */
function generateSalt($max = 32) {
$baseStr = time() . rand(0, 1000000) . rand(0, 1000000);
$md5Hash = md5($baseStr);
if($max < 32){
$md5Hash = substr($md5Hash, 0, $max);
}
return $md5Hash;
}

//Usage:
/*
echo "Salt with 32 characters:\n";
echo generateSalt() . "\n";
echo "Salt with 5 characters:\n";
echo generateSalt(5) . "\n";
*/
?>