Monthly Archives: July 2010

How to test HTTPS CURL in development server?

HTTPS is secure HTTP communication based on SSL protocol (HTTP over SSL). Generally all sensitive info (like passwords, financial details, etc.) are sent over this transport. Common example: your gmail login is done through HTTPS channel and different payment gateway.
So here in this deal –

$postfields = array('field1'=>'value1', 'field2'=>'value2');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://foo.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);

CURLOPT_SSL_VERIFYHOST is off. This allows you to test the CURL in your dev server without having HTTPS. PHP/Curl will handle the http request.

Share

What is CSS Image Sprites?

An image sprite is a collection of images put into a single image.

What is advantage of using image sprite?

  1. Reduce multiple server requests.
  2. Sprites reduce the number of server requests and save bandwidth.
  3. Another advantage of sprites is that you can keep all your images in one location and in some cases it makes more sense (for menus and so on).

A real life Example
If you use sprites for a “mouse over” display, the user won’t experience image disappear for a second… and it looks really good when you have heavy graphics in your site.
If you change the image instead of just moving the sprite around it will load a new image and the loading time can be visible to the end user.

CSS Image Sprites Example Code

.NotGood{

  background:url(sprites.jpg);

}

.NotGood:hover{

  background:url(spritesHover.jpg);

}

.Good{

  background:url(sprites.jpg) 0px 0px;

}

.Good:hover{

  background-position:15px 0px;

}

You can use Adobe Photoshop or any other image editing software to determine which area needs to be display.

Share

JavaScript Refrence

JavaScript Variable Manipulation Functions

As shown in the following table, you can use these JavaScript statements in your own code to create and modify variables in your JavaScript functions.

Element Description
var myVar = 0; Creates a variable with given starting value. Type is determined dynamically.
stringVar = prompt(“message”) Sends message to user in a dialog box, retrieves text input from user and stores it in stringVar.
stringVar.length Returns the length (in characters) of stringVar.
stringVar.toUpperCase(), stringVar.toLowerCase() Converts stringVar to upper- or lowercase.
stringVar.substring() Returns a specified subset of stringVar.
stringVar.indexOf() Returns location of a substring in stringVar (or -1).
parseInt() Converts string to int.
parseFloat() Converts string to float.
toString() Converts any variable to string.
eval() Evaluates string as JavaScript code.
Math.ceil() Converts any number to integer by rounding up.
Math.floor() Converts any number to integer by rounding down.
Math.round() Converts any number to integer by standard rounding algorithm.
Math.random() Returns random float between 0 and 1.

Basic I/O Commands in JavaScript

JavaScript programmers commonly use the commands shown in the following table for controlling dialog-based input and output in programs to be used on the Web.

Element Description
alert(“message”); Creates a popup dialog containing “message.”
stringVar = prompt(“message”) Send message to user in a dialog box, retrieve text input from user and store it in stringVar.

JavaScript Conditions and Branching Code Structures

Look to the following table for JavaScript control structures you can use in your program code to add branching and looping behavior to your JavaScript programs.

Element Description
if (condition){

// content

} else {

// more content

} // end if

Executes content only if condition is true.

Optional else clause occurs if condition
is false.

switch (expression)

case: value;

//code

break;

default:

//code
}

Compares expression against one or more values. If expression
is equal to value, runs corresponding code.

Default clause catches any uncaught values.

for(i = 0; i < count; i++)

//code

} // end for

Repeats code i times.
While (condition){

//code

} // end while

Repeats code as long as condition is true.
Function fnName(paramaters) {

//code

} // end function

Defines a function named fnName and
sends it parameters. All code inside the function will execute when
the function is called.

Add JavaScript Comparison Operators to Condition Statements

JavaScript uses comparison operators inside conditions to make numeric or alphabetical comparisons of variables to other variables or values. Using these operators, you can determine whether a variable is greater than, less than, or equal to another variable or value. You can also use combinations of these comparison operators.

Name Operator Example Notes
Equality == (x==3) Works with all variable types, including strings.
Not equal != (x != 3) True if values are not equal.
Less than < (x < 3) Numeric or alphabetical comparison.
Greater than > (x > 3) Numeric or alphabetical comparison.
Less than or equal to <= (x <= 3) Numeric or alphabetical comparison.
Greater than or equal to >= (x >= 3) Numeric or alphabetical comparison.

Create JavaScript Structures and Objects

JavaScript allows you to put together code lines to create functions and variables to create arrays. You can put functions and variables together to create objects.

Element Description
function fnName(parameters) {

//code

} // end function

Defines a function named fnName and
sends it parameters. All code inside function will execute when the
function is called.
var myArray = new Array(“a”,
“b”, “c”);
Creates an array. Elements can be any type (even mixed
types).
Var myJSON = {

“name”:
“Andy”,

“title”:
“Author”

}

Creates a JSON object. Each element
has a name/value pair, and can contain anything, including an array
(with square braces) another JSON object,
or a function.
Var person = new Object();

Person.name =
“Andy”;

Creates an object. You can add ordinary variables (which become
properties) or functions (which become methods).

Change Your Web Page with JavaScript Document Object Model Methods

The Document Object Model methods shown in the following table offer you a great way to access and modify your Web pages through your JavaScript code.

Element Description
myElement =
document.getElementById(“name”);
Gets an element from the page with the specified ID and copies
a reference to that element to the variable myElement.
myElement.innerHTML =
“value”
Changes the value of the element to “value”.
document.onkeydown = keyListener When a key is pressed, a function called keyListener is automatically activated.
document.onmousemove =
mouseListener
When the mouse is moved, a function called mouseListener is automatically activated.
setInterval(function, ms); Runs function each ms
milliseconds.
myArray =
document.getElementByName(“name”)
Returns an array of objects with the current name (frequently
used with radio buttons).
Share

What is ajax synchronous and asynchronous?

Synchronous – Script stops and waits for the server to send back a reply before continuing. There are some situations where Synchronous Ajax is mandatory.

In standard Web applications, the interaction between the customer and the server is synchronous. This means that one has to happen after the other. If a customer clicks a link, the request is sent to the server, which then sends the results back.

Because of the danger of a request getting lost and hanging the browser, synchronous javascript isn’t recommended for anything outside of (onbefore)unload event handlers, but if you need to hear back from the server before you can allow the user to navigate away from the page, synchronous Javascript isn’t just your best option.

Synchronous AJAX function Example using GET.

    function getFile(url) {
  if (window.XMLHttpRequest) {
    AJAX=new XMLHttpRequest();
  } else {
    AJAX=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (AJAX) {
     AJAX.open("GET", url, false);
     AJAX.send(null);
     return AJAX.responseText;
  } else {
     return false;
  }
}

var fileFromServer = getFile('http://www.phpmind.com/om.txt');

Synchronous AJAX function Example using POST.

function getFile(url, passData) {
  if (window.XMLHttpRequest) {
    AJAX=new XMLHttpRequest();
  } else {
    AJAX=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (AJAX) {
    AJAX.open("POST", url, false);
    AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    AJAX.send(passData);
    return AJAX.responseText;
  } else {
     return false;
  }
}

var fileFromServer = getFile('http://www.phpmind.com/data.php', sendThisDataAsAPost);

Asynchronous – Where the script allows the page to continue to be processed and will handle the reply if and when it arrives. If anything goes wrong in the request and/or transfer of the file, your program still has the ability to recognize the problem and recover from it.
Processing asynchronously avoids the delay while the retrieval from the server is taking place because your visitor can continue to interact with the web page and the requested information will be processed with the response updating the page as and when it arrives.

Share

What is Ajax?

Ajax (sometimes called Asynchronous JavaScript and XML) is a way of programming for the Web that gets rid of the hourglass. Data, content, and design are merged together into a seamless whole. When your customer clicks on something on an Ajax driven application, there is very little lag time. The page simply displays what they’re asking for.

Ajax is a way of developing Web applications that combines:

  • XHTML and CSS standards based presentation
  • Interaction with the page through the DOM
  • Data interchange with XML and XSLT
  • Asynchronous data retrieval with XMLHttpRequest
  • JavaScript to tie it all together
Share

JSON: The 5 minute lesson

What is Json ?

JSON stand for  JavaScript Object Notation.
It is a lightweight text-based open standard designed for human-readable data interchange.
It is derived from the JavaScript  programming language for representing simple data structures and associative arrays, called objects.
Despite its relationship to JavaScript, it is language-independent, with parsers available for virtually every programming language. The JSON filename extension is .json.

It is easy for humans to read and write. It is easy for machines to parse and generate.

Exmaple:

{"skillz": {
"web":[
{"name": "html",
"years": "5"
},
{"name": "css",
"years": "3"
}],
"database":[
{"name": "sql",
"years": "7"
}]
}}

Squiggles, Squares, Colons and Commas

1. Squiggly brackets act as ‘containers’ { }
2. Square brackets holds arrays [ ]
3. Names and values are separated by a colon. :
4. Array elements are separated by commas. ,

Share