Deprecated: I don’t maintain this site anymore …

March 27, 2016

Thanks for reaching out to this site but I am not maintaining this anymore…

I will blog (if I do 🙂 ) at shaikhul.wordpress.com

 

Cheers

Shaikhul (Shaymol)


My first Chrome Extension

July 24, 2010

Yesterday i was playing with chrome extension. I search for a tutorial and found that google provides the best and easy tutorial to build a chrome extension. When i have done with the tutorial i see its too easy to build a chrome extension, prerequisites is only in depth knowledge of Javascript and HTML DOM and built a simple extension that will show today’s bangla date. To get the bangla date it will use a javascript widget from tareq’s site.

Here is a screenshot of the extension

Bangla date chrome extension

You can check this extension from chrome extension directory

Cheer$
Do not reinvent the wheel – anonymous


Handling Fatal Error in PHP way

July 22, 2010

In PHP when any fatal error happen, (ex. Fatal error: Call to undefined function phpinf() .. or .. Class ‘User’ not found etc ..) it stops executing script, as a result a broken page is shown. This is very unexpected behavior to users. So we need to handle this and show an user friendly error message to the browser.

To handle the fatal error and some other errors we can use a method register_shutdown_function which accepts a function name as a param that will be called when current script execution completed. So in case of any fatal error happen, the script execution will end and call the registered method instantly. and thus we can takeover the execution flow and do our custom task, ex. report admin about the error, show user an appropriate error message etc. By the way, this registered method will be called each time current script ends its execution. So in that method we need to check the last error if there is any real error happen. There are many PHP Error Constants.

Here is a simple example

/*
* define the shutdown method, which will be called each time this script execution ends
*/
function shutdown()
{
// get last error
$lastError = error_get_last();

// show user error message if any error happen
if(!is_null(criticalError)){
// get error type
$errorType = $lastError['type'];
$criticalError = array(E_ERROR , E_PARSE , E_CORE_ERROR, E_COMPILE_ERROR ,E_RECOVERABLE_ERROR);
if(in_array($errorType, $criticalError )){
// mail to admin
// mail('admin@example.com', ....);
}

// print a proper message to output
echo "Sorry, an unexpected error happen, please check later";
}
}

// register the shutdown function
register_shutdown_function('shutdown');

// now do some fatal error like call a class which does not exsits
User::load(); // here User Class is not included

Here is the output ..

Sorry, an unexpected error happen, please check later

The more you do mistake, the more you get the chance to learn 😉
Cheer$


php Classes n Methods are Unicoded ;)

July 21, 2010

wao PHP supports unicoded user defined classes and functions. Try out the following simple code yourself if you dont believe me :D…


class āĻŦāĻžāĻ‚āĻ˛āĻž{
public function __toString(){
return 'āĻŦāĻžāĻ‚āĻ˛āĻžāĻ¯āĻŧ āĻ•ā§āĻ˛āĻžāĻ¸ āĻ˛āĻŋāĻ–āĻŋ ;)';
}

public function āĻšā§‡āĻ˛āĻ“_āĻ“āĻ¯āĻŧāĻžāĻ°ā§āĻ˛ā§āĻĄ(){
echo 'āĻ†āĻ‡ āĻāĻŽ āĻ āĻšā§‡āĻ˛āĻ“_āĻ“āĻ¯āĻŧāĻžāĻ°ā§āĻ˛ā§āĻĄ āĻŽā§‡āĻĨāĻĄ :āĻĻ ';
}
}

$obj = new āĻŦāĻžāĻ‚āĻ˛āĻž;
echo $obj;
echo $obj->āĻšā§‡āĻ˛āĻ“_āĻ“āĻ¯āĻŧāĻžāĻ°ā§āĻ˛ā§āĻĄ();

Here is the output

āĻŦāĻžāĻ‚āĻ˛āĻžāĻ¯āĻŧ āĻ•ā§āĻ˛āĻžāĻ¸ āĻ˛āĻŋāĻ–āĻŋ ;)
āĻ†āĻ‡ āĻāĻŽ āĻ āĻšā§‡āĻ˛āĻ“_āĻ“āĻ¯āĻŧāĻžāĻ°ā§āĻ˛ā§āĻĄ āĻŽā§‡āĻĨāĻĄ :āĻĻ

N.B. This is only for fun 😉 , dont try to use this approach, as your teammate might get puzzled about the class names if she does not understand Banagli 😉


++$city where $city = ‘NY’ ?

July 10, 2010

I have found something about the unary op in PHP. I was checking it in PHP 5.2.1.
Check the following code and guess the output. When you have done check with the answer given below.

$city='NY';
echo ++$city , ' '; // think well ;)
echo $city = $city + 1, ' '; // is there any diff ?

$cg = '3.53 is my cg';
echo ++$cg, ' ';
echo $cg = $cg + 1;

Did you get your answer? then check with the following

NZ 1 3.53 is my ch 4.53

Let me explain if not matched with your answer. When php do an unary operation like (++$city) on character variable it follows perl style, its actually increment to next character, say if it is A it will B and so on… so for the example it will be NZ.
But when the same is done by using binary operation (+), in that case first it convert the string into integer (and this is called type juggling ) and in that case it converts it into 0 then do the plus operation, so the output here is 0 + 1 = 1.

So be careful what you actually want and select operator accordingly!

Cheer$


fun with $ in Js

July 10, 2010

have you ever tried with character javascript variable ? trust me i am not kidding ;). javascript supports two special one character variable. one is underscore(_) and another one is ($). Try out in your firebug console.

_ = ' i am an underscore';
$ = ' 6 million dollar man';
alert(_ + ' : ' + $);

Isn’t it interesting ? but dont use it professionally if you are not master in javascript as it would be make others ins puzzle about what the variable means.

Cheer$
$haymoL


did you typecast (array) $obj before ?

July 9, 2010

Today i found an interesting thing in typecasting. We actually typecast float to int, string to int etc. but have you ever tried to typcast an object to an array ? what would it return in that case ? well its very interesting to me. It will return an array with all properties of the object irrespective to access modifiers!

lets have a look in a simple example, say an User class have two property field one is private (userId) and another is public (userEmail) and two setter method, now if we create an instance of that class and set those two fields and then convert it into array, it will return thoese two fields as array index.


class User{
private $userId;
public $userEmail;

public function setUserId($userId){
$this->userId = $userId;
}

public function setUserEmail($email){
$this->userEmail = $email;
}
}

$user = new User();
$user->setUserId('123');
$user->setUserEmail('shaymol@ymail.com');
$userArr = (array)$user;

print_r($userArr);

The above code will result as follows

Array
(
[UseruserId] => 123
[userEmail] => shaymol@ymail.com
)

Just look at the private field, it append the Class name with the private property in the array index. and public field is as it is. isn’t it interesting 😉

Cheer$
Happy Phping ….


Never trust on user data

July 9, 2010

In web development we most oftenly saves user provided data into our database and show them to the browser. But we never knows if user put clean data or malicious data which may cause security leak.

Say for example i have a form to collect user name and email.

Now say user put name = “iamgooduser” and password =”mail@email.com”.

In php we are collecting this via $_POST or $_GET or $_REQUEST, and if we try to save these data as it is like

$sql = “insert into user_table set name = ” . $_POST[‘name’] . ‘ and ‘ . ‘ email = ‘ . $POST[’email’], then there is the possibility for security leak , like user may enter name =”myname Or 1=1″ , this is called SQL injection

So we need to filter user data before doing any db operation to avoid such sql injection.
To do so we can use mysql_escape_string($data) or mysql_real_escape_string($data).

Also when showing user data to browser we need to escape special characters to avoid XSS (Cross site scripting) .
ex (user can give comment = ‘alert(document.cookie)’). when rendering this into browser without escaping special chars, it will alert the browser cookie. Thats why we need to escape special chars, to do so we can use php function htmlentities($data).

Cheer$
Prevention is better than cure 😉


Forking with PHP-CLI

February 9, 2010

You know PHP does not support multi-threading as Java does, but it has linux like process control functions which can be used from command line and off course under linux platorm.

In Linux, fork method create a child process that differs with the parent process only its PID and PPID. You will get more info about fork from here. By forking we can execute multiple child processes at a time.

We can do similar process control in PHP by using PHP’s process control functions. and it is not enabled by default with PHP. For this we need to install PHP with –enable-pcntl configuration option. Please check the php manual about its installation details from here.

The idea is simple, get a process id, if it is a parent process we will wait, if it is a child process we will execute what we want to.

ex. from php manual

$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// we are the parent
pcntl_wait($status); //Protect against Zombie children
} else {
// we are the child
}

Now lets see how we can implement it with real code. Some years ago, Hasin bhai post a blog about hacking slideshare slides at here. Please visit the link and have a look at the code. We are going to update it with forking. The forking idea will be the same.

Here is the code with forking.

In the script it will create child processes and download each slide from there and thus it makes it parallel processing from serial iterating. You can check it without the forking and check the difference. The download time will vary by download speed though.

Happy forking
ShaymoL


Useful RegEx Pattern link

November 18, 2009

Regular expression is very important in any programming . Sometimes we cannot think correctly for some common regular expressions. Here is a link where i found very handy regular expression set.

Regular Expressions Patterns and Examples

This will be very helpful i guess.

Cheer$