Posts Tagged ‘php’

Perl, Trim White Space from a String

Tuesday, July 13th, 2010

When we grep a bunch of string, there are white spaces where we wish to trim, especially the white space at the beginning and the end of a string. There is no trim function in Perl like trim in PHP. The Perl function below should help you to trim the string.


#!/usr/bin/perl

sub trim($);
sub ltrim($);
sub rtrim($);

sub trim($)
{
	my $trim_string = shift;
	$trim_string =~ s/^\s+//;
	$trim_string =~ s/\s+$//;
	return $trim_string;
}

sub ltrim($)
{
	my $trim_string = shift;
	$trim_string =~ s/^\s+//;
	return $trim_string;
}

sub rtrim($)
{
	my $trim_string = shift;
	$trim_string =~ s/\s+$//;
	return $trim_string;
}

my $long_string = "  \t  foo foo        bar \t \t bar          ";
print trim($long_string);

Upgrade to php52 on FreeBSD

Friday, July 2nd, 2010

By default, after portsnap to latest FreeBSD port tree, it will upgrade your php5.2 to php5.3 automatically. There isn’t any option where you can to remain your PHP version to 5.2.

In order to keep my PHP applications running on PHP version 5.2, what I did is upgrade all php5-* packages to php52-*, but it will be a little waste of time if doing it manually, port by port.

So I written this simple bash to output me the packages of php5-* which need to upgrade to php52-*


for d in `portversion -vL= | grep php5 | awk '{print $1}'`; do  OLD=`echo $d | cut -d - -f 1,2`; NEW=`echo $d | sed s/php5/php52/ | cut -d - -f 1,2`; THEPATH=`whereis $NEW | awk '{print $2}' | cut -d / -f 4,5`; echo "portupgrade -o $THEPATH $OLD && \\"; done

The bash will actually output


portupgrade -o textproc/php52-simplexml php5-simplexml && \
portupgrade -o devel/php52-spl php5-spl && \
portupgrade -o databases/php52-sqlite php5-sqlite && \
portupgrade -o devel/php52-tokenizer php5-tokenizer && \
portupgrade -o textproc/php52-xml php5-xml && \
portupgrade -o textproc/php52-xmlreader php5-xmlreader && \
portupgrade -o textproc/php52-xmlwriter php5-xmlwriter && \
portupgrade -o archivers/php52-zip php5-zip && \
portupgrade -o archivers/php52-zlib php5-zlib && \

Before upgrading the php extensions package, upgrade your php52 as below


portupgrade -o lang/php52 php5

On the last time, remember to remove “&& \”, or you can press enter key twice to activate the upgrade.

Disable phpinfo() on Apache Web Server

Tuesday, July 22nd, 2008

Information displays from phpinfo() may provide resourceful information to attacker, such as file patch, web server environment, php modules, web server modules and etc. It’s better to disable phpinfo() function on your webserver.