Friday, April 25th, 2008 07:54 PM
Use this code to force a download of any type of content with PHP
header("Content-type:application/octect-stream");
header('Content-Disposition: attachment; filename=filename_' . date('Y-m-d') . '.sql');
print "Hello World";
No Comments »
Tuesday, October 30th, 2007 10:34 PM
Compress all PHP output by adding this line to .htaccess file
php_value output_handler ob_gzhandler
No Comments »
Friday, October 5th, 2007 10:56 PM
301 Redirect using PHP
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$new_location);
No Comments »
Wednesday, September 19th, 2007 11:38 PM
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;
}
[tags]php,export,import,csv,function[/tags]
3 Comments »
Monday, June 25th, 2007 11:30 AM
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
No Comments »
Sunday, May 20th, 2007 02:22 PM
Use find and grep to search for a specific text in a directory. In this example, we search for files which uses the PHP short tag - <?
find -name '*.php' -exec grep '<?[^p\=]' {} \;
No Comments »
Thursday, April 19th, 2007 12:45 AM
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
No Comments »
Wednesday, March 28th, 2007 01:13 PM
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]
No Comments »
Wednesday, March 7th, 2007 06:38 PM
Using PHP’s Curl functions even if the net is accessible only thru a proxy. This is the only way curl can be used if you use GoDaddy’s hosting.
<?php
$ch = curl_init("http://rss.news.yahoo.com/rss/topstories");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, "http://192.168.0.15:80");
curl_setopt($ch, CURLOPT_PROXYPORT, 80);
//curl_setopt ($ch, CURLOPT_PROXYUSERPWD, "username:password");
$data = curl_exec ($ch);
curl_close($ch);
print $data;
?>
No Comments »