Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

handy, generic PHP coding

suppressing error msgs, collecting POST variables, etc.

4:38 pm on Apr 11, 2002 (gmt 0)

10+ Year Member

Server-side coding can solve difficult problems; for example, when the HTML must be different for different browsers.

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]
error_log("x\r\n", 3, "data/Log.txt");

Suppress undefined variable I/O, and other error detection in a series of


Create separate variables for POSTed data

foreach ($HTTP_POST_VARS as $key => $value)
$$key = $value;
12:42 pm on Apr 16, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

David, thanks for starting this thread - hopefully we can uncover a few gems ;-)

One little function I use a lot is for creating a random 8 character string (for session IDs etc):

function ID() {
for ($index = 0; $index < 8; $index++)
$sid .= substr($pool, (rand()%(strlen($pool))), 1);

11:07 pm on Apr 17, 2002 (gmt 0)

10+ Year Member

If you remove some of the 'ambiguous' characters from the strings, this becomes a nifty password generator.

Ambiguous means 0 and o (eliminate both), 1 and l (remove both), and perhaps others.

4:23 pm on May 14, 2002 (gmt 0)

10+ Year Member

I guess this thread was not interesting to people, judging from the lack of postings. It's too bad; I keep inventing neat stuff in PHP and would love to share with others and find out what they have come up with.


6:06 pm on May 29, 2002 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month


Perhaps if this thread were expanded to include tips, techniques, and methodology or would that be unacceptable?

6:30 pm on May 29, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Here is little function someone gave me that I find invaluable for some CMS work. It gets the emumerations from a table:

function mysql_fetch_enumerations($db_connection, $table_name, $column_name,
$sorted="unsorted") {
Thomas J. Swan
Revision History
1.0 Creation of function
1.1 Added the ability to sort the array before returning it
$db_connection an established mysql connection to the correct
$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"
Array of valid values for an enumerated type
$bob = mysql_fetch_enumerations($mysql_connection, "Event",
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" :
case "descending" :
default :
return $labels;
} else {
echo ("Error not ENUM type\n");
} else {
echo ("Error querying database.\n");
echo mysql_errno( ).": ".mysql_error( )."\n";
return false;

11:15 pm on May 29, 2002 (gmt 0)

10+ Year Member

Heres a little snipet of my own that seems to create nice passwords. The passwords it makes are more memorable than typically because they are ficticious "words".

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))];

return $pass;

12:11 am on May 30, 2002 (gmt 0)

10+ Year Member

here is a shorter piece of code for creating a unique id in PHP:

$id = md5(uniqid(rand()));



Featured Threads

Hot Threads This Week

Hot Threads This Month