Download Tutorial PHP Lengkap 17 Bab [pdf]
4 Komentar
CREATE TABLE IF NOT EXISTS `user` (
`id_user` int(11) NOT NULL,
`nama` varchar(20) NOT NULL,
`username` varchar(20) NOT NULL,
`password` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
<?php
// memulai pencatatan session
session_start();
// koneksi database
mysql_connect('localhost','root','');
mysql_select_db('myreport');
// yang akan dijadikan session adalah id_user
if(isset($_SESSION['id_user'])) {
$id_user = $_SESSION['id_user'];
} else {
$id_user = 0;
}
?>
<?php
// ambil konfigurasi database dan session
include('config.php');
// cek apakah user sudah login atau belum
// jika sudah login nilainya bukan 0 tetapi id_user
// direct ke halaman index.php
if($id_user<>0) { header('location:index.php'); }
// jika tombol register diklik
if(isset($_POST['btnregister'])) {
$txtnama = $_POST['txtnama'];
$txtuser = $_POST['txtuser'];
// password di enkripsi dengan password_hash()
$txtpass = password_hash($_POST['txtpass'],PASSWORD_BCRYPT);
$user = mysql_query("SELECT * FROM user WHERE username='$txtuser'");
// apakah username yang dimasukan sudah ada dalam database ?
$cekuser = mysql_num_rows($user);
if($cekuser>0) {
// jika sudah ada username yang sama maka register ditolak
echo "<script>alert('Username ini sudah terdaftar!');</script>";
} else {
// jika belum ada username yang sama maka register diterima
// kemudian masukan kedalam database
$insert = mysql_query("INSERT INTO user (nama,username,password) VALUES ('$txtnama','$txtuser','$txtpass')");
if($insert) {
echo "<script>alert('Daftar berhasil! Silahkan login.');</script>";
} else {
echo "<script>alert('Daftar gagal! Silahkan ulangi.');</script>";
}
}
}
echo "
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h3>REGISTER</h3>
<form action='' method='post'>
<label>Nama</label><br/>
<input type='text' name='txtnama' required='true'/><br/>
<label>Username</label><br/>
<input type='text' name='txtuser' required='true'/><br/>
<label>Password</label><br/>
<input type='password' name='txtpass' required='true'/><br/>
<br/>
<button type='submit' name='btnregister'>Register</button><br/>
</form>
<br/>
<a href='login.php'>Login</a>
</body>
</html>
";
?>
<?php
// ambil konfigurasi database dan session
include('config.php');
// cek apakah user sudah login atau belum
// jika sudah login nilainya bukan 0 tetapi id_user
// direct ke halaman index.php
if($id_user<>0) { header('location:index.php'); }
// jika tombol login diklik
if(isset($_POST['btnlogin'])) {
$txtuser = $_POST['txtuser'];
$txtpass = $_POST['txtpass'];
$user = mysql_query("SELECT * FROM user WHERE username='$txtuser'");
// apakah username yang dimasukan sudah ada dalam database ?
$cekuser = mysql_num_rows($user);
if($cekuser==0) {
// jika username belum ada dalam database
echo "<script>alert('Anda belum terdaftar!');</script>";
} else {
// jika username sudah ada dalam database
$getuser = mysql_fetch_array($user);
// menyocokan password yang di input dengan password di database
if(!password_verify($txtpass,$getuser['password'])) {
// jika password tidak cocok
echo "<script>alert('Password salah!');</script>";
} else {
// jika password cocok
// id_user dijadikan session
$_SESSION['id_user'] = $getuser['id_user'];
// direct ke halaman index.php
header('location:index.php');
}
}
}
echo "
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h3>LOGIN</h3>
<form action='' method='post'>
<label>Username</label><br/>
<input type='text' name='txtuser' required='true'/><br/>
<label>Password</label><br/>
<input type='password' name='txtpass' required='true'/><br/>
<br/>
<button type='submit' name='btnlogin'>Login</button><br/>
</form>
<br/>
<a href='register.php'>Register</a>
</body>
</html>
";
?>
<?phpPada kode diatas menampilkan nama dan username dari user yang telah login.
// ambil konfigurasi database dan session
include('config.php');
// cek apakah user sudah login atau belum
// jika belum login nilainya adalah 0
// direct ke halaman login.php
if($id_user==0) { header('location:login.php'); }
// ambil data user sesuai id_user dari session
$user = mysql_fetch_array(mysql_query("SELECT * FROM user WHERE id_user='$id_user'"));
echo "
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h3>HOME</h3>
Anda login sebagai <b>$user[nama]</b> dengan username <b>$user[username]</b>.<br/>
<br/>
<a href='logout.php'>Logout</a>
</body>
</html>
";
?>
<?phpYang berwarna merah adalah fungsi untuk menghapus session id_user tadi.
// ambil konfigurasi database dan session
include('config.php');
// hapus session saat ini
unset($_SESSION['id_user']);
// direct ke halaman login.php
header('location:login.php');
?>
<html>
<head>
<title>BOM SMS with GAMMU by siunus</title>
<style>
body, table {
font-family: Helvetica,Arial,'lucida grande',tahoma,verdana,arial,sans-serif;
font-size: small;
}
table {
border: 1px solid #090;
}
table th {
background: #090;
color: white;
}
td#td1 {
border-top: 1px solid #090;
}
input, textarea {
border: 1px solid #069;
background: #39F;
}
</style>
</head>
<body>
<form action='' method='post'>
<table border='0' align='center'>
<tr>
<th colspan='3' align='center' valign='top'>BOM SMS</td>
</tr>
<tr>
<td align='right' valign='top'>No Tujuan</td>
<td align='right' valign='top'>:</td>
<td align='left' valign='top'><input type='text' name='nomor' value='+62' size='26'/></td>
</tr>
<tr>
<td align='right' valign='top'>Pesan</td>
<td align='right' valign='top'>:</td>
<td align='left' valign='top'><textarea name='pesan'></textarea></td>
</tr>
<tr>
<td align='right' valign='top'>Jumlah Pesan</td>
<td align='right' valign='top'>:</td>
<td align='left' valign='top'>
<input type='text' name='loop' value='100' size='4'/>
<input type='submit' name='kirim' value='KIRIM'/>
<input type='reset' name='reset' value='RESET'/>
</td>
</tr>
</form>
<?php
// koneksi database
$host = "localhost";
$user = "root";
$pass = "";
$db = "sms";
$konek = mysql_connect($host,$user,$pass);
if(!$konek) {
echo "<tr><td id='td1' colspan='3' align='center'>Koneksi mysql gagal.</td></tr>";
} else {
$dbase = mysql_select_db($db);
if(!$dbase) {
echo "<tr><td id='td1' colspan='3' align='center'>Database tidak ditemukan.</td></tr>";
} else {
if(isset($_POST['kirim'])) {
$nomor = $_POST['nomor'];
$pesan = $_POST['pesan'];
$loop = $_POST['loop'];
if(empty($nomor) OR empty($pesan)) {
echo "<tr><td id='td1' colspan='3' align='center'>Isi semua kolom.</td></tr>";
} else {
if($loop>1000) {
echo "<tr><td id='td1' colspan='3' align='center'>Maksimal 1000 pesan /1x kirim.</td></tr>";
} else {
for ($i=1; $i<=$loop; $i++) {
echo "<tr><td id='td1' colspan='3' align='center'>";
echo "$nomor - ($i/$loop) $pesan ... ";
$kirim = mysql_query("INSERT INTO outbox(DestinationNumber, TextDecoded, CreatorID) VALUES('$nomor', '$pesan', 'siunus $i/$loop')");
if($kirim) {
echo "OK <br/>";
} else {
echo "NO <br/>";
}
echo "</td></tr>";
echo "<tr><td id='td1' colspan='3' align='center'>";
if($i==$loop) {
echo "Semua pesan telah dikirim.";
}
echo "</td></tr>";
}
}
}
}
}
}
?>
</table>
</body>
</html>
*Angka berwarna merah adalah jumlah kata-kata yang akan ditampilkan.<?php
$x = rand(1,7);
$kata = array(
'Kata-kata pertama.',
'Kata-kata kedua.',
'Kata-kata ketiga.',
'Kata-kata keempat.',
'Kata-kata kelima.',
'Kata-kata keenam.',
'Kata-kata ketujuh.'
);
echo $kata[$x];
?>
<?php*Angka berwarna merah adalah jumlah kata-kata yang akan ditampilkan.
$x = rand(1,7);
$kata[1] = "Kata-kata pertama.";
$kata[2] = "Kata-kata kedua.";
$kata[3] = "Kata-kata ketiga.";
$kata[4] = "Kata-kata keempat.";
$kata[5] = "Kata-kata kelima.";
$kata[6] = "Kata-kata keenam.";
$kata[7] = "Kata-kata ketujuh.";
echo $kata[$x];
?>
<html>
<head>
<title>Simple Guestbook by siunus</title>
<link rel='stylesheet' href='style.css'/>
<style>body { max-width: 500px; margin: auto; }</style>
</head>
<body>
<div class='title' align='center'>
<big>Simple Guestbook</big><br/>
by: <a href='http://www.siunus.co.vu'>siunus</a>
</div>
<div class='gmenu'>
<form action='post.php' method='post'>
Nama : <input type='text' name='nama'/> <br/>
Web : <input type='text' name='web' value='http://'/> <br/>
Pesan : <input type='text' name='pesan'/> <br/>
<input type='submit' value='kirim'/>
</form>
</div>
<div class='maintxt'>
<?php
if(file_exists("db.html") && filesize("db.html") > 0){
$handle = fopen("db.html", "r");
$contents = fread($handle, filesize("db.html"));
fclose($handle);
echo "$contents";
}
else {
echo "<div class='alarm'>no messages!<br/>be the first!</div>";
}
?>
</div>
<div class='footer' align='center'>
Copyright © <a href='http://www.siunus.co.vu'>siunus</a> 2013
</div>
</body>
</html>
<?php
if(!empty($_POST['nama']) AND !empty($_POST['pesan'])) {
$nama = $_POST['nama'];
if($_POST['web'] != 'http://' OR !empty($_POST['web'])) { $web = $_POST['web']; } else { $web = "#"; }
$pesan = $_POST['pesan'];
$tgl = date('D, d M Y - H:i:s');
$text = stripslashes(htmlspecialchars($pesan));
$l = rand(1,2);
$fp = fopen("db.html", 'a');
fwrite($fp, "<div class='list$l'><div class='hr'><div align='right'>$tgl</div></div><img src='say.png'/> <a href='$web'><b>$nama</b></a> : $text </div>");
fclose($fp);
}
header('location:index.php');
?>
Biarkan file ini kosong. Ini untuk menyimpan data nantinya.
body {
color: #787878;
font-size: 11px;
font-family: Tahoma;
margin: auto;
border: 1px solid #e1e1e1;
max-width: 500px;
}
a, a:link, a:visited {
color: #00bae8;
text-decoration: none;
}
input, textarea, select , button{
color: #6d6d6d;
background-color: #fbfbfb;
border: 1px solid #e3e3e3;
margin: 1px;
}
hr, div.hr {
margin-top: 2px;
margin-bottom: 2px;
border-bottom: 1px dotted #e3e3e3;
border-right-style: none;
border-right-width: 0;
border-top-style: none;
border-top-width: 0;
border-left-style: none;
border-left-width: 0;
}
.title {
text-align: center;
text-transform: uppercase;
font-weight: bold;
color: #ffffff;
text-shadow: #000000 1px 1px 1px;
background-color: #84e2f8;
margin-top: 1px;
margin-bottom: 1px;
padding: 2px;
border: 1px solid #68e1ff;
}
.gmenu {
color: #75bf00;
background-color: #fafff0;
margin-top: 1px;
margin-bottom: 1px;
padding: 2px;
border: 1px solid #d4eba1;
}
.gmenu a {
color: #6db900;
border-bottom: 1px dotted#cfe798;
}
.footer{
color: #ffffff;
text-shadow: #45cdf5 1px 1px 1px;
margin-top: 1px;
margin-bottom: 1px;
padding: 2px;
border: 1px solid #68e1ff;
background-color: #84e2f8;
}
.footer a {
color: #fe8f17;
text-decoration: none;
border-bottom: 1px dotted #979797;
}
.list1 {
background-color: #f6f6f6;
margin-top: 1px;
margin-bottom: 1px;
padding: 2px;
border: 1px solid #e7e7e7;
}
.list2 {
background-color: #fdfdfd;
margin-top: 1px;
margin-bottom: 1px;
padding: 2px;
border: 1px solid #ececec;
}
.maintxt {
padding-right: 1px;
padding-left: 1px;
border: 1px solid #dadada;
}
.alarm {
color: #fd4646;
background-color: #fff0f0;
margin-top: 1px;
margin-bottom: 1px;
padding: 2px;
border: 1px solid #ffd0d0;
}