[trick] login multi user dengan PHP berorientasi object dan MYSQL

BIsmillah … langsung saja >>

siapkan databases :

d

 

 

masuk ke code PHP :

class.php


<?php

class database{

private $dbHost="localhost";
private $dbUser="root";
private $dbPass="";
private $dbName="oop";

function koneksi(){
mysql_connect($this->dbHost,$this->dbUser,$this->dbPass);
mysql_select_db($this->dbName);
}

function tambahData($id,$nama,$email,$pesan){
$query="insert into table_oop(id,nama,email,pesan)values('$id','$nama','$email','$pesan')";
$hasil=mysql_query($query);
if($hasil){
echo"<meta http-equiv='refresh' content='0; url=lat1.php'>";
}
}

function tampilData(){
$sql=mysql_query("select * from table_oop");
while($rows=mysql_fetch_array($sql))
$data[]=$rows;
return $data;
}

function bacaData($field,$id){
$query=mysql_query("select * from table_oop where id='$id'");
$data=mysql_fetch_array($query);
if($field == 'id'){
return $data['id'];
}elseif($field == 'nama'){
return $data['nama'];
}elseif($field == 'email'){
return $data['email'];
}elseif($field == 'pesan'){
return $data['pesan'];
}
}

function updateData($id,$nama,$email,$pesan){
$query=mysql_query("update table_oop set id='$id',nama='$nama',email='$email',pesan='$pesan' where id='$id'");
echo"<meta http-equiv='refresh' content='0; url=lat1.php'>";
}

function hapusData($id){
$query=mysql_query("delete from table_oop where id='$id'");
if($query){
echo"<meta http-equiv='refresh' content='0; url=lat1.php'>";
}
}
}

class user{
function cek_login($user,$password){
$password=md5($password);
$sql=mysql_query("select * from user where nama='$user' and password='$password'");
$data=mysql_fetch_array($sql);
$cek=mysql_num_rows($sql);
if($cek > 0){
session_register('id');
session_register('nama');
$_SESSION['id']=true;
$_SESSION['id']=$data['id'];
$_SESSION['nama']=$data['nama'];
return true;
}else{
return false;
}

}

function get_sesi(){
return $_SESSION['id'];
}

function logout(){
$_SESSION['id']=false;
session_destroy();
}

}
?>

2. utama.php/index.php


<?php
session_start();
include"class.php";

$user = new user() ;
$db = new database() ;

$db->koneksi();

if($user->get_sesi()){
header("location:home.php");
}

if($_SERVER['REQUEST_METHOD']=="POST"){
$login=$user->cek_login($_POST['nama'],$_POST['password']);
if($login){
header('location:home.php');
}else{
echo"Login Gagal";
}
}
?>

<form method="POST">
Nama : <input type=text name='nama'><br>
Password : <input type=password name='password'><br>
<input type=submit value='Login'>
</form>

3.home.php


<?php
session_start();
include"class.php";

$user= new user();
$db = new database();
$db->koneksi();

$id_user=$_SESSION['id'];
if(!$user->get_sesi()){
header('location:utama.php');
}if($_GET['menu']=='logout'){
$user->logout();
header('location:utama.php');
}

?>

<table border=1 width=780 align=center>
<tr><td height=150 colspan=2></td></tr>
<tr><td colspan=2 height=50 align=right>Selamat Datang <?php echo" $id_user | <a href='?menu=logout'>logout</a> <br> "; ?></td></tr>
<tr><td width=130 height=500 valign=top><?php include"menu.php";?></td><td valign=top><?php include"lat2.php";?></td></tr>
<tr><td colspan=2 height=30></td></tr>
</table>

4.menu.php


<?php
if(!$user->get_sesi()){
header('location:utama.php');
}elseif($user->get_sesi()==1){
echo"<a href='?menu=home'>home</a><br>
 <a href='?menu=profile'>profile</a><br>
 <a href='?menu=layanan'>layanan</a> <br>";
}elseif($user->get_sesi()==2){
echo"<a href='?menu=home'>home</a><br>
 <a href='?menu=profile'>profile</a><br>
 <a href='?menu=layanan'>layanan</a> <br>
 <a href='?menu=user'>user</a> <br>";

}
?>

 

untuk tampilan kira-kira seperti ini >>

r

 

 

untuk code silahkan download aja disini

Leave a comment

6 Comments

  1. terima kasih om udah mau share…🙂
    beginilah indahnya berbagi…

    Reply
  2. Bang Yas

     /  14/12/2014

    error gan😀

    Reply
  3. inz

     /  16/02/2015

    Notice: Undefined index: id in \oop_login\oop\class.php on line 79

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: