Advertisement
cdsatrian

Latihan session

Sep 23rd, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.83 KB | None | 0 0
  1. <?php
  2. session_start();
  3. /*********************************
  4. FILENAME   : session.php
  5. CREATE BY  : CAHYA DSN
  6. CREATE DATE: 2013-09-23
  7. NOTES      : latihan session
  8. **********************************
  9. USE test;
  10.  
  11. DROP TABLE IF EXISTS `tbl_users`;
  12. CREATE TABLE IF NOT EXISTS `tbl_users` (
  13.   `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  14.   `username` VARCHAR(20)NOT NULL,
  15.   `password` VARCHAR(40) NOT NULL,
  16.   `nama` VARCHAR(25) NOT NULL,
  17.   `alamat` VARCHAR(100) NOT NULL,
  18.   `level` TINYINT(2) UNSIGNED NOT NULL DEFAULT '2',
  19.   PRIMARY KEY (`id`)
  20. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
  21.  
  22. INSERT INTO tbl_users(username,password,nama,alamat,level)
  23. VALUES
  24. ('admin',SHA1('password'),'administrator','kota',1),
  25. ('agus',SHA1('password'),'agus sujatmiko','desa',2),
  26. ('budi',SHA1('password'),'budi setianto','kampung',2),
  27. ('carlie',SHA1('password'),'carlie','nagari',2);
  28. */
  29. //--- DATABASE CONFIGURATION
  30. $dbhost='localhost';
  31. $dbuser='root';
  32. $dbpass='';
  33. $dbname='test';
  34. //--- DATABASE CONNNECTION (MYSQLI)
  35. $db=new mysqli ($dbhost,$dbuser,$dbpass,$dbname);
  36. if(isset($_GET['logout']))
  37. {
  38.   session_destroy();
  39.   header('location:session.php');
  40. }
  41. elseif(!isset($_SESSION['level']))
  42. {
  43.   ?>
  44. <!DOCTYPE html>
  45. <html>
  46.   <head><title>Login User</title></head>
  47.   <body>
  48.     <form method="POST">
  49.       <table>
  50.         <tr><td>Username</td><td><input type="text" name="username"/></td></tr>
  51.         <tr><td>Password</td><td><input type="password" name="password"/></td></tr>
  52.         <tr><td colspan="2"><input type="submit" value="login" /></td></tr>
  53.       </table>
  54.     </form>
  55.   </body>
  56. </html>  
  57.   <?php
  58. }
  59. elseif(isset($_POST['login']))
  60. {
  61.   $sql="SELECT id,level "
  62.       ."FROM tbl_users "
  63.       ."WHERE username='".$_POST['username']."' "
  64.       ."AND password=SHA1('".$_POST['password']."')";
  65.   if($result=$db->query($sql))
  66.   {
  67.     if($result->num_rows==1)
  68.     {
  69.       $row=$result->fetch_object();
  70.       $_SESSION['id']=$row->id;
  71.       $_SESSION['level']=$row->level;
  72.       header('location:session.php');
  73.     }
  74.   }
  75. }
  76. elseif(isset($_POST['save']))
  77. {
  78.   $sql="UPDATE tbl_users SET nama='".$_POST['nama']."',alamat='".$_POST['alamat']."' WHERE id='".$_SESSION['id']."'";
  79.   if($result=$db->query($sql)){
  80.     header('location:session.php');
  81.   }
  82. }
  83. elseif($_SESSION['level']==1)
  84. {
  85.   $sql="SELECT id,username,nama,alamat,level FROM tbl_users";
  86.   $data=array();
  87.   if($result=$db->query($sql))
  88.   {
  89.     while($record=$result->fetch_object())
  90.     {
  91.       $data[]=$record;
  92.     }
  93.     $result->close();
  94.   }
  95.   ?>
  96. <!DOCTYPE html>
  97. <html>
  98.   <head><title>list User</title></head>
  99.   <body>
  100.     <table>
  101.       <tr>
  102.         <th>No</th><th>username</th><th>Nama</th><th>Alamat</th><th>Level</th>
  103.       </tr>
  104. <?php
  105. $i=0;
  106. foreach($data as $dt)
  107. {
  108.   echo "<tr>
  109.        <td>".++$i."</td>
  110.        <td>".$dt->username."</td>
  111.        <td>".$dt->nama."</td>
  112.        <td>".$dt->alamat."</td>
  113.        <td>".($dt->level==1?'Admin':'User')."</td>
  114.      </tr>\n";
  115. }
  116. ?>      
  117.     </table>
  118.     <a href='session.php?logout=1'>logout</a>
  119.   </body>
  120. </html>  
  121.   <?php
  122. }
  123. elseif($_SESSION['level']==2)
  124. {
  125.   $sql="SELECT id,nama,alamat FROM tbl_users WHERE id=".$_SESSION['id'];
  126.   if($result=$db->query($sql))
  127.   {
  128.     $record=$result->fetch_object();
  129.     $data=array($record->id,$record->nama,$record->alamat);
  130.     $result->close();
  131.   }
  132.   ?>
  133. <!DOCTYPE html>
  134. <html>
  135.   <head><title>Edit User</title></head>
  136.   <body>
  137.     <form method="POST">
  138.       <table>
  139.         <tr><td>Nama</td><td><input type="text" name="nama" value="<?php echo $data[1];?>" /></td></tr>
  140.         <tr><td>Alamat</td><td><input type="text" name="nama" value="<?php echo $data[2];?>" /></td></tr>
  141.         <tr><td colspan="2"><input type="submit" value="save" /></td></tr>
  142.       </table>
  143.     </form>
  144.     <a href='session.php?logout=1'>logout</a>
  145.   </body>
  146. </html>  
  147.   <?php
  148. }
  149. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement