mysql - creating sessions and validation of data in php -


i created php script retrieving data table , storing data upon selection in table submitting form. have trouble in creating sessions storing values , validation of data. how can create session , validate data upon selection of items in drop down.

<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "leave-form";  $dbh = new pdo("mysql:host=$servername; dbname=$dbname", $username, $password);  $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception);  if (isset($_post["submit"])) {      try {          $sql = "insert authority (emp_name, repo_auth, contr_auth) values   ('" . $_post["emp_name"] . "','" . $_post["repo_auth"] . "','" . $_post["contr_auth"] . "')";         if ($dbh->query($sql)) {             echo "<script type= 'text/javascript'>alert('new record inserted   successfully');</script>";         } else {             echo "<script type= 'text/javascript'>alert('data not   inserted.');</script>";         }     } catch (pdoexception $e) {         echo $e->getmessage();     } } ?>  <div class="container">     <h1>admin</h1>     <form name="registration" action="" method="post">         <table>             <tr>                 <td>employee name:</td>                 <td>                     <select name="emp_name">                         <option value=""> -----------select----------- </option>                          <?php                         $stmt = $dbh->prepare('select name people');                         $stmt->execute();                         while ($row = $stmt->fetch(pdo::fetch_assoc)) {                             echo '<option>' . $row['name'] . '</option>';                         }                         ?>                     </select>                  </td>             </tr>              <tr>                 <td>manager:</td>                 <td>                     <select name="repo_auth">                         <option value=""> -----------select----------- </option>                          <?php                         $stmt = $dbh->prepare('select name people');                         $stmt->execute();                         while ($row = $stmt->fetch(pdo::fetch_assoc)) {                             echo '<option>' . $row['name'] . '</option>';                         }                         ?>                     </select>                  </td>             </tr>              <tr>                 <td>senior manager:</td>                 <td>                     <select name="contr_auth">                         <option value=""> -----------select----------- </option>                          <?php                         $stmt = $dbh->prepare('select name people');                         $stmt->execute();                         while ($row = $stmt->fetch(pdo::fetch_assoc)) {                             echo '<option>' . $row['name'] . '</option>';                         }                         ?>                     </select>                  </td>             </tr>             <tr>                 <td></td>                 <td>                     <input type="submit" name="submit" value="register" />                     <input type="reset" name="cancel" value="clear"/>                 </td>             </tr>         </table>     </form> </div> 

you can way :

sessions :

sessions important & easy main element of php.you can use session store values in , can access values across other pages of php script.unllke cookies sessions alot secure cookies cookies saved on client side , can manipulated can cause more harm else while on other sie sessions server side.you may store value in session actual session stored on server.you can't manipulate that's why it's more secure use sessions instead of cookies both have roles in use of each other @ places in code.!

how create session :

creating session easy, session can created way :

$_session['session_name'] = $value; 

2nd can access session in other php script pages way:

$stored_value = $_session['session_name']; echo $stored_value; 

note : remember run session_start() statement on both these pages before try access $_session array, , before output sent browser.simply add session_start() @ full begining of each , every page in going use session either setting session or if trying retrieve one.!

simple use of session :

scenario : instance want create session upon logging in , able visit other pages established session until session destroyed.! here how can :

on login page (index.php or login.php) :

<?php session_start();  // add session_start() right after php starting tags  // or getting header sent error  if ($db_username == $input_username && $db_password == $input_password) { $_session['user'] = $db_username; header("location : home.php"); } else { echo "username & password wrong"; } ?> 

and on home.php can access data can accessed if session there way :

on home page (home.php) :

<?php session_start(); $user =  $_session['user']; if (isset($user)) { echo "welcome user : ".$user; } else { // if session not there redirected login page.! header("location: login.php"); } ?> 

and if want logout can destroy session way visiting logout.php page :

logout page (logout.php) :

<?php session_start(); if (session_destroy()) {     header("location: login.php"); } ?> 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -