TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-29-2007, 01:03 AM   #1 (permalink)
The Wanderer
 
codyodell's Avatar
 
Join Date: Nov 2007
Posts: 12
Thanks: 2
codyodell is on a distinguished road
Default Useful snippets.

These are some of the function, classes and useful arrays I use most.

Code:
function refresh($strURL = NULL){
	$page = ($strURL == NULL)?$_SERVER['PHP_SELF']:$strURL;
	if(basename($_SERVER['PHP_SELF']) != $strURL){
		echo '<meta http-equiv="refresh" content="0;url='.$page.'">';
	}
}
function toString($str){
	echo '<br /><pre>';
	print_r($str);
	echo '</pre>';
}

function makeSafe($dangerous){
	$dangerous = trim($dangerous);
	$dangerous = htmlspecialchars($dangerous);
	return $dangerous;
}

function addQuote($string){
	return '"'.$string.'"';
}

function showErrors($arErrors){
	$dispErrors = '';
	if(is_array($arErrors)){
		if(count($arErrors) > 0){
			foreach($arErrors as $error){
				$dispErrors .= '<li>'.$error.'</li>';
			}
			
			return '<ul id="errors">'.$dispErrors.'</ul>';
		}else{
			return false;
		}
	
	}else{
		if($arErrors != ''){
			$dispErrors .= '<li>'.$arErrors.'</li>';
			return '<ul id="errors">'.$dispErrors.'</ul>';
		}else{
			return false;
		}
	}
}

class mailer{
    var $to         = "";
    var $subject    = "";
    var $message    = "";
    var $fromName   = "";
    var $fromEmail  = "";
    var $replyEmail = "";
    var $header     = "";
    var $type       = "text/html";
    var $characterSet = "ISO-8859-1";
    
    
    function send(){
        $this->createHeader();
        if (@mail($this->to,$this->subject,$this->message,$this->header)){
            return true;
        } else {
            return false;
        }
    }
    
    function createHeader(){
        $from   = "From: $this->fromName <$this->fromEmail>\r\n";
        $replay = "Reply-To: $this->replyEmail\r\n";    
        $params = "MIME-Version: 1.0\r\n";
        $params .= "Content-type: $this->type; charset=$this->characterSet\r\n";
        
        $this->header = $from.$replay.$params;
        return $this->header;
    }
    
}
/*
      $mailer = new mailer();
        
      $mailer->to         = isset($_POST['to']) ? $_POST['to'] : "";
      $mailer->fromName   = isset($_POST['fromname']) ? $_POST['fromname'] : "";
      $mailer->fromEmail  = isset($_POST['fromemail']) ? $_POST['fromemail'] : "";
      $mailer->replyEmail = isset($_POST['replyemail']) ? $_POST['replyemail'] : "";
      $mailer->subject    = isset($_POST['subject']) ? $_POST['subject'] : "";
      $mailer->message    = isset($_POST['message']) ? $_POST['message'] : "";

      if ($mailer->send()) {
        echo "Thanks for your message!";
      } else {
        echo "Sending email was failed!";
      }
*/


function fnRandomPassword($nLength = 8, $nChars = 3) {
	$minLength   = 6;
	$maxLength   = 20;
	$strPassword = "";
	
	for ( $i = 1 ; $i <= $nLength ; $i++ ) {
		# Randomize the type of this character
		# (1) Uppercase, (2) Lowercase, (3) Numeric
		$nChar = rand(1, $nChars);

		switch ($nChar) {
			case 1:
				# Uppercase character
				$strPassword .= chr(rand(0, 25) + 65);
				
				break;

			case 2:
				# Lowercase character
				$strPassword .= chr(rand(0, 25) + 97);
				
				break;

			case 3:
				# Numeric character
				$strPassword .= chr(rand(0, 9) + 48);
				
				break;
		}
	}
	
	return($strPassword);
}

function fnGetStates($strSelID) {
   $htmlData  = '<option value=""></option>';
   $hashData  = array("AL" => "Alabama", "AK" => "Alaska", "AZ" => "Arizona", "AR" => "Arkansas", "CA" => "California", "CO" => "Colorado", "CT" => "Connecticut", "DE" => "Delaware", "DC" => "District of Columbia", "FL" => "Florida", "GA" => "Georgia", "GU" => "Guam", "HI" => "Hawaii", "ID" => "Idaho", "IL" => "Illinois", "IN" => "Indiana", "IA" => "Iowa", "KS" => "Kansas", "KY" => "Kentucky", "LA" => "Louisiana", "ME" => "Maine", "MD" => "Maryland", "MA" => "Massachusetts", "MI" => "Michigan", "MN" => "Minnesota", "MS" => "Mississippi", "MO" => "Missouri", "MT" => "Montana", "NE" => "Nebraska", "NV" => "Nevada", "NH" => "New Hampshire", "NJ" => "New Jersey", "NM" => "New Mexico", "NY" => "New York", "NC" => "North Carolina", "ND" => "North Dakota", "OH" => "Ohio", "OK" => "Oklahoma", "OR" => "Oregon", "PA" => "Pennsylvania", "PR" => "Puerto Rico", "RI" => "Rhode Island", "SC" => "South Carolina", "SD" => "South Dakota", "TN" => "Tennessee", "TX" => "Texas", "UT" => "Utah", "VT" => "Vermont", "VA" => "Virginia", "WA" => "Washington", "WV" => "West Virginia", "WI" => "Wisconsin", "WY" => "Wyoming");

   foreach ($hashData as $strID => $strName) {
      $strSel    = ($strID == $strSelID) ? "selected" : "";
      $htmlData .= '<option value="' . $strID . '" ' . $strSel . '>' . $strName . '</option>';
   }
   
   return($htmlData);
}

function getMonths($curSel){
	$dispOptions = '
	<option value="01">January</option>
	<option value="02">February</option>
	<option value="03">March</option>
	<option value="04">April</option>
	<option value="05">May</option>
	<option value="06">June</option>
	<option value="07">July</option>
	<option value="08">August</option>
	<option value="09">September</option>
	<option value="10">October</option>
	<option value="11">November</option>
	<option value="12">December</option>';
	return $dispOptions;
}

function getYears($numYears){
	$year = date("Y");
	$dispOptions = '';
	for($i = 0;$i < $numYears; $i++){
		$dispOptions .= '<option value="'.($year + $i).'">'.($year + $i).'</option>';
	}
	return $dispOptions;
}
__________________
The man who invented the wheel was smart. The man who invented the other 3, he was a genius.
codyodell is offline  
Reply With Quote
The Following 2 Users Say Thank You to codyodell For This Useful Post:
marxx (12-02-2007), Wildhoney (11-30-2007)
Old 11-29-2007, 01:07 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Default

Some useful functions there. We were actually considering having a section on TalkPHP so everybody could bring their functions together into one huge library. We might reconsider that now we've re-coded TalkPHP!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-29-2007, 02:13 AM   #3 (permalink)
The Frequenter
Advanced Programmer Top Contributor Good Samaritan 
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 469
Thanks: 26
sketchMedia is on a distinguished road
Default

that would be a cool idea 'talkPHP php libary' or somthing, some good snippets there m8.

Heres a little script i found a few years ago whilst messing with Zend Studio, i found it in the 'snippets' section, its quite interesting and (for me anyway) abit of a brain teaser to understand whats goin on

Just to note i DID NOT write this script, i don't want credit, the guy who wrote this should get credit (although he hasn't provided any information apart from his online alias 'bigmweb') i just thought u guys would find it interesting.

its a funny, when i showed it to my friend he said 'there has to be some form of witch craft involved, it must have something to do with the black arts' to a certain point i agree, although i do know understand how it works (i think )

basically it converts a string to a barcode (well as close to a barcode as your goin to get in php) and its 2 way, as the guy says in his commenting he personally uses it to pretty up his session id's, but im sure there are other uses.

link:http://www.zend.com//code/codex.php?ozid=425&single=1
__________________

Last edited by sketchMedia : 11-30-2007 at 08:42 PM.
sketchMedia is offline  
Reply With Quote
Old 11-29-2007, 02:26 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Default

Very different. How does it assist in prettifying the session ID, though? To be honest, I'd rather see hexadecimal values than that, but it's certainly an interesting concept.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-29-2007, 04:46 AM   #5 (permalink)
The Acquainted
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 175
Thanks: 6
CoryMathews is on a distinguished road
Default

some good functions in there cody. thanks.
CoryMathews is offline  
Reply With Quote
Old 11-29-2007, 05:29 PM   #6 (permalink)
The Frequenter
Advanced Programmer Top Contributor Good Samaritan 
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 469
Thanks: 26
sketchMedia is on a distinguished road
Default

Yea im not sure of the point in supposedly prettying up the session id's personally i think it makes them look cumbersome.
__________________
sketchMedia is offline  
Reply With Quote
Old 12-01-2007, 11:56 PM   #7 (permalink)
The Contributor
 
Join Date: Oct 2007
Posts: 35
Thanks: 2
Sled is on a distinguished road
Default

Some of these are very useful. Thanks for sharing!
Sled is offline  
Reply With Quote
Old 12-02-2007, 05:36 PM   #8 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 40
Thanks: 3
marxx is on a distinguished road
Default

Thanks for sharing these mr.codyodell! =)

@wildhoney
talkPHP PHP library would be very nice in here!
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:30 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0