Skapa först en ny fil med namnet 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 definiera en ny kolumn med namnet id
som har int type
och det är auto_increment
så det kommer att vara din primära nyckel. Primära nycklar måste vara unika och din tabell saknar en sådan kolumn. Så, i din phpMyAdmin skriv i SQL-fliken:
ALTER TABLE `users` ADD `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST ;
Sedan kan du i din inloggningsfil använda PDO som nämnts ovan från de andra användarna (om du inte är redo för detta kan du ta en titt här och här ).
<?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();
}
?>
Slutligen, i din fil assigned_papers.php
du kan komma åt $_SESSION
variabler du redan har lagrat och hämta sedan alla tilldelade papper för användaren som just loggat in.
<?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();
}
?>