Shantanu's Blog
Database Consultant
March 14, 2012
Display the source code in printable format
// contents of showcode.php file
?php
$source = $file;
highlight_file( $source [ $return = false ] );
?>
It is possible that "return=false" may not work on older browsers and / or Linux OS. We can make any php file printable by passing the file name as variable in the URL. for e.g.
http://my_web_site.com/showcode.php?file=toprint.php
Labels: php tips, usability
August 18, 2010
Memory loss
There are times when you see the following PHP error in the error log.
[Mon Aug 02 14:28:11 2010] [error] PHP Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 292995097 bytes) in /var/www/html/text.inc on line 25, referer: https://yahoo.com/Compose.php?con_id=cand74035
The usual fix for this problem is as follows. At the top of the compose.php page use the following statement.
ini_set("memory_limit","200M");
This will increase the memory allocation for the current page only.
Labels: php tips
Extract of Apache error log
There are times when you need to check the latest entries in the apache error log.
Here is the easy way to do so.
Cron entry:
# extract of apache error log
# updated every 5 minutes and posted to http://yourSite.com/shantanu/index.txt
*/5 * * * * sh /home/develop/apache.sh >> /home/develop/apache_succ.txt 2>> /home/develop/apache_err.txt
Shell script:
# cat /home/develop/apache.sh
#!/bin/sh
mytemp='/home/develop/mytemp.txt'
mypath='/home/develop/index.txt'
> $mypath
> $mytemp
for file in `find /var/log/httpd/ -name "*log"`
do
tail -100 $file | grep "`date +%h\ %d`" >> $mytemp
done
sed -i '/^$/d' $mytemp
grep -v 'favicon' $mytemp > $mypath
mv $mypath /var/www/html/shantanu/
Labels: php tips, shell script
August 16, 2010
Timezone error in apache
There are times when you see the following error in the apache error log.
[Sun Aug 08 14:06:12 2010] [error] [client 199.199.199.199] PHP Strict Standards: date() [
function.date]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead in /var/www/html/global_fun.inc on line 524, referer: https://yahoo.com/edit.php?acc=deliver&addr=543
This is because of timezone mismatch.
You can export the time zone variable as shown below.
export TZ="America/New_York"
Or add it to the php.ini file.
[Date]
; Defines the default timezone used by the date functions
date.timezone = America/New_York
Or else, set the timezone in the code itself.
date_default_timezone_set('America/New_York');
echo date('D,F j, Y, h:i:s A');
?>
You can check the current time in the following file.
# cat /etc/sysconfig/clock
ZONE="America/New_York"
UTC=true
ARC=false
The following file shows that we are not sure about the current localtime. It is probably taking the time from system clock.
# ls -l /etc/localtime
-rw-r--r-- 1 root root 1267 Mar 5 2008 /etc/localtime
The localtime file needs to be linked to the correct time zone.
ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime
All the above examples use America (New York) timezone has default one. You will have to change it if you need a different zone.
Labels: linux tips, php tips
January 07, 2010
finding exit code from PHP
If you use exec to execute the UNIX command or shell script then you do not get the exit status.
passthru command will allow you to save and use the exit code.
$synarray='myfile';
passthru("gunzip ".$synarray.".sql.gz > /dev/null 2>&1", $returnvalue);
if ($returnvalue != 0){
echo "no success";
}else{
echo "success";
}
Labels: php tips, shell script
January 01, 2010
troubleshooting using logger
The logger command can be used anywhere in the script or PHP code that will log messages to... guess where? /var/log/messages
mysql_query("show databases") or die(exec('logger "show db query error on line 87 of index.php"'));
_____
mysql_query('update mytest set id = 12 where id = 103;');
$mycount = mysql_affected_rows();
if( "$mycount" == 0) {
exec('logger "update mytest failed for the value 12 the old value 103 was retained at `date`"');
}
Labels: php tips
December 18, 2009
Finding PHP syntax errors
There is a very easy way to find PHP syntax errors.
If all your .php files are in /var/www/html then you can type the following command.
# find /var/www/html -name '*.php' -exec php -l {} \; | grep -v '^No syntax'
You can save the output to a text file by appending "> tofile.txt" at the end of the above command.
The output will look something like this...
Parse error: syntax error, unexpected '<' in ./reports/extra/rc7bkp2.php on line 265
Errors parsing ./reports/extra/rc7bkp2.php
Parse error: syntax error, unexpected $end in ./reports/extra/rc6bkp.php on line 525
Errors parsing ./reports/extra/rc6bkp.php
Parse error: syntax error, unexpected '}' in ./login/login.php on line 53
Errors parsing ./login/login.php
Parse error: syntax error, unexpected $end in ./commom/bkp/25_nov/Process1.php on line 93
Errors parsing ./commom/bkp/25_nov/Process1.php
_____
If you want to check if a file for e.g. reserve.php has any errors, use the following command....
$ tail -f /var/log/httpd/error_log | grep 'reserve.php'
[Fri Dec 18 16:46:30 2009] [error] [client 59.95.20.15] PHP Notice: Undefined index: ucd in /var/www/html/login.php on line 531, referer: http://111.222.333.444/reserve.php
[Fri Dec 18 16:46:33 2009] [error] [client 117.195.35.82] PHP Notice: A session had already been started - ignoring session_start() in /var/www/html/checkPass.php on line 2, referer: http://666.777.888.999/reserve.php
Labels: php tips
October 18, 2009
PHP security tips
avoid SQL Injection
It can happen anywhere you have a form that the user can fill in and submit.
There is a PHP function to avoid the SQL Injection attack.
$query_ok = "SELECT * FROM users WHERE username = 'mysql_real_escape_string($injection_string)'";
Real escape strings will replace all single quote and double quote character with the escaped string \
$new_user=mysql_real_escape_string($_GET["injection_string"]);
$query_ok = "SELECT * FROM mysql.user WHERE User = '" . "$new_user" ."' limit 1";
echo $query_ok;
$result=mysql_query($query_ok) or die("some errror");
_____
If you need to use SMTP server to send mail, here is a nice writeup for the same.
http://9lessons.blogspot.com/2009/10/send-mail-using-smtp-and-php.html
Labels: php, php tips
Archives
June 2001
July 2001
January 2003
May 2003
September 2003
October 2003
December 2003
January 2004
February 2004
March 2004
April 2004
May 2004
June 2004
July 2004
August 2004
September 2004
October 2004
November 2004
December 2004
January 2005
February 2005
March 2005
April 2005
May 2005
June 2005
July 2005
August 2005
September 2005
October 2005
November 2005
December 2005
January 2006
February 2006
March 2006
April 2006
May 2006
June 2006
July 2006
August 2006
September 2006
October 2006
November 2006
December 2006
January 2007
February 2007
March 2007
April 2007
June 2007
July 2007
August 2007
September 2007
October 2007
November 2007
December 2007
January 2008
February 2008
March 2008
April 2008
July 2008
August 2008
September 2008
October 2008
November 2008
December 2008
January 2009
February 2009
March 2009
April 2009
May 2009
June 2009
July 2009
August 2009
September 2009
October 2009
November 2009
December 2009
January 2010
February 2010
March 2010
April 2010
May 2010
June 2010
July 2010
August 2010
September 2010
October 2010
November 2010
December 2010
January 2011
February 2011
March 2011
April 2011
May 2011
June 2011
July 2011
August 2011
September 2011
October 2011
November 2011
December 2011
January 2012
February 2012
March 2012
April 2012
May 2012
June 2012
July 2012
August 2012
October 2012
November 2012
December 2012
January 2013
February 2013
March 2013
April 2013
May 2013
June 2013
July 2013
September 2013
October 2013
January 2014
March 2014
April 2014
May 2014
July 2014
August 2014
September 2014
October 2014
November 2014
December 2014
January 2015
February 2015
March 2015
April 2015
May 2015
June 2015
July 2015
August 2015
September 2015
January 2016
February 2016
March 2016
April 2016
May 2016
June 2016
July 2016
August 2016
September 2016
October 2016
November 2016
December 2016
January 2017
February 2017
April 2017
May 2017
June 2017
July 2017
August 2017
September 2017
October 2017
November 2017
December 2017
February 2018
March 2018
April 2018
May 2018
June 2018
July 2018
August 2018
September 2018
October 2018
November 2018
December 2018
January 2019
February 2019
March 2019
April 2019
May 2019
July 2019
August 2019
September 2019
October 2019
November 2019
December 2019
January 2020
February 2020
March 2020
April 2020
May 2020
July 2020
August 2020
September 2020
October 2020
December 2020
January 2021
April 2021
May 2021
July 2021
September 2021
March 2022
October 2022
November 2022
March 2023
April 2023
July 2023
September 2023
October 2023
November 2023
April 2024
May 2024
June 2024
August 2024
September 2024
October 2024
November 2024
December 2024
January 2025
February 2025
April 2025
June 2025
July 2025
August 2025
November 2025
March 2026
July 2026
September 2026
