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)
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)
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
You can check this extension from chrome extension directory
Cheer$
Do not reinvent the wheel – anonymous
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$
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 đ
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$
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
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 ….
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 đ
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$