HN-Community
menyembunyikan file PHP dengan fungsi _GET - Printable Version

+- HN-Community (https://hacker-newbie.org)
+-- Forum: Miscellaneous (https://hacker-newbie.org/forum-86.html)
+--- Forum: Web Development (https://hacker-newbie.org/forum-87.html)
+---- Forum: Development (https://hacker-newbie.org/forum-89.html)
+---- Thread: menyembunyikan file PHP dengan fungsi _GET (/thread-18351.html)



menyembunyikan file PHP dengan fungsi _GET - cinta.damai - 11-01-2013

permisi om.. newbie mau ngeshare sedikit tentang menyembunyikan file PHP menggunakan fungsi _GET, untuk contoh misalnya kita punya file input_user.php didalam folder coba, maka pada localhost bearti kita harus menginputkan URL
localhost/coba/input_user.php bukan? gimana kalo kita buat localhost/coba/?page=User-Add sajaWatados

pada kasus ini saya buat:
nama database : test
nama tabel : user
atribut : - id int,primary key, auto increment
- username varchar(25)
- password varchar(33)
- akses varchar(6)

ane buat page insert,update,delete dan viewnya
buatlah sebuah koneksi ke database
koneksi.php
PHP Code:
<?php $host="localhost"; $db="test"; $user="root"; $pass=""; $pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pass, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )); ?>

lalu saya buat user_insert.php
PHP Code:
<!DOCTYPE HTML> <html> <head> <title>insert username</title> </head> <body> <?php require_once("koneksi.php");//memanggil koneksi yang sudah dibuat if ($_POST){ try{ $query=$pdo->prepare("insert into user (username,password,akses) values (:user,:pass,:akses)"); $datainput=array( ':user'=>$_POST['user'], ':pass'=>md5($_POST['password']), ':akses'=>$_POST['akses'] ); $query->execute($datainput); header("Location: ?page=User-View");//mengembalikan ke page user_view.php }catch(PDOException $e){ echo "Error! gagal menyimpan data user:".$e->getMessage(); } } ?> <table> <form method="post" action=""> <tr> <td>Username</td> <td><input type="text" name="user" /></td> </tr> <tr> <td>Password</td> <td><input type="password" name="password" /></td> </tr> <tr> <td>Akses</td> <td><input type="radio" name="akses" value="admin"/>Admin <input type="radio" name="akses" value="kasir"/>Kasir</td> </tr> <tr> <td></td> <td><input type="submit" name="Save" /></td> </tr> </form> </table> </body> </html>

user_edit.php
PHP Code:
<!DOCTYPE HTML> <html> <head> <title>View User</title> </head> <body> <?php require_once("koneksi.php"); $query=$pdo->prepare("select * from user where md5(id)=:id"); $query->execute(array( 'id'=>$_GET['id'] )); $data=$query->fetch(); if ($_POST){ if($data['password']==$_POST['password']){ $pass=$_POST['password']; }else{ $pass=md5($_POST['password']); } try{ $update=$pdo->prepare("update user set username=:user,password=:pass,akses=:akses where id=:id"); $dataupdate=array( ':user' => $_POST['user'], ':pass' => $pass, ':akses' => $_POST['akses'], ':id' => $_POST['id'] ); $update->execute($dataupdate); header("Location: ?page=User-View");//mengembalikan ke halaman user_view.php menggunakan GET exit; }catch(PDOException $e){ echo "Error! gagal menyimpan data user:".$e->getMessage(); } } ?> <table> <form method="post" action=""> <tr> <td>Username</td> <td><input type="text" name="user" value="<?php echo $data['username']; ?>"/></td> </tr> <tr> <td>Password</td> <td><input type="password" name="password" value="<?php echo $data['password']; ?>"/></td> </tr> <tr> <td>Akses</td> <td><input type="radio" name="akses" value="admin" <?php if($data['akses']=="admin"){ echo "checked";} ?>/>Admin <input type="radio" name="akses" value="kasir" <?php if($data['akses']=="kasir"){ echo "checked";} ?>/>Kasir</td> </tr> <tr> <td><input type="hidden" name="id" value="<?php echo $data['id']; ?>" /></td> <td><input type="submit" name="Update" /></td> </tr> </form> </table> </body> </html>

user_delete.php
PHP Code:
<?php require_once("koneksi.php"); if (isset($_GET['id'])){ try{ $query=$pdo->prepare("delete from user where md5(id)=:id"); $data=array( ':id' => $_GET['id'] ); $query->execute($data); header("Location: ?page=User-View");//mengembalikan ke halaman user_view.php exit; }catch(PDOException $e){ echo "Error! gagal menyimpan menghapus user:".$e->getMessage(); } } ?>

dan user_view.php
PHP Code:
<!DOCTYPE HTML> <html> <head> <title>View User</title> </head> <body> <a href="?page=User-Add">Tambah Data</a> <?php require_once("koneksi.php"); $query=$pdo->prepare("select * from user"); $query->execute(); ?> <table> <tr> <td>Username</td> <td>Password</td> <td>Akses</td> <td colspan="2">Action</td> </tr> <?php while ($data=$query->fetch()){?> <tr> <td><?php echo $data['username']; ?></td> <td><?php echo $data['password']; ?></td> <td><?php echo $data['akses']; ?></td> <td><a href="?page=User-Edit&id=<?php echo md5($data['id']); ?>">Edit</a> |</td> <td><a href="?page=User-Delete&id=<?php echo md5($data['id']); ?>">Delete</a></td> </tr> <?php } ?> </table> </body> </html>

ane rasa di atas gak perlu dijelasin lebih dalam karna itu cuma insert,update,delete,dan view saja.
anda belum bisa mengakses file tersebut karna perintah _GET belum "bertuan"Piktor lalu saya buat file php dengan nama index.php
PHP Code:
<!DOCTYPE HTML> <html> <head> <title>index</title> </head> <body> <p>contoh tampilan halaman index</p> <?php include "menu.php"; include "controller.php"; ?> </body> </html>

lalu saya buat file dengan nama main.php, fungsinya sendiri bila fungsi GET sedang tidak digunakan (mungkin bila dicontohkan sbg template ini adalah halaman awal)
main.php
PHP Code:
<!DOCTYPE HTML> <html> <head> <title>main</title> </head> <body> <p>contoh bila tidak memilih menu apapun</p> <p>tampilan pada file php dengan nama main.php akan hilang bila sudah memilih menu</p> </body> </html>

membuat menu.php
PHP Code:
<!DOCTYPE HTML> <html> <head> <title>main</title> </head> <body> <ul> <li><a href="?page=">Home</a></li> <li><a href="?page=User-Add">Tambah User</a></li> <li><a href="?page=User-View">View User</a></li> </ul> </body> </html>

dan bagian yang terakhir yang menurut ane paling penting.. untuk mengatur _GET yang kita miliki.
controller.php
PHP Code:
<?php if (isset($_GET['page'])){ switch ($_GET['page']){ case '' : if(!file_exists ("main.php")) die ("Sorry Empty Page!"); include "main.php"; break; case 'User-Add' : if(!file_exists ("user_insert.php")) die ("Sorry Empty Page!"); include "user_insert.php"; break; case 'User-Edit' : if(!file_exists ("user_edit.php")) die ("Sorry Empty Page!"); include "user_edit.php"; break; case 'User-Delete' : if(!file_exists ("user_delete.php")) die ("Sorry Empty Page!"); include "user_delete.php"; break; case 'User-View' : if(!file_exists ("user_view.php")) die ("Sorry Empty Page!"); include "user_view.php"; break; default: if(!file_exists ("main.php")) die ("Empty Main Page!"); include "main.php"; break; } } else{ if(!file_exists ("main.php")) die ("Empty Main Page!"); include "main.php"; } ?>

sekarang silahkan anda mengakses URL localhost/(folder php anda)/
bila sukses hasilnya begini
[Image: index.jpg]

[Image: insert.jpg]

sekian tutorial cupu saya.. walaupun gak sehebat URL rewriting punya om E1nzte1N tapi semoga berguna kalo lagi masang di template Mentok