Monday, March 9, 2009
Good Habit and Bad habit In PHP
1.Reflective yet concise names.
2.Manageable, focused functions.
3.Documented functions and classes.
4.Programming defensively.
5.Reusable functions with parameters.
There are some bad habit in PHP.
1.Ambiguous or meaningless names
2.Really long functions (that do a lot)
3.Missing and redundant function documentation
4.Not handling errors at all
5.Similar sections of code
Some good programming habits in PHP
1. Use good naming.
2. Take smaller bites.
3. Document your code.
4. Handle error conditions.
5. Never, ever, copy and paste.
Here Detailed disscussion about
1.Use Good NamingUsing good naming is the most important habit because descriptive names make code easier to read and understand. The understandability of your code ultimately determines whether it can be maintained in the future. Even if the code you write contains no comments, if it's easy to understand, it will be much easier for you or someone else to change, if necessary. Your aim, when developing this practice, should be to use good naming to make your code read much like a book.
Bad habit: Ambiguous or meaningless names
2.Take smaller bites.It's easy to get focused on solving a problem and start coding away. While you're solving an immediate problem, you keep typing, letting your functions get longer and longer. That's not a problem over the long term, as long as you go back later and refactor the code into smaller bites.
Refactoring is a great idea, but you should develop the habit of writing shorter, more focused methods the first time. Shorter methods that can be viewed in one window are easier to understand. When a method is too long to be viewed all at once in a window, it decreases in understandability because you can't quickly follow its entire flow from beginning to end.
When building methods, you should also form the habit of constructing them so they do one thing and one thing only. There are several reasons for such diligent focus in your method writing. First, methods are more easily reused when they do one thing and do it well. Second, such methods are easier to test. Third, such methods are easier to understand and change — if necessary — the simpler they are.
3. Document your code.Documenting your code well sometimes seems as difficult as writing it in the first place. Knowing what to document is tricky because it's tempting to document what the code is doing. Documenting the code's intention is a better idea. In the header blocks of functions that may not be obvious, tell the reader the expected inputs and outputs of the methods and the original intent.
Documenting what the code is doing is common, but unnecessary. If the code is so confusing that you have to document what it's doing, take that as a hint that you should rewrite the code to make it easier to understand. Develop the habit of using good naming and smaller methods and structures to make your code more readable without having to comment what it does.
4.Handle errorsIt's been said that when you're writing robust applications, error-handling code seems to follow the 80/20 rule: 80 percent of the code is dedicated to handling exceptions and validations, and 20 percent of the code does the actual work. It's natural when writing code to do happy-path coding. This means writing code that works well for the basic conditions, when all the data is valid and all the conditions are as expected. But such code can be fragile over the application's lifetime. At the other extreme, you may spend too much time writing code for conditions that may never be encountered.
This habit is about finding the balance between doing enough error handling and not doing so much gold plating that your code is never finished.
5.Never, ever copy and pasteThe ability to copy code from one place and paste it into your code editor is a double-edged sword. On one hand, it saves a lot of errors when you're retyping from an example or a template. On the other hand, it makes proliferation of similar code way too easy.
Be on your guard against copying and pasting code between parts of your application. When you find yourself doing it, stop and ask yourself how you could rewrite the section of code you're copying to be something that you can reuse. Putting code in one place allows you to more easily maintain your code, to a large degree, because changes need to be made in only one place.
Article come from IBM
Monday, March 2, 2009
Flourish, A Developer-Friendly PHP5 Library
flourish is an object oriented library by which we can reduce our code into very simple manner
It’s not an MVC framework and it doesn’t try to solve every problem. Instead, it focuses on being small, portable, well documented and easy to use.
Flourish provides classes to simplify many common and repetitive tasks in PHP with class APIs that are simple and intuitive. It helps produce code that is easy to write, and more importantly, easy to read and maintain.
The project is very well documented, and to get started you need PHP 5.1+, it include an ORM for supporting MySQL, PostgreSQL, SQLite and Microsoft SQL Server, in addition for being built with internationalization and localization in mind. Flourish is available under the MIT license.
Thanks for more information you can see the link
Sunday, July 20, 2008
implement captcha system in your website
captcha is the test which is used to confirm that the response is not generated by a computer. means response is generated by a human being.
How to implement the captcha system in your website?
This script generates images (known as "Captcha's") which contain security codes used for protecting a form from spam bots. By encoding a 'password' inside an image and asking the user to re-enter what they see you can verify the user is a human and not automated software submitting your form.
session_start();/** File: CaptchaImages.php*
Author:* Copyright:2008Ankur Garg
Date: 20/07/2008
* Requirements: PHP 4/5 with GD and
class CaptchaImages {
var $font = 'monofont.ttf';
function generateCode($characters) {
/* list all possible characters, similar looking characters and vowels have been removed */
$possible = '23456789bcdfghjkmnpqrstvwxyz'; $code = ";
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0,strlen($possible)-1), 1);
$i++;
}
return $code;
}
function CaptchaImages($width='120',$height='40',$characters ='6') {
$code = $this->generateCode($characters );
/* font size will be 75% of the image height */
$font_size = $height * 0.75;
$image = imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width),mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0,$this->font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0,
$x, $y, $text_color,
$this->font , $code) or die
('Error in imagettftext function');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['security_code'] = $code;
}
}
$width = isset($_GET['width']) &&
$_GET['height'] < 600 ? $_GET['width'] : '120';
$height = isset($_GET['height']) &&$_GET['height'] < 200 ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) &&$_GET['characters'] > 2 ? $_GET['characters'] : '6';
$captcha = new CaptchaImages($width,$height,$characters);
?>
Copy and paste the above code and save it on your webserver as CaptchaImages.php.You will also need to place a copy of the "Monofont" font in the same directory as the CaptchaImages.php file. (Alternatively you can replace the line var $font = 'monofont.ttf'; with the name of whatever font you want to use)download . If you do not have this font you can take for me just give a request on ankur.garg@wizom.com.
Place the following code on your form. This will generate an image with a random string of characters along with the text field where the user will retype the code.
php" />
Security Code:
"security_code" type="text" /> You can also specify certain options for the image by passing them as variables to CaptchaImages.php.
The options available are the width and height of the image and the number of characters
php?
width=100&height=40&characters=5″ alt="captcha" />
name="security_code" type="text" />
session_start();
if(($_SESSION['security_code'] == $_POST[ security_code']) && (!empty($_SESSION['security_code '])) ) {
// Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
unset($_SESSION['security_code' ]);
} else {
// Insert your code for showing an error message here
}
?> Join my Business Networking site