
Merhabalar, Bugün sizlere nasıl LOGİN Sayfası yapabilirsiniz.? Bu durumu anlatan bir makale sizlere paylaşacağım. Öncelikle veri tabanı işlemlerimizi halletmemiz gerekiyor. Bunun için de “loginpdo” adında bir veri tabanı oluşturuyoruz. Oluşturmuş olduğumuz veri tabanına “kullanicilar” adında yeni bir tablo oluşturuyoruz. Bu kısımda “id, kullanici ve parola” adında değişkenleri ekliyoruz. Hemen aşağıdaki gibi tabloyu oluşturun.

Daha sonra oluşturmuş olduğumuz tabloya verileri eklememiz gerekir.

Şimdi ise kullanıcı ve parola ile giriş yapacağımız sayfayı yapalım. Öncelikle kullanıcı ve parola veri tabanı üzerinden kontrollerini sağlıyoruz. Daha sonra parolayı md5 formatı ile yapılıp yapılmadığını kontrol ediyoruz. Eğer kontroller gerçekleşiyor ise kullanıcının girişi sağlanır. Böylelikle Ana Sayfaya yönlendirilir. SESSİON yani oturum oluşturmadan giriş sağlanmaz.
Login Giriş Sayfası

login.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login Sayfası</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/all.min.css">
<script src="js/jquery-3.4.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/login.js"></script>
<style>
body {background-color:#343a40;}
</style>
</head>
<body>
<?php
session_start(); //We started the session
include("fonc.php"); //we included the database
//if there is Session available we redirect the page.
if (isset($_SESSION["Session"]) && $_SESSION["Session"] == "9876") {
header("location:index.php");
} //If "remember me" is checked beforehand, we create Session and redirect the page.
else if (isset($_COOKIE["cookie"])) {
//Usernames are queried
$query = $connect->prepare("select kullanici from kullanicilar");
$query->execute();
//We get the usernames one by one with the help of loop
while ($result = $query->fetch()) {
//If there is a user suitable for the structure we have determined, we look at it.
if ($_COOKIE["cookie"] == md5("aa" . $result['kullanici'] . "bb")) {
//Session creation, you can make the values here different in terms of security. I also kept the username here
$_SESSION["Session"] = "9876";
$_SESSION["kullanici"] = $result['kullanici'];
//Redirecting to index pagem
header("location:index.php");
}
}
}
//We check if the login form has been filled
if ($_POST) {
$txtuser = $_POST["txtuser"]; //We assigned the username to the variable
$txtpassword = $_POST["txtpassword"]; //we assigned the password to the variable
}
?>
<div class="container py-5">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-6 mx-auto">
<!-- form card login -->
<div class="card rounded-0" id="login-form">
<div class="card-header">
<h3 class="mb-0">Kullanıcı Giriş Sayfası</h3>
</div>
<div class="card-body">
<form class="form" action="" method="POST">
<div class="form-group">
<label for="uname1">Kullanıcı Adı</label>
<input type="text" value="<?php echo @$txtuser ?>" class="form-control form-control-lg rounded-0" name="txtuser" id="inputuser" required name="txtpassword">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control form-control-lg rounded-0" id="inputPassword" required name="txtpassword">
</div>
<label>
<input type="checkbox" ID="cbkremember" name="cbkremember"/>
Beni Hatırla
</label>
<br>
<button type="submit" class="btn btn-warning btn-lg float-right" ID="btnlogin">Giriş Yap</button>
<script type="text/javascript" src="js/sweetalert.min.js"></script>
<?php
//If there is a post, that is, if it is submitted, we control it from the database.
if ($_POST) {
//In the query, we take the username and see if there is a corresponding password.
$query = $connect->prepare("select parola from kullanicilar where kullanici=:kullanici");
$query->execute(array('kullanici' => htmlspecialchars($txtuser)));
$result = $query->fetch();//executing query and getting data
//I encrypted the passwords with md5 and added my own to the beginning and the end.
if (md5("56" . $txtpassword . "23") == $result["parola"]) {
$_SESSION["Session"] = "9876"; //Creating a session
$_SESSION["kullanici"] = $txtuser;
//If "remember me" is selected, we create a cookie.
// I created it from the username by encrypting it in the cookie
if (isset($_POST["cbkremember"])) {
setcookie("cookie", md5("aa" . $txtuser . "bb"), time() + (60 * 60 * 24 * 7));
}
echo '<script>swal("Başarılı","Girişiniz Yapıldı.","success").then((value)=>{ window.location.href = "index.php"}); </script>';
//If adding data is successful, it is written that it is successful with sweetalert.
// If the Add query worked it redirects to index.php page
} else {
//If the username and password are not entered correctly, we get an error message.
echo '<script>swal("Oops","Hata, Lütfen Giriş Bilgilerinizi Kontrol Edin","error");</script>';
// If the id is not found or there is an error in the query, we print an error.
}
}
?>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
PHPEğer böyle bir oturum var ise ve kullanıcı adı ve parola ile ADMİN Paneline giriş yapılır. Bu sayede kullanıcı Login (Giriş) sayfasını gerçekleştirmiş olacak. Giriş yapınca kullanıcı bu sayfada CRUD işlemlerini gerçekleştirmiş olacak.

index.php
<?php
//When you open the project, we first provide redirection to the login page.
session_start(); //We started the session
//if there is Session available we redirect the page.
if (!(isset($_SESSION["Session"]) && $_SESSION["Session"] == "9876")) {
header("location:login.php");
} //If remember me is checked beforehand, we create Session and direct the page.
?>
<!DOCTYPE html>
<html>
<head>
<title>LOGİN PDO MD5</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/all.min.css">
<html lang="en">
</head>
<body>
<div class="container">
<div class="col-md-6">
<div class="text-center">
<h3>LOGİN PDO MD5</h3>
<a class="btn btn-danger" href="cıkıs.php">Çıkış Yap</a>
</div>
<br/>
</div>
<div class="col-md-6">
<div class="card">
<a class="btn btn-success" href="ekle.php">Yeni Kullanıcı Ekle</a>
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped table-bordered first">
<thead>
<th>ID</th>
<th>Kullanıcı Adı</th>
<th>Düzenle</th>
<th>Sil</th>
</thead>
<tbody>
<?php
include('fonc.php');
$query = $connect->prepare("SELECT * from kullanicilar"); // We Wrote Our Query
$query->execute();
while ($result = $query->fetch()) { // Returning Our Query with While
?>
<tr>
<td><?=$result['id']?></td>
<td><?=$result['kullanici']?></td>
<td><a href="duzenle.php?id=<?= $result["id"] ?>"><img height="25" width="25" src="img/edit.png"/></a></td>
<td>
</a>
<a data-toggle="modal" href="#" data-target="#sil<?php echo $result["id"] ?>">
<img height="25" width="25" src="img/delete.png"/></a>
<!-- Modal -->
<div class="modal fade" id="sil<?php echo $result["id"] ?>" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Kullanıcı Silme Aracı</h4>
</div>
<div class="modal-body">
<h2 style="color: red; text-align: center">Önemli Uyarı !</h2>
<h4 style="text-align: center">
Silmek İstediğinizden Emin Misiniz ?<br><b><?php echo $result["kullanici"] ?><br>
</h4>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-left" data-dismiss="modal">
İptal
</button>
<a href="sil.php?sayfa=kullanicilar&id=<?= $result["id"] ?>" class="btn btn-danger">Sil</a>
</div>
</div>
</div>
</div>
</td>
<?php
} // While End
?>
</tr>
</tbody>
</table>
</div>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="js/jquery-3.4.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
PHPYeni Kullanıcı Ekleme

Yeni kullanıcı eklemek için İNSERT İNTO komutunu kullanmamız gerekmektedir. Öncelikle bir TABLE oluşturup aşağıdaki gibi alanları ekliyoruz. Daha sonra $POST komutu ile girilen verileri değişkene ekliyoruz. Daha sonra veri tabanı kontrollerini sağlıyoruz.
ekle.php
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<title></title>
</head>
<body>
<body>
<div class="container">
<div class="row">
<div class="col-md-6">
<ol class="breadcrumb">
<li class="breadcrumb-item">
<a href="index.php">Ana Sayfa</a>
</li>
<li class="breadcrumb-item active">Yeni Kullanıcı Ekle</li>
</ol>
<div class="card mb-3">
<div class="card-body">
<form method="POST" action="" enctype="multipart/form-data">
<div class="form-group">
<label>User Name</label>
<input required type="text" class="form-control" name="kullanici" placeholder="Yeni Kullanıcı Adı">
</div>
<div class="form-group">
<label>Yeni Parola</label>
<input required type="password" class="form-control" name="parola" placeholder="Yeni Parola">
</div>
<div class="form-group">
<label>Yeni Parola Tekrar</label>
<input required type="password" class="form-control" name="parolatekrar" placeholder="Confirm New Password">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Kaydet</button>
<script type="text/javascript" src="js/sweetalert.min.js"></script>
<?php
include('fonc.php');
if ($_POST) { // We check if there is a post on the page.
$kullanici = $_POST['kullanici'];// After the page is refreshed, we assign the posted values to the variables
$parola = md5('56' . $_POST['parola'] . '23');
// We encrypt variables with MD5 Format according to specified ranges
$parolatekrar = md5('56' . $_POST['parolatekrar'] . '23'); // We encrypt variables with MD5 Format
$error = ""; // We print our mistakes
if ($kullanici <> "" && $parola <> "" && $error == "") { // // We check if the data fields are empty. You can do it in other controls.
//Data to change
$line = [
'kullanici' => $kullanici,
'parola' => $parola,
];
if ($parola == $parolatekrar && $parola != '' && $kullanici != '') { // Checking if New Password and Repeat Password are the same
$sql = "INSERT INTO kullanicilar SET kullanici=:kullanici, parola=:parola;";
// If all conditions are positive, we write our data insertion query.
$status = $connect->prepare($sql)->execute($line);
if ($status) {
echo '<script>swal("Başşarılı.","Yeni Kullanıcı Eklendi.","success").then((value)=>{ window.location.href = "index.php"}); </script>';
//If adding data is successful, it is written that it is successful with sweetalert.
// If the Add query works, it redirects to the index.php page.
}
}
else {
echo '<script>swal("Hata","Lürfen Bilgilerinizi Kontrol Edin","error");</script>';
// If the id is not found or there is an error in the query, we print an error
}
}
if ($error != "") {
echo '<script>swal("Hata","' . $error . '","Error");</script>'; // We print our errors that may occur in queries and database
}
}
?>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
PHPHemen çıkış yapmak için index.php sayfasında çıkış yap adı altında bir button oluşturalım. Bu button rengi kırmızı olacak. Bunun için de BootStrap kütüphanesinden yararlanacağız. Kullanıcın sayfadan çıkış yapması için öncelikle bir SESSION başlatmamız gerekir. Eğer beni hatırla kısmı tıklandıysa tarama verilerinizi silmeniz gerekecektir. Çünkü beni hatırla kısmı bir çerez (cookie) tarayıcınıza kayıt eder. Bunun için eski çerezleri silmeniz gerekebilir. Eğer beni hatırla tıklanmadıysa direk çıkış sağlanır. Çıkış yapmak için de header komutu kullanıp gidilecek sayfaya yönlendirme yapıyoruz.
cıkıs.php
<?php
session_start();
session_destroy();
setcookie("Session", md5("aa" . $txtuser . "bb"), time() - 1);
header("location:index.php");
?>
PHPDüzenleme işlemi yapmak için index.php sayfasında button oluşturup gidileceği sayfa yolunu belirliyoruz. Daha sonra bu sayfada id ekleyip veri tabanı id tümünü dahil ediyoruz. Böylelikle verilerin numarası ile düzenleme işlemi gerçekleştirebileceğiz.

duzenle.php
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<title></title>
</head>
<?php
include('fonc.php'); // We Connected Our Database
$query = $connect->prepare("SELECT * FROM kullanicilar Where id=:id");
// We transfer the incoming IDs to variables and inputs.
$query->execute(['id' => (int)$_GET["id"]]);
$result = $query->fetch();//executing query and getting data
?>
<body>
<div class="container">
<div class="row">
<div class="col-md-6">
<ol class="breadcrumb">
<li class="breadcrumb-item">
<a href="index.php">Ana Sayfa</a>
</li>
<li class="breadcrumb-item active">Kullanıcı Düzenleme Sayfası</li>
</ol>
<div class="card mb-3">
<div class="card-body">
<form method="post" action="" enctype="multipart/form-data">
<div class="form-group">
<label>Kullanıcı Adı</label>
<input required type="text" value="<?= $result["kullanici"] ?>" class="form-control" name="kullanici"
placeholder="Yeni Kullanıcı Adı">
</div>
<div class="form-group">
<label>Yeni Parola</label>
<input required type="password" class="form-control" name="parola"
placeholder="Yeni Parola Giriniz.">
</div>
<div class="form-group">
<label>Yeni Parola Tekrar</label>
<input required type="password" class="form-control" name="parolaTekrar"
placeholder="Yeni Parolayı Tekrar Giriniz">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Güncelle</button>
<script type="text/javascript" src="js/sweetalert.min.js"></script>
<?php
if ($_POST) { // We check if there is a post on the page.
$kullanici = $_POST['kullanici'];// After the page is refreshed, we assign the posted values to the variables
$parola = md5('56' . $_POST['parola'] . '23');
// We encrypt variables with MD5 Format according to specified ranges
$parolaTekrar = md5('56' . $_POST['parolaTekrar'] . '23'); // We encrypt variables with MD5 Format
$error = "";
// We make sure that the data fields are not empty. You can do it in other controls.
if ($kullanici <> "" && $parola <> "" && $error == "") { // We make sure that the data fields are not empty.
//Data to change
$line = [
'id' => $_GET['id'],
'kullanici' => $kullanici,
'parola' => $parola,
];
if ($parola == $parolaTekrar && $parola != '' && $kullanici != '') {
// We make sure that the data fields are not empty. You can do it in other controls.
$sql = "UPDATE kullanicilar SET kullanici=:kullanici,parola=:parola WHERE id=:id;";
$status = $connect->prepare($sql)->execute($line);
if ($status) {
echo '<script>swal("Başarılı","Kullanıcı Güncellendi.","success").then((value)=>{ window.location.href = "index.php"});
</script>';
// If the update query is working, it redirects to the products.php page.
}
}
else {
echo '<script>swal("Oops","Hata, Lütfen Bilgileri Doğru Girdiğinizden Emin Olun.","error");</script>'; // If the id is not found or there is an error in the query, we print an error.
}
}
if ($error != "") {
echo '<script>swal("Oops","' . $error . '","error");</script>'; // We print out any errors that may occur in queries and databases.
}
}
?>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
PHPSon olarak silme işlemi gerçekleştirmek için index sayfasında sayfa adında değişken oluşturuyoruz. Bu sayfa adına kullanıcılar ismini veriyoruz. Böylelikle bir veri silme işlemi yaparsak veri tabanı ismini sayfa değişkenine atadığımız için kolay olacak. Ayrıca sil buttonuna modal işlemi uyguluyoruz.

sil.php
<?php
session_start(); //we started a session
//redirects page if current session exists
if (!(isset($_SESSION["Session"]) && $_SESSION["Session"] == "9876")) {
header("location:login.php");
} //If remember me is checked beforehand, we create a session and redirect the page.
if ($_GET) {
$sayfa = $_GET["sayfa"];
include("fonc.php"); // we include our database connection on our page.
$query = $connect->prepare("SELECT * FROM $sayfa Where id=:id");
$query->execute(['id' => (int)$_GET["id"]]);
$result = $query->fetch();//executing query and getting data
// We write our query to delete the data whose id is selected.
$where = ['id' => (int)$_GET['id']];
$status = $connect->prepare("DELETE FROM $sayfa WHERE id=:id")->execute($where);
if ($status) {
header("location:index.php"); // If the query works, we send it to the index.php page.
}
}
?>
PHPMakalemi beğendiyseniz beni Disqus hesabı oluşturarak ifadenizi belirtebilirsiniz. Ayrıca yorum yaparak ne gibi programlar yapmamı istiyorsanız belirtebilirsiniz.
önceki makaleme göz atın –> crud işlemleri php pdo
projeyi indirin — > login sayfası