Home

Advertisement

PHP IDE: Configuring PDT, NetBeans, xdebug

  • Jul. 8th, 2009 at 12:41 PM

<input ... ><input ... >
 
Installing PDT and plug ins for subversion
http://2tbsp.com/content/getting_started_eclipse_php_development_tools_(pdt)




<input ... ></input><input ... >
 

PHP Security

  • Jun. 30th, 2009 at 11:46 AM

<input ... ></input><input ... >
 
Disable Register Globals
register_globals = Off
Disable Remote URL's
allow_url_fopen = Off in php.ini
Restricting php read/write
open_basedir = /var/www/htdocs/files
Safe Mode
safe_mode = Off
Enable Acess to Group safe_mode_gid = On
Allow binary execution from directory safe_mode_exec_dir = /var/www/binaries Allow Access to environment variables, say starting with PHp safe_mode_allowed_env_vars = PHP_ Limit Resources
max_execution_time = 30  ; Max script execution time
max_input_time = 60      ; Max time spent parsing input
memory_limit = 16M       ; Max memory used by one script
upload_max_filesize = 2M ; Max upload file size
post_max_size = 8M       ; Max post size
Limit access to certian file name patterns(prevent .inc & .sql files) In Apache conf
<FilesMatch "\.(inc|.*sql|.*~)$"> Order allow,deny Deny from all </FilesMatch> Error Message & logging display_errors = Off
log_errors = On
Hide php extension
expose_php = Off
 
 



Javascript Learning Notes

  • Jun. 3rd, 2009 at 4:06 PM
  • Debugging suddenly stops working, no wath points, console.log starts giving errors:  Disable & enable console that solves this
## If you want know your next set of data. e.g you are showing 10 items per page and want to know if next 10 results exist or not then easiest way is to query twice like

select * from sample limit 0,10
select * from sample limit 10,20

instead of this, we can write
SELECT * FROM sample LIMIT 0,11
if this returns 11 rows, You know next set of rows exist so you can enable next link

MYSQL tips cheat sheat

  • Apr. 15th, 2009 at 12:00 PM

InnoDB

MYISAM

-          Good for low write, high read applications, as table level lock happens for write

-          Key_buffer_size should be ¼ th of total RAM

-          .frm(definition structure of table), .MYI(index), .MYD(data) files for all tables in mysql directory

-          Supports INSERT_DELAYED, LOW_PRIORITY, HIGH_PRIORITY – can be used in statements to prioritize statements

-          Supports fulltext search

-          Supports MERGE

-          Disabling of non unique indexes while loading data will help in fast insertion

-          Supports storage formats fixed, dynamic-row, compressed

MERGE

-          provides logical entity that exceeds MYISAM table size

-          logically query on merge acts as query on all inner myisam tables

-          supports select,insert,update,delete. Should specify in create table that inserted item should go to first or last table.

-          MERGE creates .MRG text file

-          CREATE TABLE table_name (id INT ….) ENGINE=MERGE UNION(table1, table2)

-          Increases number file descriptors opened as it opens all underlying tables.

-          It’s slower as it opens multiple indexes.

INNODB

-          Supports transactions.

-          Supports foreign key and referential integrity

-          Innodb_buffer_pool_size should be 80% of total memory

-          Supports auto recovery after crash.

-          Shares same storage space for all tables(unless innodb files per table set), not constrained by file size limitations of OS.

-          Manages query contention by multi versioning & row level locking

-          innodb_flush_log_at_trx_commit w0/1/2 to set flushing on every commit/every 1 sec/only db flush on every commit

-          show engine innodb status;

 

MEMORY

-          Data and indexes are stored in memory, results in faster performance

-          Uses table level locking

-          Data don’t survive after restart.

-          Don’t support TEXT & BLOB

FEDERATED

-          Allows to use tables from other servers & make them available to clients as if tables were its own.

-          COMMENT=mysql://user_name[:password]@host_name[:port]/db_name/table_name should be added to make federated table.

BLACKHOLE

-          Engine discards all the data stored in, but logs in bin_log if enabled.

-          USES; trigger for insertion

-          Uses; verification of syntax

-          Uses; to measure performance of logging;

-          Uses; measure application performance not related to storage.

 

 

Normalization

1NF: Each column of table has to be atomic, no set values with in column

2NF: non key columns must depend on entire primary key.

3NF: Non key field should not depend on another non key field

4NF: A record should not contain two or more independent multi valued facts about enity.

 

SQL QUERY Syntax

CREATE TABLE

Create table table_name ( column definition ) ENGINE=INNODB;

Create table table_name select …. è creates & populates table with contents returned from select

Create table table_name like table è creates empty table

CREATE TEMPORARY TABLE mail SELECT * FROM mail è creates temp table

 

ALTER TABLE

ADD colmn_name INT NOT NULL

MODIFY id BIGINT

CHANGE old_clmn_name new_clmn_name

t1 RENAME TO t2

RENAME TABLE t1 to t2

DROP TABLE

Drop table if exists no_table;

INDEX

primary kay and unique fields create indexes automatically

ALTER TABLE  ADD INDEX index_name(id) è creates named index

Create index index_name USING btree on table_name (id) è To use btree algorithm for indexing

ALTER TABLE table_name DROP PRIMARY KEY

DROP INDEX ‘primary’ ON t

SHOW INDEX FROM  table_name;

USER VARIABLES

Set @var1=’USA

PREPARED STATEMENTS

Prepare stmt_name from ‘Select …. Where id =?’;

Execute stmt_name using @variable;

Deallocate stmt_name;

PROCEDURE

Create procedure test_proced()

BEGIN … END

TRIGGER

Create trigger trigger_name Before/after Insert/update/delete On table_name For each row Trigger_stmt

FULL TEXT

SELECT COUNT(*) from kjv WHERE MATCH(vtext) AGAINST('Mizraim');
REGEXP
SELECT name FROM metal WHERE name REGEXP '^[aeiou]|er$';

VARIABLES

Set @var_name = select name;

ANY/ALL

Select ….. where pop > ALL ( select …)

Exists, any , all , any <>

SHOW

SHOW TABLE STATUS LIKE 'mail'\G
SHOW CREATE TABLE mail\G

 

QUERY CACHE

Query_cache_type=2 (DEMAND)lets u choose SQL_CACHE in the query

Query_cache_size

Server side query prepared statements cannot use query cache

Select status like ‘Qcache%’ == query cache status hits/misses/free memory

 

Optimizing query

Check slow query log

log-queries-not-using-indexes –enable

-          USE INDEX(P1), to force to use index

-          Analyze table ..

-          Show warnings

For tables that are being accessed frequently

Create separate key cache “set  global city _cache.key_buffer_size=1024; creates city_cache with 1KB size

Cache index world.city in city_cache

Load index into cache worl.cache

-          slect …  PROCEDURE ANALYZE() == will show you optimal values to be used for columns

 

VIEW

Creat view as

Restrictions: - No temporary views, no triggers,

select statement cannot have – subqueries in where, cannot reference variables stored procedure,

WITH CHECK OPTION – causes condition in the where clause of view definition to be checked when updates attempted.

Information_schema.views  will have information about the view

 

IMPORT EXPORT

Load data infile file_name into table table_name;

Select * into outfile file_name  from table_name;

Mysqlimport –h host –u user db_name input_file

Mysqlimport db_name file_name

 

SETTINGS (OPTIMIZATION)

Thread_cache_size nos of extra connections to be kept

Sort_buffer = should be high if lot sorting is used

Table_locks_waited/Table_locks_immediate =  table contention

Show status like ‘Created_tmp%’

 

Table maintenance

-          check table

-          repair table

-          analyze table

-          mysqlcheck

-          myisamcheck

 

Backup

-          Binary backup: mysqlhotcopy, InnoDBhotcopy

-          Text Backup: “select  … into outfile”, mysqldump

Binary backup

Lock the table and Cp files .MYI, .MYD and .frm from mysql directory.

Mysql hot copy

 

 

COMMANDS/TOOLS

Mysqldumpslow

Mysqlbinlog

myisampack

 

Mysql

Mysqldump

Mysqlimport

Mysqladmin

Mysqlcheck

Myisamchk

 

optimizing count(*) for innodb tables

  • Apr. 15th, 2009 at 9:39 AM
count(*) queries on Innodb tables are very slow as it does full table scan for these kind of queries. Ways to improve performance of such queries.

> Use index Keys in query in the query,
Surprisingly using secondary keys (non primary) shows better performance than using primary keys.
Using Primary Key
mysql> select SQL_NO_CACHE count(*) from ball_details use index(PRIMARY);
+----------+
| count(*) |
+----------+
|   156228 |
+----------+
1 row in set (0.14 sec)
Using non primary key
mysql> select SQL_NO_CACHE count(*) from ball_details use index(index_ball_id);
+----------+
| count(*) |
+----------+
|   156228 |
+----------+
1 row in set (0.09 sec)

But mysql is smart enough to use non primary key, explain tells u this
mysql> explain select  count(*) from ball_details;
+----+-------------+--------------+-------+---------------+---------------+---------+------+--------+-------------+
| id | select_type | table        | type  | possible_keys | key           | key_len | ref  | rows   | Extra       |
+----+-------------+--------------+-------+---------------+---------------+---------+------+--------+-------------+
|  1 | SIMPLE      | ball_details | index | NULL          | index_ball_id |       4 | NULL | 152377 | Using index |
+----+-------------+--------------+-------+---------------+---------------+---------+------+--------+-------------+
1 row in set (0.00 sec)


> using show table status
If total count is not critical and 1-2% of difference in total count is fine then, show table status can be used.
e.g: show table status like 'ball_details';
1 row in set (0.02 sec)


Links related to count(*) performance.
http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/

Multiple inheritance in php

  • Apr. 2nd, 2009 at 11:08 AM
Using Mixin logic
You vcan find very good example at http://www.php.net/manual/en/language.oop5.basic.php

Other similar implementation s,

implement several interfaces an then compose a common

classes that implements said interfaces.  this is the workaround
for single inheritance languages.  here is a trivial example,

interface CommonStuff {
  function commonFunction($a, $b);
  function moreCommonFunction($x, $y, $z);
}

interface OftenUsed {
  function imSpecial($c, $d);
  function imMoreSpecial($u, $v, $w);
}

now you might have standard or common implementations
of these, sometimes referred to as mixins (taken from ruby,
i believe [<-- here i go again greg ;)])

class CommonStuffImpl implements CommonStuff {
  function commonFunction($a, $b) {
    return $a + $b;
  }

  function moreCommonFunction($x, $y, $z) {
    return $x + $y + $z;
  }
}

class OftenUsedImpl implements OftenUsed {
  function imSpecial($c, $d) {
    return $c - $d;
  }

  function imMoreSpecial($u, $v, $w) {
    return $u - $v - $w;
  }
}

and now finally for the multiple inheritance workaround;
suppose you have a class that needs to be both
CommonStuff and OftenUsed; you simply implement
both the interfaces, then delegate to the mixins, and
viola!  multiple inheritance the single inheritance way ;)

class MultiInherit implements CommonStuff, OftenUsed {
    private $commonStuffImpl = null;
    private $oftenUsedImpl = null;

    public function __construct() {
       $commonStuffImpl = new CommonStuffImpl();
       $oftenUsedImpl = new OftenUsedImpl();
    }

   /// now make sure to implement the interfaces and delegate
   function commonFunction($a, $b) {
      return $this->commonStuffImpl->commonFunction($a, $b);
   }

   function moreCommonFunction($x, $y, $z) {
      return $this->commonStuffImpl->moreCommonFunction($x, $y, $z);
   }

  function imSpecial($c, $d) {
    return $this->oftenUsedImpl->imSpecial($c, $d);
  }

  function imMoreSpecial($u, $v, $w) {
    return $this->oftenUsedImpl->imMoreSpecial($u, $v, $w);
  }
}

Reference : http://www.nabble.com/Multiple-Inheritance-td15717829.html


Not very well known unix commands

  • Feb. 8th, 2009 at 12:01 AM
lsof  lists the files attached/opened by currently running processes
watch runs commands periodically,outputs in full screen
e.g watch -n 2 ls -l ==> runs 'ls -l' every 2 seconds
       watch -d ls -l ==> highlights diff in out puts
pstree shows tree of processes
e.g pstree
init-+- getty
       |-  getty
       |-  getty
fuser Identify the process used by file or socket
e.g fuser /home/notroot ==> lists process that r using /home/notroot
       fuser -km /home/notroot ==> kills all the processes that r using /home/notroot



Copying mysql schema to new database

  • Feb. 4th, 2009 at 7:03 PM
on source database machine
mysqldump --no-data --all-databases -u root -pPASSWD >>cricket_schema.sql
On destination machine
mysql -u root
mysql> source /home/sudhirp/cricket_schema.sql;

List of commands that will let you find ports that are being used and process its attached to
Linux:
$ sudo netstat -lnptu|grep apache
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      11151/yapache
tcp        0      0 0.0.0.0:4080                0.0.0.0:*                   LISTEN      11151/yapache

BSD:
$ sockstat -4l|grep apache
nobody   yapache    21369 19 tcp4   *:80                  *:*
root     yapache    21367 19 tcp4   *:80                  *:*
root     yapache    6429  20 tcp4   *:1556                *:*

$ grep ftp /etc/services
ftp-data        20/tcp
ftp-data        20/udp

$ sudo netstat -lnptu|grep apache
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      11151/yapache
tcp        0      0 0.0.0.0:4080                0.0.0.0:*                   LISTEN      11151/yapache


for more details read this article, http://blogs.techrepublic.com.com/security/?p=443

squid cache miss problem while using curl

  • Jan. 13th, 2009 at 3:48 PM

 

By default Curl header is set no-cache so every time u hit a URL with proxy(squid) it’s a cache miss. Unset pragma header while curling to avoid this problem.

 

From command mode

curl -H "Pragma:"  -x 'localhost:3128'  URL

 

From PHP code

curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("Pragma: "));

 

reference
http://in.php.net/curl_setopt
http://curl.haxx.se/docs/manpage.html#-H--header
http://curl.haxx.se/mail/archive-2002-07/0018.html

How Auto complete/drop down suggest works?

  • Dec. 14th, 2008 at 2:48 PM
Its intresting how auto complete or url drop downs in firefox works. U will get so many examples about how its implemented on fronend,
but real problem lies at backend how algorithms and data structures used.

Below are the few ways that could be used
1> Modifying simple trie like structure. every charecters on the path will have remaining charecters in the word. go through below link  for more info
http://rmandvikar.blogspot.com/2008/10/trie-examples.html

But this can only be used for matches.

2>usig suffix trees
http://www.allisons.org/ll/AlgDS/Tree/Suffix/

but ther is lot more to it, like how to rank or prioritize strings. I could not find implementations of these.anybody have any links or suggestions? would be really helpful
Few days back cleared PHP 5 certification exam. Will share my thoughts about preparation

First of all you should have at least 6moths-1 Year of coding experience in PHP unless ur really geeky kind of a person. Exam isn't  very hard but u need to have good preparation. It had many question related other web technologies like  XML, security and not just PHP. I would rank exam difficulty as medium. Some thing very irritating is, lot of questions about function names and whats more some of these function names have to be entered, u got it right no multiple choice. Surprisingly lot of questions about file uploads, i didnt do well in those.

Now coming to preparation Not sure whats the value of this certification in terms of getting job etc but good to test ur php skills. Hope this helps.

UPDATE:
Recently found new blog regarding preparation http://saidur.wordpress.com/2008/12/05/how-to-preapre-for-zend-php5-certification-exam/ .   But so many congrats!  .... i never knew ZCE is reputed or difficult :)

Useful examples of using find command

  • Nov. 20th, 2008 at 4:54 PM
Find
To find and delete files older than 3 days
find /home/y/var/dbdist/feedfactory/  -mtime +3 -exec sudo rm {} \;

Get CPU Details(like type cpu, nos of cores etc)
FreeBSD
sysctl -a | egrep -i 'hw.machine|hw.model|hw.ncpu'
LINUX
cat /proc/cpuinfo

Commands work on redhat linux but should work on all *NIX systems
  • top : Will show you the current memory and cpu utilization
  • ps -aux : Shows the %mem,%cpu  etc consumed by the process, use it with process id to get details of that process only
  • vmstat 2 : shows virtual memory statistics for every 2 seconds.
  • cat /proc/<pid>/status : Shows you more detailed information about the process
  • ipcs -u : Shows shared memory status in segments, semaphores and queues
  • systat vmstat 1 : Shows Detailed virtual memory usage(Only in Free BSD).

xargs

  • Nov. 17th, 2008 at 9:28 PM
ls | grep scorecard|xargs grep -l 'matchid="2"'

Move all files from directory $1 to directory $2, and use the xargs -t option to echo each move as it happens:

 

ls $1 | xargs -I {} -t mv $1/{} $2/{}

The following   example finds the unique owners of all the files in the /bin directory: 

 

# all on one line
find /bin -type f -follow | xargs ls -al | awk ' NF==9 { print $3 } '|sort -u
reference http://www.unixreview.com/documents/s=8274/sam0306g/

Command to compress uncompress .tar.gz file

  • Nov. 13th, 2008 at 4:31 PM

Compress folder Test/ to Test.tar.gz

tar czfv Test.tar.gz Test/
czfv = ‘Compress Zip File Verbose’
If you want bzip files, use ‘j’ instead of ‘z’.

Uncompress Test.tar.gz to folder Test/

tar -xzf Test.tar.gz
x = ‘eXtract’
Again, if you want bzip files, use ‘j’ instead of ‘z’.

http://en.wikipedia.org/wiki/Tar_(file_format)

Reference
http://txt.binnyva.com/2007/02/command-to-compressuncompress-targz-and-tarbz2-files/
 

Vi replacing characters with new line

  • Oct. 13th, 2008 at 2:57 PM
Vi replacing characters with new line

:%s/<char>/^M/g

NOTE: ^M should typed by holding control key first type^ then M

xmllint : view formatted xml in vim

  • May. 10th, 2008 at 8:31 PM

Vim doesn't format xml structures. Its very difficult to read xml in vim & its very kludgy too. Here is tip to solve this by xmllint,  use this command

    xmllint --format filename | vi -
by default xmllint should have been installed on linux boxes.