Primero, crea un nuevo archivo llamado constants.php
.
<?php
//This is constants.php file
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'conference');
?>
Debe definir una nueva columna llamada id
que tiene int type
y es auto_increment
por lo que será su clave principal. Las claves principales deben ser únicas y su tabla carece de dicha columna. Entonces, en su phpMyAdmin escriba en la pestaña SQL:
ALTER TABLE `users` ADD `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST ;
Luego, en su archivo de inicio de sesión, puede usar PDO como se mencionó anteriormente de los otros usuarios (si no está listo para esto, puede echar un vistazo aquí y aquí ).
<?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();
}
?>
Finalmente, en su archivo assigned_papers.php
puede acceder a la $_SESSION
variables que ya ha almacenado y luego busque todos los documentos asignados para el usuario que acaba de iniciar sesión.
<?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();
}
?>