Advertisement
Riju18

no.63_session variable(no.22)[useful for login]

Jul 24th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.89 KB | None | 0 0
  1. ### if a session variable is set once it can be accessible in all page. suppose in "the main" page we logged in with username and password. our username we can in all page along with "main" page until we log out.
  2.  
  3. #### here 2file is created. 1)session.php 2)contact.php
  4.  session.php
  5. ---------------
  6. ----------------
  7. <?php
  8.   session_start();
  9.  ?>
  10. <!DOCTYPE html>
  11. <html>
  12.   <head>
  13.     <meta charset="utf-8">
  14.     <title>session</title>
  15.     <link rel="stylesheet" href="/css/master.css">
  16.     <style media="screen">
  17.       *{margin: 0;padding: 0;}
  18.        body{background-color: gray;}
  19.       .header
  20.       {
  21.         width: 960px;
  22.         height: 50px;
  23.         background: green;
  24.         margin: 20px auto;
  25.         border: 2px solid white;
  26.         border-radius: 10px;
  27.       }
  28.       .header h2
  29.       {
  30.         color: white;
  31.         margin-left: 200px;
  32.         padding: 8px;
  33.         font-family: arial;
  34.       }
  35.       .main
  36.       {
  37.         width: 960px;
  38.         height: 450px;
  39.         background: green;
  40.         margin: 10px auto;
  41.         border: 1px solid white;
  42.         border-radius: 20px;
  43.       }
  44.       .main .code
  45.       {
  46.         margin: 10px 20px;
  47.         color: white;
  48.         font-weight: bold;
  49.         font-size: 20px;
  50.         font-family: verdana;
  51.       }
  52.       .footer
  53.       {
  54.         width: 960px;
  55.         height: 50px;
  56.         background: green;
  57.         margin: 10px auto;
  58.         border: 2px solid white;
  59.         border-radius: 10px;
  60.       }
  61.       .footer h2
  62.       {
  63.         color: white;
  64.         margin-left: 250px;
  65.         padding: 8px;
  66.         font-family: arial;
  67.       }
  68.     </style>
  69.   </head>
  70.   <body>
  71.     <div class="header">
  72.       <h2>How session variable works:</h2>
  73.     </div>
  74.     <div class="main">
  75.       <div class="code">
  76.         <p>Basic of session:</p>
  77.         <br>
  78.         <p>put username and passord to log in  access content page:</p><br>
  79.         goto <a href="contact.php" target="_blank">content</a>  page
  80.         <br><br>
  81.         <form class="" action="" method="post">
  82.           <table>
  83.             <tr>
  84.               <td>username:</td>
  85.               <td><input type="text" name="user" value="" autofocus></td>
  86.             </tr>
  87.             <tr>
  88.               <td>Password:</td>
  89.               <td><input type="password" name="pass" value="" autofocus></td>
  90.             </tr>
  91.             <tr>
  92.               <td></td>
  93.               <td><input type="submit" name="prcoeed" value="prceed"></td>
  94.             </tr>
  95.           </table>
  96.         </form>
  97.         <?php
  98.           if(isset($_POST['prcoeed']))
  99.           {
  100.             $name = $_POST['user'];
  101.             $pass = $_POST['pass'];
  102.             $_SESSION['USERNAME'] = $name;
  103.             echo $_SESSION['USERNAME'];
  104.             echo "<br>";
  105.             echo "session variable is set";
  106.           }
  107.          ?>
  108.       </div>
  109.     </div>
  110.     <div class="footer">
  111.       <h2>&copy; the hompage is created by <i>Riju</i></h2>
  112.     </div>
  113.   </body>
  114. </html>
  115.  
  116.  
  117. contact.php
  118. ----------------
  119. ------------------
  120. <?php
  121.   session_start();
  122.  ?>
  123. <!DOCTYPE html>
  124. <html>
  125.   <head>
  126.     <meta charset="utf-8">
  127.     <title>session</title>
  128.     <link rel="stylesheet" href="/css/master.css">
  129.     <style media="screen">
  130.       *{margin: 0;padding: 0;}
  131.        body{background-color: gray;}
  132.       .header
  133.       {
  134.         width: 960px;
  135.         height: 50px;
  136.         background: green;
  137.         margin: 20px auto;
  138.         border: 2px solid white;
  139.         border-radius: 10px;
  140.       }
  141.       .header h2
  142.       {
  143.         color: white;
  144.         margin-left: 200px;
  145.         padding: 8px;
  146.         font-family: arial;
  147.       }
  148.       .main
  149.       {
  150.         width: 960px;
  151.         height: 450px;
  152.         background: green;
  153.         margin: 10px auto;
  154.         border: 1px solid white;
  155.         border-radius: 20px;
  156.       }
  157.       .main .code
  158.       {
  159.         margin: 10px 20px;
  160.         color: white;
  161.         font-weight: bold;
  162.         font-size: 20px;
  163.         font-family: verdana;
  164.       }
  165.       .footer
  166.       {
  167.         width: 960px;
  168.         height: 50px;
  169.         background: green;
  170.         margin: 10px auto;
  171.         border: 2px solid white;
  172.         border-radius: 10px;
  173.       }
  174.       .footer h2
  175.       {
  176.         color: white;
  177.         margin-left: 250px;
  178.         padding: 8px;
  179.         font-family: arial;
  180.       }
  181.     </style>
  182.   </head>
  183.   <body>
  184.     <div class="header">
  185.       <h2>How session variable works:</h2>
  186.     </div>
  187.     <div class="main">
  188.       <div class="code">
  189.         <p>Basic of session:</p>
  190.         <br>
  191.         <p>contact page:</p><br>
  192.         goto <a href="session.php" target="_blank">session</a>  page
  193.         <br><br>
  194.         <?php
  195.             echo $_SESSION['USERNAME']."(session variable which is set in session page)";
  196.          ?>
  197.       </div>
  198.     </div>
  199.     <div class="footer">
  200.       <h2>&copy; the hompage is created by <i>Riju</i></h2>
  201.     </div>
  202.   </body>
  203. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement