:::: MENU ::::

Grep with context

Small thing but extremely useful. Getting context with grep.

-C – Print num lines of leading and trailing context surrounding each match. The default is 2 and is equivalent to -A 2 -B 2. Note: no whitespace may be given between the option and its argument.

grep -C 5 file.txt

Full MySQL dump

If you would like to dump all tables and triggers and stuff try this one:

mysqldump -uUSER -p -R --opt --triggers DB > DB_DUMP.sql

but if you have some troubles with permissions for locking tables use --lock-tables=false

mysqldump -uUSER -p -R --opt --triggers --lock-tables=false DB > DB_DUMP.sql

and to dump all databases you have

mysqldump -uUSER -p -R --opt --triggers --lock-tables=false --all-databases > DB_DUMP.sql

also, great post on StackExchange with a script for dumping all databases without databases like `mysql`, `information_schema`, `performance_schema`, `sys`.

# Collect all database names except for
# mysql, information_schema, and performance_schema
SQL="SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN"
SQL="${SQL} ('mysql','information_schema','performance_schema')"

mysql ${MYSQL_CONN} -ANe"${SQL}" > ${DBLISTFILE}

for DB in `cat ${DBLISTFILE}` ; do DBLIST="${DBLIST} ${DB}" ; done

MYSQLDUMP_OPTIONS="--routines --triggers --single-transaction"
mysqldump ${MYSQL_CONN} ${MYSQLDUMP_OPTIONS} --databases ${DBLIST} > all-dbs.sql

iframe communication on different domains, Facebook App example

You have a Facebbok App. When you go to that app you have a Facebook page and your app is embedded via iframe.

Later on it turns out that within your code you have an iframe with something, lets say some kind of form but in different domain. And you have a problem, because due to Same Domain Origin Policy you can’t talk between those iframes. There is a nice solution for that.

You can use a friendly helper from main domain that is embedded in forms iframe, as 3rd level you’d say.

So the main idea is to change iframe src if you form file, where you can embed this helper.

Take a look on this brief schema.



		<iframe src="http://domain-a.com/app.html" style="width: 500px; height: 300px;"></iframe>


		<iframe src="http://domain-b.com/form.html" id="form"></iframe>

		<script type="text/javascript">
			function redirect(url){  
			    window.location.href = url;
			function resize() {
			    document.getElementById('form').height = 500;


		<iframe id="helper" src="" width="0" height="0" ></iframe>
		<a onclick="redir();">REDIR</a>
		<a onclick="resize();">RESIZE</a>
		<script type="text/javascript">
			function redir() {
			    var helper = document.getElementById('helper');
			    helper.src = "http://domain-a.com/iframeparent/helper.html?action=redir";
			function resize() {
			    var helper = document.getElementById('helper');
			    helper.src = "http://domain-a.com/iframeparent/helper.html?action=resize";


	<body onload="start();">
		<script type="text/javascript">
			function start(){
			    var action = getParam('action');
			    if (action =='redir') {
			    } else if (action == 'resize') {
			function getParam( name )
			  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
			  var regexS = "[\\?&]"+name+"=([^&#]*)";
			  var regex = new RegExp( regexS );
			  var results = regex.exec( window.location.href );
			  if( results == null )
			    return "";
			    return results[1];

source: Resizing an iframe based on content

PHP foreach and passing by reference

$arr = array(1,2,3,4,5,6);

foreach($arr as $v){
    $arr[] = 1;
    echo $v;

output: 123456

$arr = array(1,2,3,4,5,6);

foreach($arr as &$v){
    $arr[] = 1;
    echo $v;

output: 1234561111111111111111111111111111111111111111111111…
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 32 bytes) in /Users/gradzinski/dev/playground/foreach.php on line 7

So within the foreach loop we are adding 1 to referenced array arr and this is why we are failing into infinite loop.

Below you can find PHP foreach manual explanation that may put some light in that behaviour.

Referencing $value is only possible if the iterated array can be referenced (i.e. if it is a variable). The following code won’t work:

<?php foreach (array(1, 2, 3, 4) as &$value) { $value = $value * 2; } ?>

PHP and passing object by reference

It is not really that we are passing object instances in PHP by reference. It is a little bit tricky, but the manual has this covered!

As of PHP 5, an object variable doesn’t contain the object
itself as value anymore. It only contains an object identifier which allows
object accessors to find the actual object. When an object is sent by
argument, returned or assigned to another variable, the different variables
are not aliases: they hold a copy of the identifier, which points to the same

class A { 
	public $foo = 1; 

$a = new A; 
$b = $a; 
$a->foo = 2;
$a = NULL;
echo $b->foo."\n"; // 2

$c = new A;
$d = &$c;
$c->foo = 2;
$c = NULL;
echo $d->foo."\n"; // Notice:  Trying to get property of non-object…