abc

Share This blog with your friends, so that we can improve more & more . our aim is to easy & simple way of learning.

5/22/2018

PHP session, cookie creation & use .



Question : - How create define session variable & what is cookies how to set cookies.
Session are temporary storage variable, by using session you can get variable value in another file .
Session_start();
Session[‘ab’] = ‘hello’;
Session_close();

PHP session is easily started by call it session_start() function. this function first checks if a session is already started and if none is started then it starts one. It is recommended to put the call to session_start() at the beginning of the page.
Session variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session.
<?php
   session_start();
   if( isset( $_SESSION['ab'] ) ) {
      $_SESSION['ab'] -= 1;
   }else {
      $_SESSION['ab'] = 1;
   }
       
   $msg = "value of ab is ".  $_SESSION['ab'];
   $msg .= "in this session.";
?>

  <html>
   <head>
      <title>define session</title>
   </head>
    <body>
      <?php  echo ( $msg ); ?>
   </body>
   </html>
Similarly use session_destroy() destroy current session.
& session_close() use to close started session.
Let us see cookies , as similar to session cookies are text file to store user information.
An alert or popup message most of site are uses, eg : we are using your cookies to store information for better performance.
<?php
   setcookie("name", "mayor abc", time()+1600, "/","", 0);
   setcookie("age", "34", time()+1600, "/", "",  0);
?>
   <html>
   <head>
   <title>how to define Cookies with PHP</title>
   </head>
   <body>
      <?php echo "hi setting cookies"?>
   </body>
   </html>
In next session we see more detail.
Thanks for viewing blog send comment in case any query.

1 comment:

An Introduction to the Laravel Framework: What It Is and Why You Should Use It

  If you're a PHP developer looking for a modern, efficient, and powerful framework to build web applications, look no further than Lara...