Category: PHP

Down and Dirty PHP mailer with backup CSV

<?php
if($_POST){
	//set up variables
	$to = '';
	$from = '';
	$cc = '';
	$bcc = '';
	$subject = '';
	$backup_filename = '';
	//loop through fields
	foreach($_POST AS $k => $v){
		$message .= '<li>'.$k.': '.$v."</li>\n";
		$csv_header .= $k.',';
		$csv_record .= $v.',';
	}
	//clean up for csv
	$csv_header .= "\n";
	$csv_record .= "\n";
	//htmlize message
	$message = '<html>
	<head>
	  <title>'.$subject.'</title>
	</head>
	<body>
	  <ul>'.$message.'</ul>
	</body>
	</html>';
	// To send HTML mail, the Content-type header must be set
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	// Additional headers
	//$headers .= 'To:' . $to . "\r\n";
	$headers .= 'From:' . $from . "\r\n";
	$headers .= 'Cc:'. $cc . "\r\n";
	$headers .= 'Bcc:'. $bcc . "\r\n";
	// Mail it
	mail($to, $subject, $message, $headers);
	//store backup data
	if (!file_exists($backup_filename)) {
		$csv_record = $csv_header.$csv_record;
	}
	if (!$handle = fopen($backup_filename, 'a')) {
		exit;
	}
	if (fwrite($handle, $csv_record) === FALSE) {
		exit;
	}
	fclose($handle);
	//done.
}

if(!function_exists()){};

Always wrap loose functions in if(!function_exists()){}; to allow for pluging and to prevent name clashes in multiple plugins! Especially useful with dev and/or troubleshooting functions!

Require Dir

if(!function_exists('requireDir')){
function requireDir($dir){
	$dh = @opendir($dir);

	if (!$dh) {
		throw new Exception("Cannot open directory $dir");
	} else {
		while (($file = readdir($dh)) !== false) {
			if ($file != '.' && $file != '..') {
				$requiredFile = $dir . DIRECTORY_SEPARATOR . $file;
				if ('.php' === substr($file, strlen($file) - 4)) {
					require_once $requiredFile;
				} elseif (is_dir($requiredFile)) {
					requireDir($requiredFile);
				}
			}
		}
	closedir($dh);
	}
	unset($dh, $dir, $file, $requiredFile);
}
}

Test a Form Entry Against a CSV File.

The simple way.

<?php
/**
* Test for post; check against list; return error if bad.
*/
function check_data($input){
	//if the input is null, go no further;
	if(empty($input))
		return 'Please enter a valid code.';
	//if the input isn't 6 alphanumeric characters, go no further
	if(!preg_match('/[A-Za-z0-9]{6}/i',$input))
		return 'The code provided is not valid.';
	//finally, test against the list
	$filepath = dirname(__FILE__).'/data/datafile.csv';
	if (($list = fopen($filepath, "r")) !== FALSE) {
		while (($data = fgetcsv($list, 1000, ',')) !== FALSE) {
			if($input == $data[0]){
				header('Location:'.$data[1]);
				die();
			}
		}
		fclose($list);
	}
	return 'The code provided is not valid.';
	}
if($_POST)	
$error = check_data($_POST['code']);
?>

Splash Page Cookie function

function cookie_jumper($cookie_name, $redirect = '', $exp = 86400){
	$mycookie = $_COOKIE[$cookie_name]!='' ? $_COOKIE[$cookie_name] : FALSE;
	$exp = time() + $exp;
	if($mycookie){
		header('Location:'.get_site_url().$redirect);
	} else {
		setcookie($cookie_name,TRUE,$exp);
	}
}

Display stuff inline for troubleshooting

<?php
/*
* A useful troubleshooting function. Displays arrays in an easy to follow format in a textarea.
*/
if ( ! function_exists( 'ts_data' ) ) :
function ts_data($data){
	$ret = '<textarea class="troubleshoot" cols="100" rows="20">';
	$ret .= print_r($data,true);
	$ret .= '</textarea>';
	print $ret;
}
endif;
/*
* A useful troubleshooting function. Dumps variable info in an easy to follow format in a textarea.
*/
if ( ! function_exists( 'ts_var' ) && function_exists( 'ts_data' ) ) :
function ts_var($var){
	ts_data(var_export( $var , true ));
}
endif;