Archive for the ‘PHP’ Category

fpm-status and restart

Tuesday, April 17th, 2018

http://x.x.x.x/fpmstatus.php?full

systemctl restart php-fpm

Purify Function to Prevent XSS

Saturday, July 13th, 2013

This is a purify function to ensure user-inputed string is sanitized and prevent XSS kind of attacks.

function purify($input){
	$result = "";
	if (isset($input) && !empty($input)) {
		$result = rawurldecode($input);
		$result = strip_tags($result);
		$result = stripcslashes($result);
		$result = htmlspecialchars($result, ENT_QUOTES); 
		$result = iconv('utf-8','utf-8//IGNORE',$result);
	}
	return $result;
}

Get Client IP Address

Saturday, July 13th, 2013

The following PHP code snippet will return client IP address.

function get_client_ip() {
	$ipaddress = '';
	if (getenv('HTTP_CLIENT_IP'))
		$ipaddress = getenv('HTTP_CLIENT_IP');
	else if(getenv('HTTP_X_FORWARDED_FOR'))
		$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
	else if(getenv('HTTP_X_FORWARDED'))
		$ipaddress = getenv('HTTP_X_FORWARDED');
	else if(getenv('HTTP_FORWARDED_FOR'))
		$ipaddress = getenv('HTTP_FORWARDED_FOR');
	else if(getenv('HTTP_FORWARDED'))
	   $ipaddress = getenv('HTTP_FORWARDED');
	else if(getenv('REMOTE_ADDR'))
		$ipaddress = getenv('REMOTE_ADDR');
	else
		$ipaddress = 'UNKNOWN';

	return $ipaddress;
}

Compiling PHP with mysql support on x64 platform

Tuesday, July 12th, 2011

In order to compile PHP on a 64 bit platform, you will need to include the following configure option
–with-libdir=lib64

PHP Redirect http to https

Friday, June 17th, 2011

if ($_SERVER[‘HTTPS’] != “on”) {
$url = “https://”. $_SERVER[‘SERVER_NAME’] . $_SERVER[‘REQUEST_URI’];??? ?header(“Location: $url”);
exit;
}

Enable Error Reporting for PHP

Thursday, August 5th, 2010

If you don’t have access to the php.ini or have issue setting up .htaccess to enable error reporting.

You can try the following code

ini_set( ‘display_errors’, true );
error_reporting( E_ALL );

mod_geoip

Tuesday, May 5th, 2009

Found a nice article on how to integrate mod_geoip in PHP.

http://blackonsole.blogspot.com/2009/02/install-and-configure-geoip-on-opensuse.html

wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.5.tar.gz

wget http://geolite.maxmind.com/download/geoip/api/mod_geoip2/mod_geoip2_1.2.5.tar.gz

apxs2 -i -a -L/usr/local/lib -I/usr/local/include -lGeoIP -c mod_geoip.c


IfModule mod_geoip.c

GeoIPEnable On
GeoIPDBFile /usr/local/share/GeoIP/GeoIP.dat

IfModule


$country_code = apache_note("GEOIP_COUNTRY_CODE");
print "Code : " . $country_code . "
";
$country_name = apache_note("GEOIP_COUNTRY_NAME");
print "Country: " . $country_name . "
";

Indexing Word / Powerpoint / Excel / PDF

Tuesday, May 6th, 2008

I am working on a project to allow full-text indexing of common file types such as word, powerpoint, excel and pdf. After searching for a few days, I have a hard time finding a perfect solution.

PDF

http://www.foolabs.com/xpdf/

Word

http://ftp.wagner.pp.ru/~vitus/software/catdoc/

Excel

http://ftp.wagner.pp.ru/~vitus/software/catdoc/

PowerPoint

Parsing a Config File in PHP

Thursday, December 13th, 2007

This is a sample code to parse a config file in PHP

$mosConfig_CONFIG_IDE=Y
$mosConfig_HOST=localhost


while (!feof($fp)) {
$line = trim(fgets($fp));
if (ereg('\$mosConfig_', $line)) {
$line = ereg_replace ('\$mosConfig_',"",$line);
$pieces = explode("=", $line);
$option = trim($pieces[0]);
$value = trim($pieces[1]);
$value = ereg_replace ("^\'","",$value);
$value = ereg_replace ("\'\;$","",$value);
$config_values[$option] = $value;
}
}
fclose($fp);

Installing Apache 2.x and PHP5

Saturday, March 10th, 2007

Installing Apache 2.x

./configure \
--prefix=/usr/local/apache \
--enable-info \
--enable-rewrite \
--enable-ssl \
--enable-unique-id \
--enable-deflate \
--enable-usertrack \
--enable-so

make
make install


cp build/rpm/httpd.init /etc/init.d/httpd
chkconfig --add httpd
chkconfig --level 3456 httpd on

Installing PHP required Libraries
http://www.ijg.org/files/jpegsrc.v6b.tar.gz
./configure --enable-shared
mkdir -p /usr/local/man/man1
make
make install

http://www.libpng.org/pub/png/libpng.html
./configure
make
make install

http://voxel.dl.sourceforge.net/sourceforge/freetype/freetype-2.1.9.tar.gz
./configure
make
make test
make install

Install LibMCrypt
http://sourceforge.net/projects/mcrypt
./configure
make
make install

Installing PHP5

./configure \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-openssl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--with-ttf \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-mcrypt \
--enable-mbstring \
--enable-bcmath

make
make install

Configuring Apache for PHP
vi /usr/local/apache/conf/httpd.conf

add
AddType application/x-httpd-php .php

change
DirectoryIndex index.php index.html index.html.var

php.ini
copy php.ini-development or php.ini-production to /usr/local/lib/php.ini

edit /usr/local/lib/php.ini
timezone = Asia/HongKong