Tagged with " CSV"
May 6, 2013 - PHP    Comments Off on Down and Dirty PHP mailer with backup CSV

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.
}
Jan 4, 2012 - PHP    Comments Off on Test a Form Entry Against a CSV File.

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