homepage Welcome to WebmasterWorld Guest from 54.235.16.159
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
handy, generic PHP coding
suppressing error msgs, collecting POST variables, etc.
david752




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

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

 

sugarkane




msg:1261110
 12:42 pm on Apr 16, 2002 (gmt 0)

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

[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]

david752




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

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.

david752




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

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.

David

lorax




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

Suggestion.

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

Nick_W




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

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") {
/*
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;
}

Cypher




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

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))];
$numb_length--;
}

return $pass;
}

northener




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

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

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

:)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved