Is Cross-Origin Resource Sharing (CORS) AJAX Requests interfering with your development ?

This is again another learning from one of my latest projects. I have been creating a Backbone based web application. The data was being fetched from the webservice exposed on the developers machine. The issue occurred when backbone view tried making AJAX call to the webservice; it was being rejected by Chrome browser marking it as a CORS call.

We wanted an immediate intermediary solution to continue with our development and we found that using a command line parameter  while starting chrome browser makes it ignore CORS check during AJAX call.

I thought it would come handy to someone facing same issue and needing immediate solution :-)

Please use the below steps to start chrome browser with CORS security disabled;

1. press windows button + R key to start windows Run prompt
2. type follwoing command i.e.

chrome.exe --disable-web-security

EDIT:
Some people could not get CORS working using above method.

For them, here is a little PHP function that can also help you get rid of CORS issue.
All you need to do is call this function in the start of the PHP page.


/**
 *  An example CORS-compliant method.  It will allow any GET, POST, or OPTIONS requests from any
 *  origin.
 *
 *  In a production environment, you probably want to be more restrictive, but this gives you
 *  the general idea of what is involved.  For the nitty-gritty low-down, read:
 *
 *  - https://developer.mozilla.org/en/HTTP_access_control
 *  - http://www.w3.org/TR/cors/
 *
 */
function cors() {
    // Allow from any origin
    if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }
    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");        
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
        exit(0);
    }
    echo "You have CORS!";
}

Thanks for reading and happy coding :-)

Make a bootable windows 7 / Windows 8 usb drive

Recently, I landed into my worst fear i.e. my work computer crashed and I had to setup a new machine from scratch. In the entire recovery process; installing the OS the most tedious work.
It was then when I realized the lack of proper documentation regarding how to create a bootable USB drive to install windows 7 or windows 8

With lot of pain I found a tutorial and I would like to share the steps with all of you;


  1. Open the windows command prompt in the Administrator modeType following commands in sequence i.e.
  • diskpart (Note: This will open a new command window. Please do not close the previous command window as we will need that later)
  • list disk
  • select disk 1 (Note: here disk 1 is the disk number of the usb drive)
  • clean
  • create partition primary
  • select partition 1
  • active
  • format fs=ntfs quick
  • assign
  • exit (Note: This will close the command window and you will land into the original / first command window)
Now, you need the copy of windows 7 or windows 8 that you want to move to usb drive
Execute the following commands in the command window that is open i.e.
  • cd d:\windows7_install\boot (Note:  Navigate to directory named boot under the windows installation directory. For me it was in the D: drive in windows7_install directory)
  • bootsect.exe/nt60 e: (Note:  Here e: is the drive letter of the USB drive. Make sure that you make no mistake in this step)
  • Your bootable USB drive is all set now. Copy the windows 7 or windows 8 install directory in the USB drive and restart the computer with boot from USB disk option selected in the BIOS.
  • You computer boots from the USB drive. Now you can execute the setup from the install directory that you copied in previous step

Cheers !! :-)



Source: http://www.youtube.com/watch?v=nZVgInbhTAw&index=93&list=WL


PHP Mail - Send HTML email with attachment

As the title says, we are going to discuss how to send mail with attachment using the default PHP mail() function.
Sending a plain text / HTML email using PHP mail is very simple but when it comes to adding attachments to it then it becomes equally tricky and difficult.
I was caught up in this tricky situation and after searching around and trying zillions of solutions suggested by users in various forums got the following solution to work;
Hope this comes handy to someone in need :-)

This is the code that worked for me;

/* Email Detials */
  $mail_to = $to;
  $from_mail = $from;
  $from_name = $from_name;
  $reply_to = $from;
  $subject = $subject;
  $message_body = $msg;

/* Attachment File */
  // Attachment location
  $file_name = $basefilename;
  $path = "../reports/";
 
    // Read the file content
    $file = $path.$file_name;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
   
/* Set the email header */
    // Generate a boundary
    $boundary = md5(uniqid(time()));
   
    // Email header
    $header = "From: ".$from_name." \r\n";
    $header .= "Reply-To: ".$reply_to."\r\n";
    $header .= "cc: ".$cc."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
   
    // Multipart wraps the Email Content and Attachment
    $header .= "Content-Type: multipart/mixed;\r\n";
    $header .= " boundary=\"".$boundary."\"";

    $message .= "This is a multi-part message in MIME format.\r\n\r\n";
    $message .= "--".$boundary."\r\n";
   
    // Email content
    // Content-type can be text/plain or text/html
    $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
    $message .= "Content-Transfer-Encoding: 7bit\r\n";
    $message .= "\r\n";
    $message .= "$message_body\r\n";
    $message .= "--".$boundary."\r\n";
   
    // Attachment
    // Edit content type for different file extensions
    $message .= "Content-Type: application/csv;\r\n";
    $message .= " name=\"".$file_name."\"\r\n";
    $message .= "Content-Transfer-Encoding: base64\r\n";
    $message .= "Content-Disposition: attachment;\r\n";
    $message .= " filename=\"".$file_name."\"\r\n";
    $message .= "\r\n".$content."\r\n";
    $message .= "--".$boundary."--\r\n";
   
    // Send email
    if (mail($mail_to, $subject, $message, $header)) {
        echo "Sent";
    } else {
        echo "Error";
    }


Credit goes to user Karmaprod at the forum eureka.ykyuen.info
link: http://eureka.ykyuen.info/2010/02/16/php-send-attachmemt-with-php-mail/