Pertama, buat file baru bernama constants.php
.
<?php
//This is constants.php file
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'conference');
?>
Anda harus menentukan kolom baru bernama id
yang memiliki int type
dan itu auto_increment
jadi itu akan menjadi kunci utama Anda. Kunci utama harus unik dan tabel Anda tidak memiliki kolom seperti itu. Jadi, di phpMyAdmin Anda, tulis di tab SQL:
ALTER TABLE `users` ADD `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST ;
Kemudian, dalam file login Anda, Anda dapat menggunakan PDO seperti yang disebutkan di atas dari pengguna lain (jika Anda belum siap untuk ini, Anda dapat melihat di sini dan di sini ).
<?php
function SignIn() {
require_once("constants.php"); //Now constants will be accessible
session_start();
try {
$link = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASSWORD);
$username = $_POST['username']; //no need to esaping as we will use prepared statements
$password = $_POST['password'];
if (!empty($username) && !empty($password)) {
//You need to define a new column named "id" which will be int auto_increment and it will be your primary key
$sql = "SELECT id, username, password FROM users where username = :username AND password = :password";
//Prepare your query
$stmt = $link->prepare($sql);
//Execute your query binding variables values
$stmt->execute(array(':username'=>$username, ':password'=>$password));
//Fetch the row that match the criteria
$row = $stmt->fetch();
if (!empty($row['username']) && !empty($row['password'])) {
$_SESSION['is_logged'] = true; //Now user is considered logged in
$_SESSION['username'] = $row['username'];
$_SESSION['id'] = $row['id'];
//Never store passwords in $_SESSION
echo "Welcome to your User Account for CSIT Conference. Click to go home: ";
echo '<a href="index.html"> Home Page </a>. ';
echo "Or here to go to your assigned papers: ";
echo '<a href="assigned.php"> Assigned Papers </a>. ';
} else {
echo "SORRY... YOU ENTERED WRONG ID AND PASSWORD... PLEASE RETRY...";
}
$link = null;
} else {
echo 'Please enter username and password.';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
}
if (isset($_POST['submit'])) {
SignIn();
}
?>
Terakhir, dalam file Anda assigned_papers.php
Anda dapat mengakses $_SESSION
variabel yang sudah Anda simpan dan kemudian ambil semua kertas yang ditugaskan untuk pengguna yang baru saja masuk.
<?php
//assigned_papers
session_start();
require_once("constants.php"); //Now constants will be accessible
if (!empty($_SESSION['is_logged'])) {
echo 'Hello, '.$_SESSION['username'].'! Here are your assigned papers: ';
try {
$link = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASSWORD);
$sql = "SELECT * FROM assigned_papers where users_id = :users_id";
$stmt = $link->prepare($sql);
//We want all assigned papers for the particular user
$stmt->execute(array(':users_id'=>$_SESSION['id']));
$result = $stmt->fetchAll();
foreach ($result as $row) {
//You can echo what you want from table assigned_papers
//echo '<p>'.$row['paper_name'].'</p>';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
} else
header("Location: login.php"); //If user isn't logged in then redirect him to login page
die();
}
?>