Session

A PHP session variable is used to store information , or change settings for a user session. Session variables hold information about an user, and are available to all pages in one application.

To store user information in a session variable , you need to first start session .

How session works ?

1. The session_start( ) function generates a random Session Id and stores it in a cookie on the user's computer (this is the only session information that is actually stored on the client side.) or is propagated in the URL.
2. The default name for the cookie is PHPSESSID, although this can be changed in the PHP configuration files on the server .
3. After a session is established , you can now create , store and retrieve information pertaining to that session .
4. Information to be stored for a session is kept in session variables (in the above example session variable views storing value 1 ).

1) session_start :

Initialize session data

example 1:
  	
		<?php
			session_start(); // initiates session
			$_SESSION['views'] = 1 ; // stores session variable
			echo $_SESSION['views'] ; // output : 1
			print_r($_SESSION) ; // print all the session variables
		?>
	
example 2:
  	
			<?php
				define("APP_ROOT",'http://trainee.afixiindia.com/');
				session_start(); 
				echo 'Welcome to page #1';
				$_SESSION['name'] = 'Rasmi';
				$_SESSION['dept']   = 'mca';
				$_SESSION['college']    = 'Ravenshaw';
				echo 'page 2';  
				session_start(); 
				echo 'Welcome to page #2'; 
				print_r($_SESSION['college']);
			?>
		

2) session_destroy:
a. Destroys all data registered to a session
b. session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie.
example:
  	
			<?php
				define("APP_ROOT",'http://trainee.afixiindia.com/');
				session_start(); 
				echo 'Welcome to page #1';
				$_SESSION['name'] = 'Rasmi';
				$_SESSION['dept']   = 'mca';
				$_SESSION['college']    = 'Ravenshaw';
				echo 'page 2';  
				session_start(); 
				echo 'Welcome to page #2'; 
				print_r($_SESSION['college']);
				Session_destroy(); 
				print_r($_SESSION['college']);
			?>
		

3) session_unset:

The session_unset() function frees all session variables currently registered.

example:
  	
			<?php
				unset($_SESSION['email']);
			?>
		

4) session_id():

session_id() returns the session id for the current session


5) session_name():

* session_name() get and/or set the current session name
* session_name() returns the name of the current session.
* If name is given, session_name() will update the session name and return the old session name.


example:
  test.php	
<?php
	echo "<pre>This is test.php. Run test2.php in another tab(Note: not in other browser)";
    define("APP_ROOT",'http://trainee.afixiindia.com/');
    session_start();
    echo 'Session Starts<br/>';
    $_SESSION['name'] = 'Rasmi Ranjan';
    $_SESSION['college'] = 'Ravenshaw';
	echo 'Value assigned<pre>';
    print_r($_SESSION['college']);
//	session_unset();
	session_destroy();
//	$_SESSION = NULL;
	var_dump($_SESSION);
    print_r($_SESSION['college']);
?>
		
  test2.php	
<?php
	echo "<pre>";
    session_start();
	var_dump($_SESSION);
    print_r($_SESSION['college']);
?>
		

COOKIES

A cookie is used to keep information in the user's browser .A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

The main difference between cookie and session is that cookies are stored on the client, while session data is stored on the server .

Eg . If a person has a login and password, this can be set as a cookie in their browser so they do not have to re-login to your website every time they visit.

1.setcookie:

Set cookie variable.

example:
  	
			<?php
				$value = 'something from somewhere';
				setcookie("TestCookie", $value);
				setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
				setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
				echo $_COOKIE['TestCookie'];  // Output : something from somewhere
				print_r($_COOKIE) ; // print all the cookies
			?>
		

Questions

  1. What is the difference between session and cookie ?
  2. Difference between session_unset() and session_destroy() ?
  3. Which is more secure , session or cookie ?
  4. In which case we have to use session and cookies. Give examples?
  5. How can free simple variable, session variable, and cookies ?
  6. Create a session and cookies and give them user defined name, password. And get these variables and then destroy them.?