PHP sessions default timeout
We are settind the Session Time out in Php as mentioned as below:
session.gc_maxlifetime = 1440
(1440 seconds = 24 minutes)
<html> <head> <title>JSON Example in Php</title> <script src="js/jquery-1.11.0.min.js"></script> </head> <body> <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" > </form> <div id="outputDisplay"></div>
<script> 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; $.ajax({ type : "POST", url : url, data : postdata, dataType : "json", success : phonestatus_check, }); return false; } function phonestatus_check(response){ if(response.status == 1){ alert('success'); $("#outputDisplay").css("color","green"); $("#outputDisplay").html(response.message); }else{ alert('failure'); return false; } } //ends </script>
Php Page:
<?php if(isset($_POST['phone'])){ $phonenumber = $_POST['phone']; $responsearray = array();$responsestatus = '1'; if($responsestatus == 1){ $responsearray = array('status' => 1,'message'=> 'Thanks for submitting the Phone Number'); }else{ $responsearray = array('status' => 0,'message'=> 'failure'); } echo json_encode($responsearray); /* $json = json_encode($responsearray); if($_GET['jsoncallback']) exit("{$_GET['jsoncallback']}($json)"); */ } ?>
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
OR
# 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 OR # /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
Cookies |
Sessions |
---|---|
Cookies are stored in browser as text file format. |
Sessions are stored in server side. |
It is stored limit amount of data. It is only allowing 4kb[4096bytes] |
It is stored unlimit amount of data. |
It is not holding the multiple variable in cookies. |
It is holding the multiple variable in sessions. |
we can accessing the cookies values in easily. So it is less secure. The setcookie() function must appear BEFORE the <html> tag |
we cannot accessing the cookies values in easily. So it is more secure. |
Destroy Cookies: 1. if we Closing the browsers at the time cookies values destoryed. 2. setting the cookie time to expire the cookie. |
Destroy Sessions : 1. using unset() session,we will destroyed the sessions. 2. using session_destory(), we we will destroyed the sessions. |
Example: <?php
setcookie(name, value, expire,
path,domain, secure, httponly);
$cookie_uame = "codingslover"; $cookie_uvalue= "website"; //set cookies for 1 hour time setcookie($cookie_uname, $cookie_uvalue, 3600, "/");
//expire cookies setcookie($cookie_uname,"",-3600); ?>
|
Example: <?php session_start(); //session variable $_SESSION['testvaraible'] = 'Codings';
variable "testvaraible". unset($_SESSION['testvaraible']); ?> |
<?php session_destroy(); ?>
<?php // Destroyed the session variable "testvaraible". unset($_SESSION['testvaraible']); ?>
<?php if isset($_SESSION['LAST_MINITUte_ACTIVITY']) &&
(time() - $_SESSION['LAST_MINITUte_ACTIVITY'] > 1800)) { // last request was more than 30 minutes ago session_unset(); // unset $_SESSION variable for the run-time session_destroy(); // destroy session data in storage } // update last activity time stamp $_SESSION['LAST_MINITUte_ACTIVITY'] = time(); /* You can also use an additional time stamp to regenerate the session ID periodically,to avoid attacks on sessions like session fixation: */ if (!isset($_SESSION['CREATED'])) { $_SESSION['CREATED'] = time(); } else if (time() - $_SESSION['CREATED'] > 1800) { // session started more than 30 minutes ago // change session ID for the current session // an invalidate old session ID session_regenerate_id(true); $_SESSION['CREATED'] = time(); // update creation time }
<?php ini_set session.gc_maxlifetime ?>
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
get '/json' do
content_type :json
content = { :response => 'Sent via JSON',
:timestamp => Time.now,
:random => rand(10000) }
content.to_json
end
var url = host_prefix + '/json';
$.getJSON(url, function(json){
$("#json-response").html(JSON.stringify(json, null, 2));
});
{
"response": "Sent via JSON",
"timestamp": "2014-06-18 09:49:01 +0000",
"random": 6074
}
get '/jsonp' do
callback = params['callback']
content_type :js
content = { :response => 'Sent via JSONP',
:timestamp => Time.now,
:random => rand(10000) }
"#{callback}(#{content.to_json})"
end
var url = host_prefix + '/jsonp?callback=?';
$.getJSON(url, function(jsonp){
$("#jsonp-response").html(JSON.stringify(jsonp, null, 2));
});
{
"response": "Sent via JSONP",
"timestamp": "2014-06-18 09:50:15 +0000",
"random": 364
}