Cevap: php kod isteği
**PHP Kullanıcı Yönetimi Sistemi Örneği**
Aşağıda, basit bir kullanıcı yönetimi sistemi örneği verilmiştir. Bu sistem, kullanıcıların veritabanına eklenmesi, giriş yapması ve çıkış yapması işlemlerini içerir.
### Veritabanı Yapısı
Öncelikle, kullanıcı bilgilerini saklamak için bir veritabanı tablosu oluşturulmalıdır. Aşağıdaki MySQL komutu ile `kullanicilar` tablosu oluşturabilirsiniz:
```sql
CREATE TABLE kullanicilar (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
);
```
### db.php
Veritabanı bağlantısını yönetmek için `db.php` dosyası oluşturun:
```php
<?php
class DB {
private $host = 'localhost';
private $dbname = 'kullanici_yonetimi';
private $user = 'kullanici';
private $pass = 'parola';
public function connect() {
try {
$conn = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->user, $this->pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
} catch(PDOException $e) {
die("Bağlantı hatası: " . $e->getMessage());
}
}
}
```
### Kullanıcı Ekleme (register.php)
Yeni bir kullanıcı eklemek için `register.php` dosyası oluşturun:
```php
<?php
require_once 'db.php';
if(isset($_POST['kaydol'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$db = new DB();
$conn = $db->connect();
$query = "INSERT INTO kullanicilar (username, password) VALUES (:username, :password)";
$stmt = $conn->prepare($query);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', password_hash($password, PASSWORD_DEFAULT));
try {
$stmt->execute();
echo "Kullanıcı başarıyla eklendi.";
} catch(PDOException $e) {
echo "Hata: " . $e->getMessage();
}
}
?>
<form action="" method="post">
<input type="text" name="username" placeholder="Kullanıcı adı">
<input type="password" name="password" placeholder="Parola">
<input type="submit" name="kaydol" value="Kaydol">
</form>
```
### Giriş Yapma (login.php)
Kullanıcı girişi için `login.php` dosyası oluşturun:
```php
<?php
require_once 'db.php';
session_start();
if(isset($_POST['giris'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$db = new DB();
$conn = $db->connect();
$query = "SELECT * FROM kullanicilar WHERE username = :username";
$stmt = $conn->prepare($query);
$stmt->bindParam(':username', $username);
try {
$stmt->execute();
$user = $stmt->fetch();
if($user && password_verify($password, $user['password'])) {
$_SESSION['username'] = $username;
header("Location: index.php");
} else {
echo "Hatalı kullanıcı adı veya parola.";
}
} catch(PDOException $e) {
echo "Hata: " . $e->getMessage();
}
}
?>
<form action="" method="post">
<input type="text" name="username" placeholder="Kullanıcı adı">
<input type="password" name="password" placeholder="Parola">
<input type="submit" name="giris" value="Giriş">
</form>
```
### Çıkış Yapma (logout.php)
Kullanıcıyı çıkış yaptırmak için `logout.php` dosyası oluşturun:
```php
<?php
session_start();
session_unset();
session_destroy();
header("Location: index.html");
exit;
```
### index.php
Kullanıcı girişi başarılı ise kullanıcıya hoş geldiniz mesajı göstermek için `index.php` dosyası oluşturun:
```php
<?php
require_once 'db.php';
session_start();
if(isset($_SESSION['username'])) {
echo "Hoş geldiniz, " . $_SESSION['username'];
echo " <a href='logout.php'>Çıkış</a>";
} else {
echo "Giriş yapınız.";
echo " <a href='login.php'>Giriş</a>";
}
```
### index.html
Giriş yapmamış kullanıcılar için `index.html` dosyası oluşturun:
```html
<!DOCTYPE html>
<html>
<head>
<title>Kullanıcı Yönetimi</title>
</head>
<body>
<h1>Kullanıcı Yönetimi</h1>
<p>Giriş yapınız.</p>
<a href="login.php">Giriş</a>
</body>
</html>
```
Bu örnekte, kullanıcılar `register.php` sayfasından kayıt olabilir, `login.php` sayfasından giriş yapabilir ve `logout.php` sayfasından çıkış yapabilirler. Giriş başarılı ise `index.php` sayfasında hoş geldiniz mesajı görüntülenir. |