{"id":247,"date":"2018-07-13T06:31:32","date_gmt":"2018-07-13T06:31:32","guid":{"rendered":"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=247"},"modified":"2018-12-07T11:32:15","modified_gmt":"2018-12-07T11:32:15","slug":"cookie-and-session","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/chapter\/cookie-and-session\/","title":{"rendered":"COOKIE and SESSION"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/kkOGRQOZZUA\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Objectives<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0 <strong>Cookie<\/strong>\r\n\r\n\u2022\u00a0\u00a0<strong>Session<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>COOKIE<\/strong>\r\n<ul>\r\n \t<li style=\"text-align: justify\">In any web application, it is essential to keep track of users as he\/she moves in a web pages of web application. To keep track of user\u2019s movement within a web application session and cookies are used.<\/li>\r\n \t<li style=\"text-align: justify\">Cookie provides a way for web application to store information in the user\u2019s web browser and can retrieve the information of cookie every time as and when the user request a page.<\/li>\r\n \t<li>Cookie is a name=value pair which is stored on user\u2019s browser.<\/li>\r\n \t<li>On the server, cookie is created by web application and sends it to browser.<\/li>\r\n \t<li>On the client, the cookie is saved in the browser and sends it back to the server each time when server access a page.<\/li>\r\n \t<li>By default, cookie exists till the user\u2019s browser is open. However, it can be store in the user browser for maximum 3 years by set to persistent cookie.<\/li>\r\n \t<li>Cookie can be disabled in the browser by user. So you cannot always count that cookie is enabled by all the users.<\/li>\r\n \t<li>Browser generally accept<\/li>\r\n<\/ul>\r\n<ol>\r\n \t<li>20 cookies from each site<\/li>\r\n \t<li>300 cookies in total<\/li>\r\n \t<li>Maximum storage capacity for each cookie is 4 kilobytes<\/li>\r\n<\/ol>\r\n<ul>\r\n \t<li>Cookie can relate to one or more sub domain names.<\/li>\r\n<\/ul>\r\n<strong>\u00a0 \u00a0 \u00a0Most common uses for cookies are<\/strong>\r\n<ul>\r\n \t<li style=\"text-align: justify\"><strong>To Allow users to skip login and registration forms: <\/strong>Cookie can gather data like username, password , address or credit card number. By using this stored data, user can skip login and registration forms data.<\/li>\r\n \t<li style=\"text-align: justify\"><strong>To Customize Pages: <\/strong>Cookie can customize pages as per user choices and display information like sports score, weather information and stock price.<\/li>\r\n \t<li style=\"text-align: justify\"><strong>To Focus advertising: <\/strong>Cookie can be used for advertising like banner ads which is used to target user\u2019s interest.<\/li>\r\n<\/ul>\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0<\/strong>\r\n\r\n<strong>Third party cookie<\/strong>\r\n<ul>\r\n \t<li style=\"text-align: justify\">The common misconceptions are that cookies are harmful because they can store user information and retrieve each time.<\/li>\r\n \t<li style=\"text-align: justify\">There are rumors like cookie can transmit viruses, steal passwords and copy file from user\u2019s hard-disk. Although these rumors are not true, cookies can be misused sometimes. For example, some advertisers use cookie to track the web site visited by users. These cookies are known as third party cookie because it is sent from advertisers, not by the user\u2019s visited website.<\/li>\r\n \t<li style=\"text-align: justify\">To fight with third party cookies, modern web browsers are facilitated to block third party cookies by changing browser options.<\/li>\r\n \t<li style=\"text-align: justify\">Cookies are consisted of plain text so they cannot directly modify user\u2019s computer, generate span, steal files or create pop-up ads.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong>Set Cookie<\/strong>\r\n<ul>\r\n \t<li>To set a cookie setcookie function is used<\/li>\r\n \t<li>setcookie function creates a cookie and set it in the browser.<\/li>\r\n \t<li>Setcookie function must be called before any HTML output is sent from the application.<\/li>\r\n \t<li>Cookies can be classified as<\/li>\r\n<\/ul>\r\n<ol>\r\n \t<li>Per-session<\/li>\r\n \t<li>Persistent<\/li>\r\n<\/ol>\r\n<ul>\r\n \t<li>Per-session cookie exists till the browser is open.<\/li>\r\n \t<li>Persistent cookie exists till the specified expiration date.<\/li>\r\n \t<li>The syntax to setcookie is shown below<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nsetcookie($name, $value, $expire, $path, $domain, $secure, $httponly)\r\n\r\nWhere\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022 $name is the name of the cookie<\/p>\r\n<p style=\"text-align: justify\">\u2022 $value is the value of the cookie. By default value is empty string<\/p>\r\n<p style=\"text-align: justify\">\u2022 $expire is the expiration date of cookie in timestamp format. Default value is 0 which means it is a per-session cookie.<\/p>\r\n<p style=\"text-align: justify\">\u2022 $path is the path on the server to available cookie. To make available cookie to all the directories on the current server, $path is set to \u2018\/\u2019\u2019. Default directory is the directory of PHP file which setting\u2019s the cookie.<\/p>\r\n<p style=\"text-align: justify\">\u2022 $domain is the domain where cookie is available. Default is the name of the server that setting\u2019s the cookie.<\/p>\r\n<p style=\"text-align: justify\">\u2022 $secure is true or false. True means cookie is only available if it is sent using https.<\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">\u2022 $httponly is true or false. True means cookie is only available http protocol only. It is not available through client side scripting language such as java script. Default value is false.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 \u00a0<strong>Following example shows how to set a cookie<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$name = 'Hiren';\r\n\r\n$value = 'password';\r\n\r\n$exp = time() + 86400;\r\n\r\necho \"&lt;br&gt; $exp\";\r\n\r\nsetcookie($name,$value,$exp);\r\n\r\necho \"&lt;br&gt; Cookie is set\";\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\nThe output of the above code is:1502354508 Cookie is set\r\n\r\n&nbsp;\r\n\r\n<strong>Get Cookie<\/strong>\r\n<ul>\r\n \t<li>To get the value of stored cookie, $_COOKIE super global variable is used<\/li>\r\n \t<li>$_COOKIE is an associative array.<\/li>\r\n \t<li>In $_COOKIE associate where array cookie name is key and cookie value is a value<\/li>\r\n<\/ul>\r\n<strong>\u00a0 \u00a0 \u00a0Following example shows how to get a cookie value<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$name = $_COOKIE['Hiren'];\r\n\r\necho \"&lt;br&gt; The value of cookie is :\". $name;\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\nThe output of the above code is:<strong>The value of cookie is :password<\/strong>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">Delete Cookie<\/strong>\r\n<div>\r\n<ul>\r\n \t<li style=\"text-align: justify\">To delete a cookie from browser, code the cookie with the same name as the time of set it, set value to empty string and set expiration attribute is the past date.<\/li>\r\n \t<li>All other remaining parameters value must be the same when cookie was created.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong>Following example shows how to delete a cookie<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$expire = time() - 86400;\r\n\r\n$name = 'Hiren';\r\n\r\nsetcookie($name,'',$expire);\r\n\r\nif(isset($_COOKIE['Hiren']))\r\n\r\n{\r\n\r\necho \"&lt;br&gt; Cookie is set\";\r\n\r\n}\r\n\r\nelse\r\n\r\n{\r\n\r\necho \"&lt;br&gt; Cookie is not set\";\r\n\r\n}\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\nThe output of the above code is:<strong>Cookie is not set<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Enabled or Disabled Cookie in a browser<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022 To test the behavior of application when cookie is disabled, cookie can be disabled.<\/p>\r\n\u2022 To test the behavior of application when cookie is enabled, cookie can be enabled\r\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">\u2022 For example, to display a message which shows that the cookies are required for the application to function properly.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 How to enabled or disabled cookie is depended on browser.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 Following code shows how you can view cookies in your browser.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$name = 'Hiren';\r\n\r\n$value = 'password';\r\n\r\n$exp = time() + 86400;\r\n\r\necho \"&lt;br&gt; $exp\";\r\n\r\nsetcookie($name,$value,$exp);\r\n\r\necho \"&lt;br&gt; Cookie is set\";\r\n\r\nsetcookie('Age',19,$exp);\r\n\r\necho \"&lt;br&gt; 2nd Cookie is set &lt;br&gt;\";\r\n\r\nprint_r($_COOKIE);\r\n\r\n<strong>?&gt;<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>The output of the above code is shown below<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>1502386488<\/strong>\r\n\r\n<strong>Cookie is set<\/strong>\r\n\r\n<strong>2nd Cookie is set<\/strong>\r\n\r\n<strong>Array ( [Hiren] =&gt; password [Age] =&gt; 19 )<\/strong>\r\n<ul>\r\n \t<li style=\"text-align: justify\">To view the cookie in chrome browser, go to settings -&gt; show advance settings -&gt; privacy -&gt;content settings-&gt;All cookies and site data-&gt;localhost<\/li>\r\n \t<li>It will display all the information of each cookie like Name, content, domain, path created and expires.<\/li>\r\n<\/ul>\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>SESSION<\/strong>\r\n<ul>\r\n \t<li>Session tracking is a technique to keep track of the user\u2019s movement around the website.<\/li>\r\n \t<li>Session tracking is used to maintain the state in a web application.<\/li>\r\n \t<li>HTTP is a stateless protocol. So once a browser send request to the server, and server responds to the browser, the connection between browser(client) and server is dropped.<\/li>\r\n \t<li>Cookie is used to store a SessionID in each browser by default. Then, the cookie is passed by the browser to the server for each request.<\/li>\r\n \t<li>URL encoding is used to store sessionID in the URL of each page of an application when cookie is disabled.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong>Why session tracking is difficult with HTTP<\/strong>\r\n\r\n<img class=\"size-full wp-image-250 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-117.png\" alt=\"\" width=\"723\" height=\"287\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As shown in above figure, the browser send a first HTTP request to the web server. The web server response the browser by sending the requested page and the connection between browser and web server is dropped. For the following HTTP requests from web browser, the web server has no way to associate the web browser with its previous request.<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">How PHP keeps track of session<\/strong>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-251 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-118.png\" alt=\"\" width=\"715\" height=\"440\" \/>\r\n<p style=\"text-align: justify\">As shown in above figure, the browser sends the first HTTP request to the web server. The PHP engine creates a session and assign ID to it. The web server sends the requested page and ID for the session as a cookie it to the browser. For the following HTTP requests, web browser sends the session ID cookie to the web server. PHP uses the session ID to associate browser with its session.<\/p>\r\n&nbsp;\r\n\r\n<strong>SESSION START<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022 Though session starting is built in to PHP, it is not automatically start.<\/p>\r\n<p style=\"text-align: justify\">\u2022 To start a new session or resume the previous session, session_start function must be called at the starting of each page in the application which needs to access session data.<\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">\u2022 session_start function prompts PHP to check sessionID in the request and in case , it is not available, session_start function creates a new sessionID and session cookie.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">\u2022 Session_start function may set a cookie, so it must be called before any HTML output sent to the browser.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 By default, per-session cookie is used to store the sessionID in the user\u2019s browser by PHP.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>Method<\/strong><\/td>\r\n<td><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"4\">session_start( )\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;<\/td>\r\n<td rowspan=\"4\">Start a new session or resume the previous session. This\r\n\r\nfunction must be called before any HTML output sent\r\n\r\nto the browser by the page. Returns true if successful\r\n\r\notherwise false.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n\u2022 session_start( ) function Starts the session with default cookie parameter.\r\n\r\n\u2022\u00a0 &lt;?php\r\n\r\n\u2022 session_start();\r\n\r\n\u2022 echo \"&lt;br&gt; Session is started\";\r\n\r\n\u2022 ?&gt;\r\n\r\n&nbsp;\r\n\r\n<strong>Session cookie<\/strong>\r\n<ul>\r\n \t<li>To control, how session cookie works, session_set_cookie_params function is used.<\/li>\r\n \t<li>session_set_cookie_params is used to set the parameters of the session cookie.<\/li>\r\n \t<li>session_set_cookie_params ($lifetime, $path, $domain, $secure, $httponly)<\/li>\r\n \t<li>Where<\/li>\r\n \t<li>$lifetime is the timespan of session cookie in seconds in which cookie is live. Default is 0.<\/li>\r\n \t<li style=\"text-align: justify\">$lifetime is the only compulsory parameters. All other parameters are optional for session_set_cookie_params function.<\/li>\r\n<\/ul>\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n\/\/Start a session with Custom cookie parameters $lifetime = 60 * 60 * 24 * 7 ; \/\/1 week\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 session_set_cookie_params($lifetime , '\/'); session_start();\r\n\r\necho \"&lt;br&gt; session is started &lt;br&gt;\";\r\n\r\n$ans = $_COOKIE['PHPSESSID'];\r\n\r\necho \"&lt;br&gt; Value of session cookie is : \".$ans; ?&gt;\r\n\r\n&nbsp;\r\n\r\nsession is started\r\n\r\n&nbsp;\r\n\r\nValue of session cookie is : dn8p6792bs1el76o9mco1rjka7\r\n\r\n&nbsp;\r\n\r\n<strong>Get and Set Session Variable<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022 Once a session is start, $_SESSION variable is used to set and get the user\u2019s data for the session.\r\n\r\n\u2022 $_SESSION variable is an associative array.\r\n\r\n\u2022 isset function can be used to test if an element already exist in the $_SESSION array.\r\n\r\n\u2022 To remove an element from the $_SESSION array, the unset function is used.\r\n<p style=\"text-align: justify\">\u2022 However the unset function should not use on the $_SESSION array itself as it can cause unpredictable results.<\/p>\r\n<span style=\"text-align: initial;font-size: 1em\">\u2022\u00a0 $_SESSION array should be set to an empty array to remove its contents.<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n\/\/Set a variable in session\r\n\r\n$_SESSION['pcode'] = 'ABC-1234';\r\n\r\n\/\/Get a varibale from a session\r\n\r\n$productcode = $_SESSION['pcode'];\r\n\r\necho \"&lt;br&gt; Product Code is: $productcode\";\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\nProduct Code is: ABC-1234\r\n\r\n&nbsp;\r\n\r\n<strong>Set and Get arrays<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n\/\/Set an array in a session\r\n\r\nIf(!isset($_SESSION['cart']))\r\n\r\n{\r\n\r\n$_SESSION['cart'] = array();\r\n\r\n}\r\n\r\n\/\/Add an element to an array that is stored in a session $_SESSION['cart']['pcode1'] = 'value1';\r\n\r\n$_SESSION['cart']['pcode2'] = 'value2';\r\n\r\n\/\/Get and use an array that\u2019s stored in a session $cart = $_SESSION['cart'];\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">foreach($cart as $p=&gt;$item)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo '&lt;li&gt;'.$p. ':' . $item.'&lt;\/li&gt;';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 The output of above code is:\r\n<ol>\r\n \t<li>pcode1:value1<\/li>\r\n \t<li>pcode2:value2<\/li>\r\n<\/ol>\r\n<strong>\u00a0 \u00a0 \u00a0Remove variables from session<\/strong>\r\n<ul>\r\n \t<li style=\"text-align: justify\">To remove a session variable, unset function is used. The unset function should not be used to remove all $_SESSION array itself because it can cause unpredictable result.<\/li>\r\n \t<li>To remove all session variables, $_SESSION array can be set to an empty array.<\/li>\r\n \t<li>Following code shows how to remove a session variable<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\nIf(!isset($_SESSION['cart']))\r\n\r\n{\r\n\r\n$_SESSION['cart'] = array();\r\n\r\n}\r\n\r\n\/\/Add an element to an array that is stored in a session $_SESSION['cart']['pcode1'] = 'value1'; $_SESSION['cart']['pcode2'] = 'value2';\r\n\r\n\/\/Get and use an array that\u2019s stored in a session $cart = $_SESSION['cart']; foreach($cart as $p=&gt;$item)\r\n\r\n{\r\n\r\necho '&lt;li&gt;'.$p. ':' . $item.'&lt;\/li&gt;';\r\n\r\n}\r\n\r\n\/\/Remove a session variable if(empty($_SESSION['cart']))\r\n\r\n{\r\n\r\necho \"&lt;br&gt; Session cart is unset\";\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">else<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Session cart is set\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">unset($_SESSION['cart']);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if(empty($_SESSION['cart']))<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Session cart is unset\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/Remove all session variable<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$_SESSION = array();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/to display<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/*<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$cart = $_SESSION['cart'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">foreach($cart as $p=&gt;$item)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo '&lt;li&gt;'.$p. ':' . $item.'&lt;\/li&gt;';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">*\/<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThe output of the above code is:\r\n\r\n&nbsp;\r\n\r\npcode1:value1\r\n\r\npcode2:value2\r\n\r\nSession cart is set\r\n\r\nSession cart is unset\r\n\r\n&nbsp;\r\n\r\n<strong>End Session<\/strong>\r\n<p style=\"text-align: justify\">\u2022\u00a0 A session ends<\/p>\r\n<p style=\"text-align: justify\">\u2022 when the browser is closed by the user,<\/p>\r\n<p style=\"text-align: justify\">\u2022 when a request is not received within specified amount of time (by default 24 minutes )or<\/p>\r\n<p style=\"text-align: justify\">\u2022 when the session_destroy function is called .<\/p>\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em;text-align: initial\">\u2022 To remove all data from the client and the server associated with the session,<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 the session data are cleared from memory<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 the session_destroy function is called and<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 the setcookie function is used to delete the session cookie.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 To get the name of the session cookie, session_name function is used.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 The session cookie by default name is \"PHPSESSID\"<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">\u2022 The session_get_cookie_params function is used to gets an associated array that contain all of the parameters for the session cookie. The parameters are same as session_set_cookie_params<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Function to end a session<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022 session_destroy()\r\n\r\n\u2022 Ends a session. Returns TRUE if successful and FALSE otherwise.\r\n\r\n\u2022 End a session\r\n\r\n&nbsp;\r\n\r\n\/\/clear session data from memory\r\n\r\n$_SESSION = array();\r\n\r\n\/\/\u00a0\u00a0 Clean up the session ID session_destroy();\r\n\r\n&nbsp;\r\n\r\n<strong>Delete the session cookie from the browser<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n\/\/Delete the session cookie from the browser\r\n\r\n\/\/\u00a0\u00a0 Get name of session cookie $name = session_name();\r\n\r\n\/\/\u00a0\u00a0 Create expire date in past\r\n\r\n$expire = strtotime('-1 year');\r\n\r\n\/\/\u00a0\u00a0 Get session parameters\r\n\r\n$params = session_get_cookie_params();\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$path = $params['path'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$domain = $params['domain'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$secure = $params['secure'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$httponly = $params['httponly'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">setcookie($name,'', $expire, $path, $domain, $secure, $httponly);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if(isset($_COOKIE[$name]))<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Session cookie is set\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">else<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Session cookie is not set\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n\r\nSession cookie is not set\r\n\r\n&nbsp;\r\n\r\n<strong>Functions to manage sessions<\/strong>\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>Function<\/strong><\/td>\r\n<td><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>session_name( )<\/td>\r\n<td>Get the name of session cookie. By default name is PHPSESSID.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"3\">session_id([$id ])\r\n\r\n&nbsp;\r\n\r\n&nbsp;<\/td>\r\n<td rowspan=\"3\">Set the session is to $id. If parameter $id is not provided, this\r\n\r\nfunction gets the current session id. If no session exists, this\r\n\r\nfunction returns empty string.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"2\">session_write_close( )\r\n\r\n&nbsp;<\/td>\r\n<td rowspan=\"2\">Close the current session and saves session data. It is only required\r\n\r\nin special case like redirects<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"3\">session_regenerate_id( )\r\n\r\n&nbsp;\r\n\r\n&nbsp;<\/td>\r\n<td rowspan=\"3\">Creates a new session id for the current session. This function can\r\n\r\nbe used to help to prevent session hijacking.It returns true if\r\n\r\nsuccessful otherwise returns false.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\nExample\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n\/\/Get the name of the session cookie\r\n\r\n\/\/ by default\u00a0\u00a0\u00a0\u00a0 PHPSESSID\r\n\r\n$name = session_name();\r\n\r\necho \"&lt;br&gt; Session name is : \".$name;\r\n\r\n\/\/Get the value of the session ID\r\n\r\n\/\/ for example,\r\n\r\n$id = session_id();\r\n\r\necho \"&lt;br&gt; Session id is : \".$id;\r\n\r\n\/\/Set the session ID\r\n\r\nsession_id('abc123');\r\n\r\n$newid = session_id();\r\n\r\necho \"&lt;br&gt; New Session id is : \".$newid;\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n\r\nSession name is : PHPSESSID\r\n\r\nSession id is :\r\n\r\nNew Session id is : abc123\r\n\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on COOKIE and SESSION<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/kkOGRQOZZUA\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<strong>References:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\r\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional, Apress<\/p>\r\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass:\r\nBeginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\r\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O'Reilly Media<\/p>\r\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove: Professional PHP,Wrox<\/p>\r\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\r\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\r\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\r\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\r\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide, Pearson Education<\/p>\r\n<p style=\"text-align: justify\">11. http:\/\/www.php.net\/<\/p>\r\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\r\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/kkOGRQOZZUA\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Objectives<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0 <strong>Cookie<\/strong><\/p>\n<p>\u2022\u00a0\u00a0<strong>Session<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>COOKIE<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify\">In any web application, it is essential to keep track of users as he\/she moves in a web pages of web application. To keep track of user\u2019s movement within a web application session and cookies are used.<\/li>\n<li style=\"text-align: justify\">Cookie provides a way for web application to store information in the user\u2019s web browser and can retrieve the information of cookie every time as and when the user request a page.<\/li>\n<li>Cookie is a name=value pair which is stored on user\u2019s browser.<\/li>\n<li>On the server, cookie is created by web application and sends it to browser.<\/li>\n<li>On the client, the cookie is saved in the browser and sends it back to the server each time when server access a page.<\/li>\n<li>By default, cookie exists till the user\u2019s browser is open. However, it can be store in the user browser for maximum 3 years by set to persistent cookie.<\/li>\n<li>Cookie can be disabled in the browser by user. So you cannot always count that cookie is enabled by all the users.<\/li>\n<li>Browser generally accept<\/li>\n<\/ul>\n<ol>\n<li>20 cookies from each site<\/li>\n<li>300 cookies in total<\/li>\n<li>Maximum storage capacity for each cookie is 4 kilobytes<\/li>\n<\/ol>\n<ul>\n<li>Cookie can relate to one or more sub domain names.<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a0 \u00a0Most common uses for cookies are<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify\"><strong>To Allow users to skip login and registration forms: <\/strong>Cookie can gather data like username, password , address or credit card number. By using this stored data, user can skip login and registration forms data.<\/li>\n<li style=\"text-align: justify\"><strong>To Customize Pages: <\/strong>Cookie can customize pages as per user choices and display information like sports score, weather information and stock price.<\/li>\n<li style=\"text-align: justify\"><strong>To Focus advertising: <\/strong>Cookie can be used for advertising like banner ads which is used to target user\u2019s interest.<\/li>\n<\/ul>\n<\/div>\n<div>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>Third party cookie<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify\">The common misconceptions are that cookies are harmful because they can store user information and retrieve each time.<\/li>\n<li style=\"text-align: justify\">There are rumors like cookie can transmit viruses, steal passwords and copy file from user\u2019s hard-disk. Although these rumors are not true, cookies can be misused sometimes. For example, some advertisers use cookie to track the web site visited by users. These cookies are known as third party cookie because it is sent from advertisers, not by the user\u2019s visited website.<\/li>\n<li style=\"text-align: justify\">To fight with third party cookies, modern web browsers are facilitated to block third party cookies by changing browser options.<\/li>\n<li style=\"text-align: justify\">Cookies are consisted of plain text so they cannot directly modify user\u2019s computer, generate span, steal files or create pop-up ads.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Set Cookie<\/strong><\/p>\n<ul>\n<li>To set a cookie setcookie function is used<\/li>\n<li>setcookie function creates a cookie and set it in the browser.<\/li>\n<li>Setcookie function must be called before any HTML output is sent from the application.<\/li>\n<li>Cookies can be classified as<\/li>\n<\/ul>\n<ol>\n<li>Per-session<\/li>\n<li>Persistent<\/li>\n<\/ol>\n<ul>\n<li>Per-session cookie exists till the browser is open.<\/li>\n<li>Persistent cookie exists till the specified expiration date.<\/li>\n<li>The syntax to setcookie is shown below<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>setcookie($name, $value, $expire, $path, $domain, $secure, $httponly)<\/p>\n<p>Where<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022 $name is the name of the cookie<\/p>\n<p style=\"text-align: justify\">\u2022 $value is the value of the cookie. By default value is empty string<\/p>\n<p style=\"text-align: justify\">\u2022 $expire is the expiration date of cookie in timestamp format. Default value is 0 which means it is a per-session cookie.<\/p>\n<p style=\"text-align: justify\">\u2022 $path is the path on the server to available cookie. To make available cookie to all the directories on the current server, $path is set to \u2018\/\u2019\u2019. Default directory is the directory of PHP file which setting\u2019s the cookie.<\/p>\n<p style=\"text-align: justify\">\u2022 $domain is the domain where cookie is available. Default is the name of the server that setting\u2019s the cookie.<\/p>\n<p style=\"text-align: justify\">\u2022 $secure is true or false. True means cookie is only available if it is sent using https.<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">\u2022 $httponly is true or false. True means cookie is only available http protocol only. It is not available through client side scripting language such as java script. Default value is false.<\/span><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 \u00a0<strong>Following example shows how to set a cookie<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$name = &#8216;Hiren&#8217;;<\/p>\n<p>$value = &#8216;password&#8217;;<\/p>\n<p>$exp = time() + 86400;<\/p>\n<p>echo &#8220;&lt;br&gt; $exp&#8221;;<\/p>\n<p>setcookie($name,$value,$exp);<\/p>\n<p>echo &#8220;&lt;br&gt; Cookie is set&#8221;;<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>The output of the above code is:1502354508 Cookie is set<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Get Cookie<\/strong><\/p>\n<ul>\n<li>To get the value of stored cookie, $_COOKIE super global variable is used<\/li>\n<li>$_COOKIE is an associative array.<\/li>\n<li>In $_COOKIE associate where array cookie name is key and cookie value is a value<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a0 \u00a0Following example shows how to get a cookie value<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$name = $_COOKIE[&#8216;Hiren&#8217;];<\/p>\n<p>echo &#8220;&lt;br&gt; The value of cookie is :&#8221;. $name;<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>The output of the above code is:<strong>The value of cookie is :password<\/strong><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">Delete Cookie<\/strong><\/p>\n<div>\n<ul>\n<li style=\"text-align: justify\">To delete a cookie from browser, code the cookie with the same name as the time of set it, set value to empty string and set expiration attribute is the past date.<\/li>\n<li>All other remaining parameters value must be the same when cookie was created.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Following example shows how to delete a cookie<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$expire = time() &#8211; 86400;<\/p>\n<p>$name = &#8216;Hiren&#8217;;<\/p>\n<p>setcookie($name,&#8221;,$expire);<\/p>\n<p>if(isset($_COOKIE[&#8216;Hiren&#8217;]))<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;br&gt; Cookie is set&#8221;;<\/p>\n<p>}<\/p>\n<p>else<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;br&gt; Cookie is not set&#8221;;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>The output of the above code is:<strong>Cookie is not set<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Enabled or Disabled Cookie in a browser<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022 To test the behavior of application when cookie is disabled, cookie can be disabled.<\/p>\n<p>\u2022 To test the behavior of application when cookie is enabled, cookie can be enabled<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">\u2022 For example, to display a message which shows that the cookies are required for the application to function properly.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 How to enabled or disabled cookie is depended on browser.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 Following code shows how you can view cookies in your browser.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$name = &#8216;Hiren&#8217;;<\/p>\n<p>$value = &#8216;password&#8217;;<\/p>\n<p>$exp = time() + 86400;<\/p>\n<p>echo &#8220;&lt;br&gt; $exp&#8221;;<\/p>\n<p>setcookie($name,$value,$exp);<\/p>\n<p>echo &#8220;&lt;br&gt; Cookie is set&#8221;;<\/p>\n<p>setcookie(&#8216;Age&#8217;,19,$exp);<\/p>\n<p>echo &#8220;&lt;br&gt; 2nd Cookie is set &lt;br&gt;&#8221;;<\/p>\n<p>print_r($_COOKIE);<\/p>\n<p><strong>?&gt;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>The output of the above code is shown below<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>1502386488<\/strong><\/p>\n<p><strong>Cookie is set<\/strong><\/p>\n<p><strong>2nd Cookie is set<\/strong><\/p>\n<p><strong>Array ( [Hiren] =&gt; password [Age] =&gt; 19 )<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify\">To view the cookie in chrome browser, go to settings -&gt; show advance settings -&gt; privacy -&gt;content settings-&gt;All cookies and site data-&gt;localhost<\/li>\n<li>It will display all the information of each cookie like Name, content, domain, path created and expires.<\/li>\n<\/ul>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>SESSION<\/strong><\/p>\n<ul>\n<li>Session tracking is a technique to keep track of the user\u2019s movement around the website.<\/li>\n<li>Session tracking is used to maintain the state in a web application.<\/li>\n<li>HTTP is a stateless protocol. So once a browser send request to the server, and server responds to the browser, the connection between browser(client) and server is dropped.<\/li>\n<li>Cookie is used to store a SessionID in each browser by default. Then, the cookie is passed by the browser to the server for each request.<\/li>\n<li>URL encoding is used to store sessionID in the URL of each page of an application when cookie is disabled.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Why session tracking is difficult with HTTP<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-250 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-117.png\" alt=\"\" width=\"723\" height=\"287\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-117.png 723w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-117-300x119.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-117-65x26.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-117-225x89.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-117-350x139.png 350w\" sizes=\"auto, (max-width: 723px) 100vw, 723px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As shown in above figure, the browser send a first HTTP request to the web server. The web server response the browser by sending the requested page and the connection between browser and web server is dropped. For the following HTTP requests from web browser, the web server has no way to associate the web browser with its previous request.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">How PHP keeps track of session<\/strong><\/p>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-251 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-118.png\" alt=\"\" width=\"715\" height=\"440\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-118.png 715w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-118-300x185.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-118-65x40.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-118-225x138.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-118-350x215.png 350w\" sizes=\"auto, (max-width: 715px) 100vw, 715px\" \/><\/p>\n<p style=\"text-align: justify\">As shown in above figure, the browser sends the first HTTP request to the web server. The PHP engine creates a session and assign ID to it. The web server sends the requested page and ID for the session as a cookie it to the browser. For the following HTTP requests, web browser sends the session ID cookie to the web server. PHP uses the session ID to associate browser with its session.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>SESSION START<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022 Though session starting is built in to PHP, it is not automatically start.<\/p>\n<p style=\"text-align: justify\">\u2022 To start a new session or resume the previous session, session_start function must be called at the starting of each page in the application which needs to access session data.<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">\u2022 session_start function prompts PHP to check sessionID in the request and in case , it is not available, session_start function creates a new sessionID and session cookie.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">\u2022 Session_start function may set a cookie, so it must be called before any HTML output sent to the browser.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 By default, per-session cookie is used to store the sessionID in the user\u2019s browser by PHP.<\/span><\/p>\n<\/div>\n<div>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>Method<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td rowspan=\"4\">session_start( )<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/td>\n<td rowspan=\"4\">Start a new session or resume the previous session. This<\/p>\n<p>function must be called before any HTML output sent<\/p>\n<p>to the browser by the page. Returns true if successful<\/p>\n<p>otherwise false.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 session_start( ) function Starts the session with default cookie parameter.<\/p>\n<p>\u2022\u00a0 &lt;?php<\/p>\n<p>\u2022 session_start();<\/p>\n<p>\u2022 echo &#8220;&lt;br&gt; Session is started&#8221;;<\/p>\n<p>\u2022 ?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Session cookie<\/strong><\/p>\n<ul>\n<li>To control, how session cookie works, session_set_cookie_params function is used.<\/li>\n<li>session_set_cookie_params is used to set the parameters of the session cookie.<\/li>\n<li>session_set_cookie_params ($lifetime, $path, $domain, $secure, $httponly)<\/li>\n<li>Where<\/li>\n<li>$lifetime is the timespan of session cookie in seconds in which cookie is live. Default is 0.<\/li>\n<li style=\"text-align: justify\">$lifetime is the only compulsory parameters. All other parameters are optional for session_set_cookie_params function.<\/li>\n<\/ul>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/Start a session with Custom cookie parameters $lifetime = 60 * 60 * 24 * 7 ; \/\/1 week\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 session_set_cookie_params($lifetime , &#8216;\/&#8217;); session_start();<\/p>\n<p>echo &#8220;&lt;br&gt; session is started &lt;br&gt;&#8221;;<\/p>\n<p>$ans = $_COOKIE[&#8216;PHPSESSID&#8217;];<\/p>\n<p>echo &#8220;&lt;br&gt; Value of session cookie is : &#8220;.$ans; ?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>session is started<\/p>\n<p>&nbsp;<\/p>\n<p>Value of session cookie is : dn8p6792bs1el76o9mco1rjka7<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Get and Set Session Variable<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 Once a session is start, $_SESSION variable is used to set and get the user\u2019s data for the session.<\/p>\n<p>\u2022 $_SESSION variable is an associative array.<\/p>\n<p>\u2022 isset function can be used to test if an element already exist in the $_SESSION array.<\/p>\n<p>\u2022 To remove an element from the $_SESSION array, the unset function is used.<\/p>\n<p style=\"text-align: justify\">\u2022 However the unset function should not use on the $_SESSION array itself as it can cause unpredictable results.<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u2022\u00a0 $_SESSION array should be set to an empty array to remove its contents.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/Set a variable in session<\/p>\n<p>$_SESSION[&#8216;pcode&#8217;] = &#8216;ABC-1234&#8217;;<\/p>\n<p>\/\/Get a varibale from a session<\/p>\n<p>$productcode = $_SESSION[&#8216;pcode&#8217;];<\/p>\n<p>echo &#8220;&lt;br&gt; Product Code is: $productcode&#8221;;<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>Product Code is: ABC-1234<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Set and Get arrays<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/Set an array in a session<\/p>\n<p>If(!isset($_SESSION[&#8216;cart&#8217;]))<\/p>\n<p>{<\/p>\n<p>$_SESSION[&#8216;cart&#8217;] = array();<\/p>\n<p>}<\/p>\n<p>\/\/Add an element to an array that is stored in a session $_SESSION[&#8216;cart&#8217;][&#8216;pcode1&#8217;] = &#8216;value1&#8217;;<\/p>\n<p>$_SESSION[&#8216;cart&#8217;][&#8216;pcode2&#8217;] = &#8216;value2&#8217;;<\/p>\n<p>\/\/Get and use an array that\u2019s stored in a session $cart = $_SESSION[&#8216;cart&#8217;];<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">foreach($cart as $p=&gt;$item)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8216;&lt;li&gt;&#8217;.$p. &#8216;:&#8217; . $item.'&lt;\/li&gt;&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 The output of above code is:<\/p>\n<ol>\n<li>pcode1:value1<\/li>\n<li>pcode2:value2<\/li>\n<\/ol>\n<p><strong>\u00a0 \u00a0 \u00a0Remove variables from session<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify\">To remove a session variable, unset function is used. The unset function should not be used to remove all $_SESSION array itself because it can cause unpredictable result.<\/li>\n<li>To remove all session variables, $_SESSION array can be set to an empty array.<\/li>\n<li>Following code shows how to remove a session variable<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>If(!isset($_SESSION[&#8216;cart&#8217;]))<\/p>\n<p>{<\/p>\n<p>$_SESSION[&#8216;cart&#8217;] = array();<\/p>\n<p>}<\/p>\n<p>\/\/Add an element to an array that is stored in a session $_SESSION[&#8216;cart&#8217;][&#8216;pcode1&#8217;] = &#8216;value1&#8217;; $_SESSION[&#8216;cart&#8217;][&#8216;pcode2&#8217;] = &#8216;value2&#8217;;<\/p>\n<p>\/\/Get and use an array that\u2019s stored in a session $cart = $_SESSION[&#8216;cart&#8217;]; foreach($cart as $p=&gt;$item)<\/p>\n<p>{<\/p>\n<p>echo &#8216;&lt;li&gt;&#8217;.$p. &#8216;:&#8217; . $item.'&lt;\/li&gt;&#8217;;<\/p>\n<p>}<\/p>\n<p>\/\/Remove a session variable if(empty($_SESSION[&#8216;cart&#8217;]))<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;br&gt; Session cart is unset&#8221;;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">else<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Session cart is set&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">unset($_SESSION[&#8216;cart&#8217;]);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if(empty($_SESSION[&#8216;cart&#8217;]))<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Session cart is unset&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/Remove all session variable<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$_SESSION = array();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/to display<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/*<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$cart = $_SESSION[&#8216;cart&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">foreach($cart as $p=&gt;$item)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8216;&lt;li&gt;&#8217;.$p. &#8216;:&#8217; . $item.'&lt;\/li&gt;&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">*\/<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>The output of the above code is:<\/p>\n<p>&nbsp;<\/p>\n<p>pcode1:value1<\/p>\n<p>pcode2:value2<\/p>\n<p>Session cart is set<\/p>\n<p>Session cart is unset<\/p>\n<p>&nbsp;<\/p>\n<p><strong>End Session<\/strong><\/p>\n<p style=\"text-align: justify\">\u2022\u00a0 A session ends<\/p>\n<p style=\"text-align: justify\">\u2022 when the browser is closed by the user,<\/p>\n<p style=\"text-align: justify\">\u2022 when a request is not received within specified amount of time (by default 24 minutes )or<\/p>\n<p style=\"text-align: justify\">\u2022 when the session_destroy function is called .<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em;text-align: initial\">\u2022 To remove all data from the client and the server associated with the session,<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 the session data are cleared from memory<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 the session_destroy function is called and<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 the setcookie function is used to delete the session cookie.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 To get the name of the session cookie, session_name function is used.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 The session cookie by default name is &#8220;PHPSESSID&#8221;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">\u2022 The session_get_cookie_params function is used to gets an associated array that contain all of the parameters for the session cookie. The parameters are same as session_set_cookie_params<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Function to end a session<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 session_destroy()<\/p>\n<p>\u2022 Ends a session. Returns TRUE if successful and FALSE otherwise.<\/p>\n<p>\u2022 End a session<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/clear session data from memory<\/p>\n<p>$_SESSION = array();<\/p>\n<p>\/\/\u00a0\u00a0 Clean up the session ID session_destroy();<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Delete the session cookie from the browser<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/Delete the session cookie from the browser<\/p>\n<p>\/\/\u00a0\u00a0 Get name of session cookie $name = session_name();<\/p>\n<p>\/\/\u00a0\u00a0 Create expire date in past<\/p>\n<p>$expire = strtotime(&#8216;-1 year&#8217;);<\/p>\n<p>\/\/\u00a0\u00a0 Get session parameters<\/p>\n<p>$params = session_get_cookie_params();<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$path = $params[&#8216;path&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$domain = $params[&#8216;domain&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$secure = $params[&#8216;secure&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$httponly = $params[&#8216;httponly&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">setcookie($name,&#8221;, $expire, $path, $domain, $secure, $httponly);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if(isset($_COOKIE[$name]))<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Session cookie is set&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">else<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Session cookie is not set&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Session cookie is not set<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Functions to manage sessions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>Function<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td>session_name( )<\/td>\n<td>Get the name of session cookie. By default name is PHPSESSID.<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"3\">session_id([$id ])<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/td>\n<td rowspan=\"3\">Set the session is to $id. If parameter $id is not provided, this<\/p>\n<p>function gets the current session id. If no session exists, this<\/p>\n<p>function returns empty string.<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">session_write_close( )<\/p>\n<p>&nbsp;<\/td>\n<td rowspan=\"2\">Close the current session and saves session data. It is only required<\/p>\n<p>in special case like redirects<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"3\">session_regenerate_id( )<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/td>\n<td rowspan=\"3\">Creates a new session id for the current session. This function can<\/p>\n<p>be used to help to prevent session hijacking.It returns true if<\/p>\n<p>successful otherwise returns false.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Example<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/Get the name of the session cookie<\/p>\n<p>\/\/ by default\u00a0\u00a0\u00a0\u00a0 PHPSESSID<\/p>\n<p>$name = session_name();<\/p>\n<p>echo &#8220;&lt;br&gt; Session name is : &#8220;.$name;<\/p>\n<p>\/\/Get the value of the session ID<\/p>\n<p>\/\/ for example,<\/p>\n<p>$id = session_id();<\/p>\n<p>echo &#8220;&lt;br&gt; Session id is : &#8220;.$id;<\/p>\n<p>\/\/Set the session ID<\/p>\n<p>session_id(&#8216;abc123&#8217;);<\/p>\n<p>$newid = session_id();<\/p>\n<p>echo &#8220;&lt;br&gt; New Session id is : &#8220;.$newid;<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Session name is : PHPSESSID<\/p>\n<p>Session id is :<\/p>\n<p>New Session id is : abc123<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on COOKIE and SESSION<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/kkOGRQOZZUA\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>References:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional, Apress<\/p>\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass:<br \/>\nBeginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O&#8217;Reilly Media<\/p>\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove: Professional PHP,Wrox<\/p>\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide, Pearson Education<\/p>\n<p style=\"text-align: justify\">11. http:\/\/www.php.net\/<\/p>\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>\n","protected":false},"author":3,"menu_order":26,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-hiren-joshi"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-247","chapter","type-chapter","status-publish","hentry","contributor-dr-hiren-joshi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":10,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/247\/revisions"}],"predecessor-version":[{"id":510,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/247\/revisions\/510"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/247\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/media?parent=247"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapter-type?post=247"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/contributor?post=247"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/license?post=247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}