Først skal du oprette en ny fil med navnet constants.php
.
<?php
//This is constants.php file
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'conference');
?>
Du bør definere en ny kolonne med navnet id
som har int type
og det er auto_increment
så det vil være din primære nøgle. Primære nøgler skal være unikke, og din tabel mangler sådan en kolonne. Så skriv i din phpMyAdmin i SQL-fanen:
ALTER TABLE `users` ADD `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST ;
Så kan du i din login-fil bruge PDO som nævnt ovenfor fra de andre brugere (hvis du ikke er klar til dette, kan du se her og her ).
<?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();
}
?>
Til sidst, i din fil assigned_papers.php
du kan få adgang til $_SESSION
variabler, du allerede har gemt, og hent derefter alle de tildelte papirer for den bruger, der lige er logget ind.
<?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();
}
?>