19 Sep

CSV Export/Import Functions in PHP

Functions to Export and import CSV data using PHP


//*Get the result of the query as a CSV stream.
function CSVExport($query) {
	global $sql;
	
	$sql_csv = $sql->getSqlHandle($query);
	
	header("Content-type:text/octect-stream");
	header("Content-Disposition:attachment;filename=data.csv");
	while($row = mysql_fetch_row($sql_csv)) {
		$row = $sql->_stripsls($row);
		print '"' . implode('","',$row) . "\"\n";
	}
	exit;
}

25 Jun

HTTP Digest Authentication with PHP

The code for using HTTP Digest Authentication with PHP.


<?php
$realm = 'Restricted area';

if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
    header('HTTP/1.1 401 Unauthorized');
    header('WWW-Authenticate: Digest realm="'.$realm.'",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
    die('Text to send if user hits Cancel button');
}

if (!empty($_SERVER['PHP_AUTH_DIGEST'])) {
	$data = parseHttpDigest($_SERVER['PHP_AUTH_DIGEST']);
	if(!$data) die('Wrong Credentials!');

	// generate the valid response
	$password = 'mypass';//$sql->getOne("SELECT password FROM User WHERE username=".$data['username']);
	$A1 = md5($data['username'] . ':' . $realm . ':' . $password);
	$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
	$valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);

	if ($data['response'] != $valid_response) die('Wrong Credentials!');

	// ok, valid username & password
	print 'Doors open, beds made - welcome home';
}

//Function to parse the http auth header
function parseHttpDigest($digest) {
	//Sample $_SERVER['PHP_AUTH_DIGEST']
	//username="admin", realm="Restricted area", nonce="467f579606f46", uri="/Projects/Nexty/Tests/auth.php", algorithm="MD5", qop="auth", cnonce="zaR5Of4nOsWwx9nA", nc=00000001, response="27212bf93d58fc12b5b7f5b0b95dc38b", opaque="cdce8a5c95a1427d74df7acbf41c9ce0"

    //Protect against missing data
    $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);

    $data = array();
    $parts = explode(", ", $digest);

    foreach ($parts as $element) {
		$bits = explode("=", $element);
		$data[$bits[0]] = str_replace('"','', $bits[1]);

		unset($needed_parts[$bits[0]]);
    }
    return $needed_parts ? false : $data;
}

Links

19 Apr

Compile and Install LAMP Server from Source – PHP5, Apache2, MySQL5

MySQL

./configure --prefix=/usr/local/mysql --localstatedir=/var/lib/mysql --with-mysqld-user=mysql --with-unix-socket-path=/tmp/mysql.sock --without-comment --without-debug --without-bench
make
make install

cp support-files/my-medium.cnf /etc/my.cnf
chown root:sys /etc/my.cnf
chmod 644 /etc/my.cnf
chown -R root:mysql /usr/local/mysql
chown mysql:mysql /var/lib/mysql

./scripts/mysql_install_db

cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &
/usr/local/mysql/bin/mysql

Apache

./configure --prefix=/usr/local/apache --enable-so --enable-cgi--enable-info --enable-rewrite --enable-speling --enable-usertrack --enable-deflate --enable-ssl --enable-mime-magic
make
make install

PHP

./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --disable-debug --enable-ftp --enable-inline-optimization --enable-magic-quotes --enable-mbstring --enable-mm=shared --enable-safe-mode --enable-track-vars --enable-trans-sid --enable-wddx=shared --enable-xml --with-dom --with-gd --with-gettext --with-mysql=/usr/local/mysql --with-regex=system --with-xml --with-zlib-dir=/usr/lib --with-gettext --with-gdbm

make
make install
cp -p php.ini-recommended /usr/local/php/php.ini

Configuration

httpd.conf
php.ini
28 Mar

Find Difference Between 2 Dates in PHP

This function will find the difference between two given times. Does PHP already have a native function for that?


//The difference between 2 given time
function dateDifference($day_1,$day_2) {
	$diff = strtotime($day_1) - strtotime($day_2);

	$sec   = $diff % 60;
	$diff  = intval($diff / 60);
	$min   = $diff % 60;
	$diff  = intval($diff / 60);
	$hours = $diff % 24;
	$days  = intval($diff / 24);
	
	return array($sec,$min,$hours,$days);
}

If you just want the day difference…


$diff = strtotime($day_1) - strtotime($day_2) + 1; //Find the number of seconds
$day_difference = ceil($diff / (60*60*24)) ;  //Find how many days that is

This can be done using the DATEDIFF function in MySQL 5

[tags]php,code,date,difference,time[/tags]