Follow Me:

Wednesday, 13 May 2015

What is JSON?

JavaScript Object Notation (JSON) is a lightweight data-interchange format inspired by the object literals of JavaScript. 

JSON values can consist of:

  •   objects (collections of name-value pairs)
  •   arrays (ordered lists of values)
  •   strings (in double quotes)
  •   numbers
  •   true, false, or null

JSON is language independent.

JSON with PHP?

   After PHP Version 5.2.0, JSON extension is decodes and encodes functionalities as default.

  • Json_encode     - returns the JSON representation of values
  • Json_decode     - Decodes the JSON String
  • Json_last_error - Returns the last error occured.

JSON Syntax and Rules?

   JSON syntax is derived from JavaScript object notation syntax:

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays

JSON Example

    This example is simple phone number validation and once phone number has validated , it will be sent this php page, there we are using the json format related encode data.

Html Page:
<title>JSON Example in Php</title>
<script src="js/jquery-1.11.0.min.js"></script>
<h1>Json Basic Example</h1>
<form name="phone-form" method="post" onsubmit="return validation();">
 <input type="text" name="phone" id="phone" value="" >
 <input type="submit" value="submit" >
<div id="outputDisplay"></div>

Javascript Function

function validation(value){ 
 //Phone Number Validations
 var phone   = $('#phone').val();
 var specialDigitPattern  = /[~`!@#\$%\^&\*()\-_\+=|\\{}\[\]:;"'<>,\?\/\.0123456789]/;
 //var mailvalidateregex  = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;  email validation
 if(phone == ""){
  alert("Please enter the phone ");
  return false;
 }else if(isNaN(phone)){
  alert('Please Enter Valid Phone Number');
  return false;
 var postdata  = 'phone='+phone;
 var url   = 'testphone.php?phone='+phone;
  type  : "POST",
  url   : url, 
  data  : postdata,
  dataType : "json",
  success  : phonestatus_check,
 return false;

function phonestatus_check(response){
 if(response.status == 1){
  return false;

Php Page:

  $phonenumber = $_POST['phone'];
  $responsearray = array();$responsestatus = '1';
  if($responsestatus == 1){
      $responsearray = array('status' => 1,'message'=> 'Thanks for submitting the Phone Number');
   $responsearray = array('status' => 0,'message'=> 'failure');
  echo json_encode($responsearray);
  /* $json = json_encode($responsearray);
   exit("{$_GET['jsoncallback']}($json)"); */

Friday, 1 May 2015

Nginx: 413 Request Entity Too Large Error and Solution

  My application is running nginx as a frond end to php based Apache server. My applicatio lets user upload images upto 2MB in size. When users trying to upload 1.5MB+ size image file using nginx reverse proxy, they are getting the following error on screen:

    Nginx 413 Request Entity Too Large

You need to configure both nginx and php to allow upload size.

Nginx configuration:

The client_max_body_size directive assigns the maximum accepted body size of client 
 request, indicated by the line Content-Length in the header of request.

If size is greater the given one, then the client gets the error "Request Entity Too Large" 
(413). To fix this issue edit your nginx.conf.
Open the Terminal or login to the remote server using ssh client. 
Type the following command to edit your nginx.conf using a text editor such as vi or joe:

# vi /etc/nginx/nginx.conf
# vi /usr/local/nginx/conf/nginx.conf
Add the following line to http or server or location context to increase 
 the size limit 
 in nginx.conf, enter:

# set client body size to 2M #
client_max_body_size 2M;

Save and close the file. Reload the nginx webserver, enter:
# /usr/local/nginx/sbin/nginx -s reload
# /sbin/nginx -s reload
OR use the following on RHEL/CentOS/Debian/Ubuntu Linux:
# service nginx reload 

PHP configuration (optional):

Your php installation also put limits on upload file size. Edit php.ini and set the following directives ;This sets the maximum amount of memory in bytes that a script is allowed
to allocate memory_limit = 32M ;The maximum size of an uploaded file. upload_max_filesize = 2M ;Sets max size of post data allowed.This setting also affects file upload.
To upload large files, this value must be larger than upload_max_filesize post_max_size = 3M Save and close the file. Make sure you reload/restart back-end apache or
nginx web server as per your setup