Follow Me:

Wednesday 1 April 2015

Cookies are stored in browser as a text file format.It is stored limit amount of data.It is only allowing 4kb[4096bytes].It is not holding the multiple variable in cookies.

we can accessing the cookies values in easily.So it is less secure.The setcookie() function must appear BEFORE the <html> tag.

Sessions are stored in server side.It is stored unlimit amount of data.It is holding the multiple variable in sessions. we cannot accessing the cookies values in easily.So it is more secure.

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_uvalue3600, "/");


  //expire cookies
setcookie($cookie_uname,"",-3600);

?>
   


Example:

<?php
    
session_start();

//session variable
$_SESSION['testvaraible'] = 'Codings';
 
//destroyed the entire sessions
session_destroy(); //Destroyed the session 
variable "testvaraible".
unset($_SESSION['testvaraible']);
?>
   

Wednesday 25 February 2015

Expire the session after some peiod of time by using in php.

Basically, two methods are available to destory the sessions.

   1. session_destroy()
   2. unset($_SESSION['testvaraible'])
   3. setting the time out for this session




 1.session_destroy()

     if we will call the session_destroy(), it will destroy all the SESSION variable.It is no need any other parameters.

<?php
 session_destroy();
?>

2. unset($_SESSION['testvaraible'])

       When we will call the unset() ,it will destroyed the particular variable.
    Example:
   
     
<?php 
 // Destroyed the session variable "testvaraible".
 unset($_SESSION['testvaraible']);

?>

3. setting the time out for this session

<?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 }

4.session.gc_maxlifetime()

   By using php ini_set session.gc_maxlifetime also we will expire the session.

<?php
 ini_set session.gc_maxlifetime
?>

 
   

Saturday 10 January 2015

mysql> desc codingslover;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| ProjId   | varchar(20) | NO   | PRI | NULL    |       |
| BranchId | int(11)     | YES  |     | NULL    |       |
| AddedOn  | datetime    | YES  |     | NULL    |       |
| Status   | int(11)     | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> desc codingslover ProjId;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| ProjId  | varchar(20) | NO   | PRI | NULL    |       |
+---------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)


How to resolved when Logged out sessions get restored by back button?

When we log out the sessions , We clear the cookies in browser. But when we press the back button after logging out the session gets restored. 
Solution:

 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past