Thursday 27 September 2012

How To Use Captcha in HTML Forms

0 comments


HTML forms are the essential needs of almost any website viz. contact us form. A contact us form is very useful as it aids your visitors to communicate easily in a simple way. A simple form, without a captcha on it are the chief look-outs of hackers and spammers. It is essential to secure your form against all 'holes' that those hackers are searching for.


How does the spammers/hackers exploit HTML forms?

1) As a relay for sending bulk unsolicited emails.

If you are not validating your form fields (on the serve side) before sending the emails, then hackers can alter your email headers to send the bulk unsolicited emails. (also known as email injection)


2) For sending spam messages to you.

There are programs known as 'spam-bots' that leech through the web pages looking for web forms. When found, those 'bots' just fills the fields with a spam message and submits. Eventually you will start getting many hundred submissions send by those spam bots and you will find it difficult to separate genuine submissions from spam messages.

The solution for this problem is to use a mechanism to identify human submitters from 'bots'. CAPTCHA is one of such tests.


Adding Captcha


Captcha is an image with a code written on it. The website visitor is required to read the code on the image and enter the value in a text field. If the word entered is wrong, the form submission is not processed. As CAPTCHA is a smartly blurred image, the spam bot can't read it. So the form cannot be auto-submitted by a 'bot'.



Contact form with Captcha


<form method="POST" name="contact_form"
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<label for="name">Name: </label>
<input type="text" name="name"
value="<?php echo htmlentities($name) ?>">
<label for="email">Email: </label>
<input type="text" name="email"
value="<?php echo htmlentities($visitor_email) ?>">
<label for="message">Message:</label>
<textarea name="message" rows=8 cols=30>
<?php echo htmlentities($user_message) ?></textarea>
<img src="captcha_code_file.php?rand=<?php echo rand(); ?>"
id="captchaimg" >
<label for="message">Enter the code above here :</label>
<input id="6_letters_code" name="6_letters_code" type="text">
<input type="submit" value="Submit" name="submit">
</form>


The HTML form above contains the fields for name, email and message. In addition, we have the CAPTCHA image. The <img> tag for the CAPTCHA image points to the script captcha_code_file.php. The PHP script in 'captcha_code_file.php' creates the image for the captcha and saves the code in a session variable named '6_letters_code'.



Validating the CAPTCHA


When the form is submitted, we compare the value in the session variable(6_letters_code) with the submitted CAPTCHA code( the value in the text field 6_letters_code). If the codes match, then we proceed with emailing the form submission. Else we display an error.

Here is the code that does the server side processing:


if(isset($_POST['submit']))
{
  if(empty($_SESSION['6_letters_code'] ) ||
    strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
  {
   //Note: the captcha code is compared case insensitively.
   //if you want case sensitive match, update the check above to
   // strcmp()
    $errors .= "\n The captcha code does not match!";
  }
  if(empty($errors))
  {
    //send the email
    $to = $your_email;
    $subject "New form submission";
    $from = $your_email;
    $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
    $body = "A user  $name submitted the contact form:\n".
    "Name: $name\n".
    "Email: $visitor_email \n".
    "Message: \n ".
    "$user_message\n".
    "IP: $ip\n";
    $headers = "From: $from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    mail($to, $subject, $body, $headers);
    header('Location: thank-you.html');
  }
}



Customizing the CAPTCHA

The CAPTCHA script in the sample code download can be customized. If you open the script, you can see the first few lines of the code as shown below:



$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
$font = './monofont.ttf';
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 0;
$random_lines = 20;
$captcha_text_color = "0x142864";
$captcha_noise_color = "0x142864";


You can change the size of the CAPTCHA by changing $image_width & $image_height. The number of characters in the CAPTCHA can be changed by updating $characters_on_image. Similarly, the text color of the CAPTCHA can be customized by updating $captcha_text_color. The code adds some 'noise' in the image by adding random lines and dots. you can increase or decrease the noise. Please note that increasing the noise may make it difficult for your genuine visitors to read the code.







Screenshot -




Continue reading →
Tuesday 25 September 2012

How to Create a Horizontal Dropdown Menu with HTML, CSS and jQuery

1 comments



Menus play an essential part on the web. They allow users to find their bearings and help them navigate your website. When designing menus, usability is the key.

Beginners often struggle with the basics. In this tutorial I'm going to show you how to create a simple, usable and functional horizontal menu with HTML and CSS. I will also dive a little bit into jQuery to add animations to your menu.

This tutorial assumes you have a basic knowledge of HTML and CSS. It’s recommended to use a CSS reset for consistency. I use the one by HTML5Doctor.



The Basics


Let's start with the basic HTML structure of the menu:


<ul id="coolMenu">
<li><a href="#">Home</a></li>
<li><a href="#">Tutorials</a></li>
<li><a href="#">Gadgets</a></li>
<li><a href="#">Tips and Tricks</a></li>
<li><a href="#">Contact us</a></li>
</ul>


A menu consists of an unordered list, and each list item contains a link with the text. Don’t create unnecessary divs. You don’t need any.

To add a sub menu simply nest another unordered list inside the item that's going to have the sub menu, like this:


<ul id="coolMenu">
<li><a href="#">Home</a></li>
<li>
            <a href="#">Tutorials</a>
            <ul>
        <li><a href="#">PHP</a></li>
        <li><a href="#">MySQL</a></li>
        <li><a href="#">JavaScript</a></li>
        <li><a href="#">HTML</a></li>
        <li><a href="#">CSS</a></li>
        <li><a href="#">jQuery</a></li>
        <li><a href="#">Java</a></li>
    </ul>
        </li>
<li><a href="#">Gadgets</a></li>
<li><a href="#">Tips and Tricks</a></li>
<li><a href="#">Contact us</a></li>
</ul>


As you can see, creating the structure is very simple. This is how it should look in your browser at this stage:




There are multiple ways to set up the CSS for a horizontal menu. After many years I found that this is the quickest and cleanest way to do it:


#coolMenu,
#coolMenu ul {
    list-style: none;
}
#coolMenu {
    float: left;
}
#coolMenu > li {
    float: left;
}
#coolMenu li a {
    display: block;
    height: 2em;
    line-height: 2em;
    padding: 0 1.5em;
    text-decoration: none;
}
#coolMenu ul {
    position: absolute;
    display: none;
    z-index: 999;
}
#coolMenu ul li a {
    width: 80px;
}
#coolMenu li:hover ul {
    display: block;
}



  • I decided to float the whole menu to contain it but you can use overflow hidden or even set a fixed width for the same purpose.
  • It is important to float the list elements rather than the links.
  • The links should be displayed as blocks, otherwise, they won’t behave as expected.
  • Absolute position the submenu and hide it to remove it from the regular flow and make it invisible. Also, set a high z-index to prevent the submenu from showing behind other elements.
  • Set a height for the link elements and the line-height equal to the height to center the text vertically. By specifying a fixed height instead of just using padding you avoid flickering problems with jQuery animations later on.
  • Even though it’s not necessary to set a fixed width for the submenu items, it’s always a good practice. It allows you to style them more consistently later on.
  • Notice that the hover state is set on the list element and not the link.


With all this set, the menu should be already working. Try opening it in your browser and hovering over the third option to show the sub menu.






Improving Usability


This step will cover how to style the menu with some basic CSS to make it more accessible.


/* Main menu
------------------------------------------*/
#coolMenu {
    font-family: Arial;
    font-size: 12px;
    background: #2f8be8;
}
#coolMenu > li > a {
    color: #fff;
    font-weight: bold;
}
#coolMenu > li:hover > a {
    background: #f09d28;
    color: #000;
}

/* Submenu
------------------------------------------*/
#coolMenu ul {
    background: #f09d28;
}
#coolMenu ul li a {
    color: #000;
}
#coolMenu ul li:hover a {
    background: #ffc97c;
}


Keep in mind this is very basic, and is meant to be just an example. You can style this however you want. The important thing to remember here is, as I mentioned before that the hover states, are styled in the list items and not the links.

This is how the menu looks so far:





Adding Animations


This final step is not necessary but it’ll help you add animations to your menu with simple jQuery. The first thing you need to do, of course, is to call the latest jQuery plugin on your website:



Now, let’s add a "noJS" class to the submenu to be able to unhook the hover css state in jQuery. This will also ensure that the menu will still work when javascript is disabled.


<li>
    <a href="#">Tutorials</a>
    <ul class="noJS">
<li><a href="#">PHP</a></li>
<li><a href="#">MySQL</a></li>
<li><a href="#">JavaScript</a></li>
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
<li><a href="#">jQuery</a></li>
<li><a href="#">Java</a></li>
   </ul>
</li>


You also need to add the class to the sub menu CSS hover state:


#coolMenu li:hover ul.noJS {
    display: block
}


Now that everything is set let’s add some magic:


$(function(){
    $('#coolMenu').find('> li').hover(function(){
        $(this).find('ul')
        .removeClass('noJS')
        .stop(true, true).slideToggle('fast');
    });
});

The code is pretty explanatory. The script finds the immediate children list items and adds a hover function. Inside the function it removes the "noJS" class since we’re using JavaScript, and then it tells the menu to slide down on hover and to slide up on un-hover. This is achieved with the slideToggle function. The stop function prevents the animation from repeating itself if we hover multiple times.







Continue reading →
Thursday 13 September 2012

Apple announced the 4-inch iPhone 5 smartphone

1 comments



Apple today announced the most awaiting phone for all the Apple lovers, the Apple iPhone 5. Many of the rumors came while talking about iPhone 5. Among those rumours was the 4-inch display and that the phone is thinner and higher than its predecessor, the iPhone 4S. Also chinese iPhone 5 was also launched in china market way before its launch. 

The 7.6mm width makes it the thinnest iPhone so far. Making the phone thinner is probably a good enough reason for many to updating their existing 4S to the new model. But making a thin phone also limit the possibility for Apple to produce a much better camera as it must be room for the lens and sensor which must be stacked on top of each other. Therefore, the Iphone 5 has similar camera specifications. It is a 8 megapixel camera with a BSI sensor, auto focus, face detection and 1080p video capture. The front camera has an increased resolution of 720p.

The new phone has a 4-inch retina display that has a 640 x 1136 pixel resolution. The width of the display is the same as iPhone 4, but the height is increased with 176 pixels up to 1136. This makes up to add one more row for icons. The iPhone 5 has a 800:1 contrast ratio and 500cd/m2 max. brightness, again same as iPhone 4.




The old 30-pin connector has been replaced by a 8-pin connector called Lightning. A Lightning-to-30-pin adapter accessory is available in the Apple Store for $29. The adapter is compatible with all older accessories and Iphone 5, Ipod Touch 5th generation and Ipod Nano 7th generation.




Apple has also updated the headset with a new design. Earlier, you could mix left and right ear-plugs, but that is not possible any more as the new headphones has directional speakers. Just like Apple has done with the camera (iSight) and display (Retina) these headset has got a name. The new headphones are called EarPods and these will work with all previously released Apple products.

Apple has kicked out Google from the iPhone 5 software. Google Maps are now history for iPhone which is now replaced by Maps. Hence, the iPhone 5 will be the first Apple device running iOS 6, and Apple Maps are now the default navigation and map app on this and future iOS devices.

                                            Comparison of Google Maps v/s Apple Maps

Apple is very good at making their new products look revolutionary and sensational. One example of this evident during the presentation today. Instead of saying that the Iphone 5 is 1.7 mm thinner than the iPhone 4S, the company showed the audience that the new phone model was 18% thinner. A much larger number to show off.

The hardware specifications is not iPhone's strength any more. Looking at the specifications alone is not a good reason to go for this particular phone model. I think consumers will buy this for the well-known intuitive user interface and of course the Apple/iPhone brand name.


                              

The iPhone 5 will be available with three variants. Apple has not yet revealed the sim-free price of iPhone 5, but these are the prices in US on two year contract.
16 GB edition for $199
32 GB edition for $299
and the largest 64 GB edition for $399.
Too bad Apple didn't add a memory card slot on the new iPhone.

Like earlier the phone is launched in two colors : Black and White. iPhone 5 will be available in the US, Canada, Hong Kong, Singapore, France, Germany, Australia, UK on September 21st. 22 other European countries will get the phone on September 28. 100 countries will receive the iPhone 5 during December.

Apple again, has shown the world that it can make good software indulged in good hardware. There's no doubt that in one year from now, the iPhone 5 will be the best-selling mobile phones from Apple. I pesonally, do not think the iPhone 5 is the "All-new design" and "Completely redesigned" as Apple chose to call it. The iPhone 5 is an evolution smartphone based on the ideas behind Iphone 4/4S.


Some new features found in iPhone 5 -

  • New A6 processor
  • Three different network configurations (2 x GSM and one CDMA)
  • LTE network speed
  • Now with Wi-Fi a (802.11a/b/g/n)
  • Larger display (4-inch with a 640 x 1136 pixel resolution)
  • 1.2 front camera with 720p video capture
  • New smaller Lightning connector (8-pin)
  • Audio jack located at the bottom of the handset
  • New EarPods headphones
  • iOS 6
  • Lighter with 112 gram


Apple iPhone 5 highlights -

  • Weight: 112 gram
  • Size: 123.8 x 58.6 x 7.6 mm
  • Networks configurations - 
  1. GSM model A1428: UMTS/HSPA+/DC-HSDPA (850, 900, 1900, 2100 MHz); GSM/EDGE (850, 900, 1800, 1900 MHz); LTE (Bands 4 and 17)
  2. CDMA model A1429: CDMA EV-DO Rev. A and Rev. B (800, 1900, 2100 MHz); UMTS/HSPA+/DC-HSDPA (850, 900, 1900, 2100 MHz); GSM/EDGE (850, 900, 1800, 1900 MHz); LTE (Bands 1, 3, 5, 13, 25)
  3. GSM model A1429: UMTS/HSPA+/DC-HSDPA (850, 900, 1900, 2100 MHz); GSM/EDGE (850, 900, 1800, 1900 MHz); LTE (Bands 1, 3, 5)
  • 4 inch touch display, 640 x 1136 pixels resolution
  • Wi-Fi a/b/g/n (2.4 and 5GHz
  • 8 megapixel camera, BSI sensor
  • Bluetooth 4.0
  • A6 processor
  • AGPS and GLONASS
  • Talk time: 8 hours (3G)
  • Standby time: 225 hours
  • Sensors: gyro, accelerometer, proximity, ambient light


Continue reading →
Sunday 9 September 2012

CSS Tip: Resetting Your Styles with CSS Reset

0 comments



Resetting your styles, commonly referred to as CSS Reset or Reset CSS is the process of resetting (or more accurately – setting) the styles of all elements to a baseline value so that you avoid cross-browser differences due to their built-in default style settings. By resetting your styles, you avoid defaulting to the browser’s built-in styles, which differs from browser to browser.


CSS Reset avoids browser inconsistencies

For example, say you use an anchor tag <a> in your HTML document. Usually, browsers like Internet Explorer and Firefox will render it as blue and underlined.

But five years from now, someone decides to release a new browser (let’s call it UltraBrowser) and they decide that they don’t like blue, underlined links – so instead they set the default style of <a> tags to red and boldfaced. If you always set a baseline value for your <a> elements, then you’re guaranteed that it will always be the same, regardless of what UltraBrowser thinks <a> elements should look like.


A simple example

Example 1: How paragraph tags are rendered by default.

In Example 1, I’ve placed three unstyled paragraphs <p> inside a container <div> (I gave the container div a blue background and orange border to highlight the difference).
By default, in Firefox, you’ll see that there’s a space in between the top of the first paragraph and the top of the container div, and a space between the bottom of the last paragraph and the bottom of the container div.

By default, these spaces don’t exist in Internet Explorer.

So which browser is right, Firefox or IE? It doesn’t matter. What does matter is that the spacing in between your paragraphs and other elements will look dissimilar if you don’t set a style for a paragraph’s margin and padding.

Of course that’s a rather simplified example. In practice, CSS Reset is important in removing inconsistent margins, paddings, line-heights, and other attributes that can cause your web pages to look differently across various browsers.

So that’s the basic theory of resetting your styles which just means you set your style rules for all elements to avoid having the browser do it for you.

The below CSS Reset code has been taken from HTML5Doctor. It’s recommended to use a CSS reset for consistency.

/* 
html5doctor.com Reset Stylesheet
v1.6.1
Last Updated: 2010-09-17
Author: Richard Clark - http://richclarkdesign.com 
Twitter: @rich_clark
*/

html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var,
b, i,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
    margin:0;
    padding:0;
    border:0;
    outline:0;
    font-size:100%;
    vertical-align:baseline;
    background:transparent;
}

body {
    line-height:1;
}

article,aside,details,figcaption,figure,
footer,header,hgroup,menu,nav,section { 
 display:block;
}

nav ul {
    list-style:none;
}

blockquote, q {
    quotes:none;
}

blockquote:before, blockquote:after,
q:before, q:after {
    content:'';
    content:none;
}

a {
    margin:0;
    padding:0;
    font-size:100%;
    vertical-align:baseline;
    background:transparent;
}

/* change colours to suit your needs */
ins {
    background-color:#ff9;
    color:#000;
    text-decoration:none;
}

/* change colours to suit your needs */
mark {
    background-color:#ff9;
    color:#000; 
    font-style:italic;
    font-weight:bold;
}

del {
    text-decoration: line-through;
}

abbr[title], dfn[title] {
    border-bottom:1px dotted;
    cursor:help;
}

table {
    border-collapse:collapse;
    border-spacing:0;
}

/* change border colour to suit your needs */
hr {
    display:block;
    height:1px;
    border:0;   
    border-top:1px solid #cccccc;
    margin:1em 0;
    padding:0;
}

input, select {
    vertical-align:middle;
}



Continue reading →
Monday 3 September 2012

How to control your Computer using your Android Device

0 comments



There are times when you are sitting some distance away from your computer and you want to play a movie, or open a file, or browse the internet, or may be shutdown your computer. This all activities could be done using an application called Unified Remote.

Through this application you can do about every single activity that you can do with your Mouse or your Keyboard. Right from typing text in notepad, commenting on your friends Facebook status, Volume up-down of your player, opening a new tab, shutdown, restart your computer each and everything.

Its simply a 2-min job to do and you are ready to access your whole computer with your Android device.


Requirements -


1) Wi-Fi installed on your computer
2) Android Device


Steps -
You have to download two applications, one from your computer and the other from your Android device.

1) Download and install the Unified Remote server application on your computer. This will allow your device to connect and communicate to your PC on your home network. Requires Windows and .NET Framework 4.

Link:  http://www.unifiedremote.com/

2) After starting the server you will have to allow the software to bypass your firewall. This is again an important step as the Unified Remote server application on your computer must be allowed to bypass your firewall for it to connect to your Android device.





3) There appears a Unified Remote icon on your task bar. You can control the server (Start/Stop) from here.




4) When you double click the icon a window opens from where you can Start/Stop the Unified Remote application server when not in use.




5) Now your computer part is done. Time for installing the Unified Remote app on your Android device.
Search into Google Play from your Android device for "Unified Remote" and download the FREE version. Or go to the link,



Screenshots of Unified Remote app-

 




Here's a demo video:


Continue reading →