Forum Moderators: coopster
Here are some PHP snippets to do common tasks. I've used letters like "x" to indicate where to customize.
[color=blue]Store a given string x into a log file[/color]
[code]
error_log("x\r\n", 3, "data/Log.txt");
Suppress undefined variable I/O, and other error detection in a series of
statements
error_reporting(0);
x
error_reporting(63);
Create separate variables for POSTed data
foreach ($HTTP_POST_VARS as $key => $value)
$$key = $value;
One little function I use a lot is for creating a random 8 character string (for session IDs etc):
[perl]
function ID() {
srand(time());
$pool="1234567890";
$pool.="abcdefghijklmnopqrstuvwxyz";
for ($index = 0; $index < 8; $index++)
{
$sid .= substr($pool, (rand()%(strlen($pool))), 1);
}
return($sid);
}
[/perl]
function mysql_fetch_enumerations($db_connection, $table_name, $column_name,
$sorted="unsorted") {
/*
Author
Thomas J. Swan
Revision History
1.0 Creation of function
1.1 Added the ability to sort the array before returning it
Parameters
$db_connection an established mysql connection to the correct
database
$table_name name of Table containing the ENUMeration
$column_name column whose possible values are to be listed
$sorted sort array in "ascending","descending", or "unsorted"
order
Returns
Array of valid values for an enumerated type
Example
$bob = mysql_fetch_enumerations($mysql_connection, "Event",
"age_group");
while(list($key,$value) = each($bob)) {
echo $value . " ";
}
echo "\n";
*/
$table = addslashes($table);
$field = addslashes($field);
if ($result = mysql_query("SHOW COLUMNS FROM $table_name LIKE
\"$column_name\"",$db_connection)) {
$temp = mysql_fetch_array($result);
if (strstr(strtoupper($temp['Type']),"ENUM")) {
$str = StripSlashes(substr($temp['Type'],
(strpos($temp['Type'],"(",0))+1, (strpos($temp['Type'],"\)",0)-1)));
$tok = strtok($str,",");
while ($tok) {
$labels[] = substr($tok,(1),(strlen($tok) - 2));
$tok = strtok(",");
}
switch(strtolower($sorted)) {
case "ascending" :
sort($labels);
break;
case "descending" :
rsort($labels);
break;
default :
break;
}
return $labels;
} else {
echo ("Error not ENUM type\n");
}
} else {
echo ("Error querying database.\n");
echo mysql_errno( ).": ".mysql_error( )."\n";
}
return false;
}
function rand_pass () {
$letters = array('b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z');
$vowels = array('a','e','i','o','u');
$numbs = array('1','2','3','4','5','6','7','8','9','0');
$word_length = 6;
$numb_length = 3;
while ($word_length > 0) {
$pass .= $letters[rand(0,count($letters))];
$pass .= $vowels[rand(0,count($vowels))];
$word_length -= 2;
}
while ($numb_length > 0) {
$pass .= $numbs[rand(0,count($numbs))];
$numb_length--;
}
return $pass;
}
$id = md5(uniqid(rand()));
:)