Monday, May 25, 2015

missing mcrypt – Fatal Error: Undefined function mcrypt_encrypt()! in Ubuntu 13.10/14.04

I came across this problem when I upgraded my Ubuntu to 14.04. I have used php-mycrypt in one of my project which was working fine until my ubuntu got upgraded and now when I try to access my project I get this Fatal Error
Fatal Error: Undefined function mcrypt_encrypt()
The solution looks simple but it took me ages to figure out and after a lot of googling around (most of the solution provided didn't help me at all ) so I thought I will share this hoping that you guys end up here quickly and don't have to spend much of your time (like I did ) :):) and hoping this will help you guys.

So, without wasting much of your time. Let me begin, First thing to do is see if you have installed php5-mcrypt (like everyone does), you can do that by:
sudo apt-get install php5-mcrypt
It should install mycrypt and if already installed, you will get the following message:
Reading package lists... Done
Building dependency tree       
Reading state information... Done
php5-mcrypt is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 13 not upgraded.
Looks good till now but wait a second, I still get that undefined fatal error. so what now,
see your ini file is in correct path. If not then move it (not neccessary if it is already there)
$mv /etc/php5/conf.d/mcrypt.ini /etc/php5/mods-available/
Enable mcrypt module. run the following command to enable it:
sudo php5enmod mcrypt
Restart Apache
sudo service apache2 restart
Try it out now And HOlA!!!!! IT works.

Hope it helped. Feel free to comment or you have any concerns or you know any other way :). Let us help each other and yes Happy coding.

jQuery Best Pratices

  1.  jQuery Files
    a) Try to use Jquery CDN where possible.                 
    <script type="text/javascript" src="//"></script>
    <script>window.jQuery || document.write('<script src="js/jquery-2.1.1.min.js" type="text/javascript"><\/script>')</script>
        Implement a fallback to locally hosted jQuery file of same version in-case CDN is not working.(as above) 
    b) DO NOT use jQuery version 2.x if you support Internet Explorer 6/7/8.
    c) If you are using other libraries like Prototype, MooTools, Zepto etc. that uses `$` sign as well, try not to use `$` for calling jQuery functions and instead use `jQuery` simply or use `$.noConflict()`.
  2.  jQuery Variable
    a) Better to start with  `$` while naming jQuery variable so it will be easy to understand and later on manipulate.
    var $someDiv = $("#someDiv"); // var $someDiv since it is a jQuery object
    var string = "some string"//not a jQuery object
    b) Always cache jQuery selectors object to a variable for reuse.
    var $someDiv = $("#someDiv");
    c) It is recommended to use Camel Case for naming Variable.
  3. Selectors
    a) Use ID selector whenever possible, since it uses javascript `document.getElementById()` selectors which is faster.
    b) Don't use the element type in your selector when using class selector. (for performance comparison click on :
    var $something = $("div.something"); // SLOW
    var $something = $(".something"); // FAST
    c) Use `.find()` selector. The `.find()` approach is faster because the first selection is handled without going through the Sizzle selector engine.          
    // Fast:
      $( "#container div.robotarm" );
    // Super-fast:
      $( "#container" ).find( "div.robotarm" );
    // BAD, goes through Sizzle selector engine
      var $productIds = $("#products");
    // GOOD, #products is already selected by document.getElementById() so only needs to go through Sizzle selector engine
      var $productIds = $("#products").find("");
    d) Be specific on the right-hand side of your selector, and less specific on the left.  
    // Unoptimized
       $(" .gonzalez");
    // Optimized
       $(".data td.gonzalez");
    e) Avoid Excessive Specificity. (for performance comparison click on :
    $(".data table.attendees td.gonzalez");
    // Better: Drop the middle if possible.
    $(".data td.gonzalez");
    f) Give your Selectors a Context.
    // SLOWER because it has to traverse the whole DOM for .class
    // FASTER because now it only looks under class-container.
    $('.class''#class-container'); //this uses `.find()` selector internally.
    g) Avoid Universal Selectors. More Info 
    $( ".buttons > *" ); // Extremely expensive.
    $( ".buttons" ).children(); // Much better.
    h) Avoid Implied Universal Selectors. When you leave off the selector, the universal selector (*) is still implied.
    $('div.someclass :radio'); // BAD
    $('div.someclass input:radio'); // GOOD
    i) Don’t Descend Multiple IDs or nest when selecting an ID. ID-only selections are handled using document.getElementById() so don't mix them with other selectors.
    $('#outer #inner'); // BAD
    $('div#inner'); // BAD
    $('.outer-container #inner'); // BAD
    $('#inner'); // GOOD, only calls document.getElementById() 
  4. DOM Manipulation
    a) Always detach any existing element before manipulation and attach it back after manipulating it. This is reduce the Reflow and Repaint of browser and increase performance drastically or use `documentfragment`.
    var $myList = $("#list-container > ul").detach();
    //...a lot of complicated things on $myList
    b) Use string concatenation or `array.join()` over `.append()`. (for performance comparison click on :
    // BAD
    var $myList = $("#list");
    for(var i = 0; i < 10000; i++){
    // GOOD
    var $myList = $("#list");
    var list = "";
    for(var i = 0; i < 10000; i++){
        list += "<li>"+i+"</li>";
    var array = []; 
    for(var i = 0; i < 10000; i++){
        array[i] = "<li>"+i+"</li>"
    c) Make sure you check for the element in DOM before manipulating it if you are not sure about that element.
    // BAD: This runs three functions before it realizes there's nothing in the selection
    // GOOD
    var $mySelection = $("#nosuchthing");
    if ($mySelection.length) {

  5. Events
    a) Use only one `document.ready` handler per page. It makes it easier to debug and keep track of the behavior flow.
    $(function(){....}) //short hand for document.ready function
    b) DO NOT use anonymous functions to attach events. Anonymous functions are difficult to debug, maintain, test, or reuse.
    // BAD
    $("#myLink").on("click", function(){...});
    // GOOD
    function myLinkClickHandler(){...}
    $("#myLink").on("click", myLinkClickHandler);

    c) Avoid Inline Javascript in HTML markup, these are debugging nightmares. Always bind events with jQuery to be consistent so it's easier to attach and remove events dynamically.
    <a id="myLink" href="#" onclick="myEventHandler();">my link</a>
    $("#myLink").on("click", myEventHandler);
    d) When possible, use custom namespace for events. It's easier to unbind the exact event that you attached without affecting other events bound to the DOM element.
    $("#myLink").on("click.mySpecialClick", myEventHandler); // GOOD
    // Later on, it's easier to unbind just your click event
    e) Use event delegation (for dynamically added element) when you have to attach same event to multiple elements. Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.
    $("#list a").on("click", myClickHandler); // BAD, you are attaching an event to all the links under the list.
    $("#list").on("click""a", myClickHandler); // GOOD, only one event handler is attached to the parent.
  6. Ajax
    a) DO NOT use http requests on https sites. Prefer schemaless URLs (leave the protocol http/https out of your URL)
    b) DO NOT put request parameters in the URL, send them using data object setting.
    // Less readable...
       url: "something.php?param1=test1&param2=test2",
    // More readable...
       url: "something.php",
       data: { param1: test1, param2: test2 }
    c) Try to specify the dataType setting so it's easier to know what kind of data you are working with.
    d) Use Delegated event handlers for attaching events to content loaded using Ajax. Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time (example Ajax).
    $("#parent-container").on("click""a", delegatedClickHandlerForAjax);
    e) Use Promise interface: 
    $.ajax({ ... }).then(successHandler, failureHandler);
    // OR
    var jqxhr = $.ajax({ ... });

    Sample Ajax Template:
    var jqxhr = $.ajax({
                   url: url,
                   type: "GET"// default is GET but you can use other verbs based on your needs.
                   cache: true// default is true, but false for dataType 'script' and 'jsonp', so set it on need basis.
                   data: {}, // add your request parameters in the data object.
                   dataType: "json"// specify the dataType for future reference
                   jsonp: "callback"// only specify this to match the name of callback parameter your API is expecting for JSONP requests.
                   statusCode: { // if you want to handle specific error codes, use the status code mapping settings.
                      404: handler404, // handler404() function
                      500: handler500  // handler500() function
    jqxhr.done(successHandler); //successHandler function; // failHandler function
  7. Effects and Animations
    a) Adopt a restrained and consistent approach to implementing animation functionality.
    b) DO NOT over-do the animation effects until driven by the UX requirements.
    c) Try to use simple `show/hide`, `toggle and slideUp/slideDown` functionality to toggle elements.
    d) Try to use predefined animations durations of "slow", "fast" or 400 (for medium).
  8. Plugins
    a) Always choose a plugin with good support, documentation, testing and community support.
    b) Check the compatibility of plugin with the version of jQuery that you are using.
    c) Any common reusable component should be implemented as a jQuery plugin. link to  jQuery Plugin Boilerplate code.
  9. Chaining
    a) Use chaining as an alternative to variable caching and multiple selector calls.        
    b) Whenever the chain grows over 3 links or gets complicated because of event assignment, use appropriate line breaks and indentation to make the code readable.
           .on("click", myClickHandler)
           .on("mouseover", myMouseOverHandler)
    For long chains it is acceptable to cache intermediate objects in a variable. 
  10. Miscellaneous
    a) Use Object literals for parameters.
    $myLink.attr("href""#").attr("title""my link").attr("rel""external"); // BAD, 3 calls to attr()
    // GOOD, only 1 call to attr()
         href: "#",
         title: "my link",
         rel: "external"
    b) Do not mix CSS with jQuery.
    $("#mydiv").css({'color':red, 'font-weight':'bold'}); 
    .error { color: red; font-weight: bold; }
    c) DO NOT use Deprecated Methods. It is always important to keep an eye on deprecated methods for each new version and try avoid using them. Checkout for a list of deprecated methods. Some of which are:
    .live() (Deprecated > Deprecated 1.7 | Removed),
    .load() (Deprecated > Deprecated 1.8 | Events > Document Loading),
    .selector Deprecated > Deprecated 1.7 | Removed
    d) Combine jQuery with native JavaScript when needed. (performance comparision :
    $("#myId"); // is still little slower than...
  11. Resources
    jQuery Performance:
    jQuery Learn:
    jQuery API Docs:
    jQuery Coding Standards and Best Practice:
    jQuery Cheatsheet:
    jQuery Plugin Boilerplate: question: