hi

//DB CONNECTOR<?phpclass dbConnector { private $host = “localhost”; private $dbname = “lecture4db”; private $dbuser = “isthi”; private $dbpw = “isthi”; public function getConnection() { try{ //create PDO $dsn = “mysql:host=”.$this->host.”; dbname=”.$this->dbname; $con = new PDO($dsn, $this->dbuser, $this->dbpw); return $con; } catch (Exception $ex) { die(“Connection failed: “.$ex->getMessage()); } } }//loginprocess<?phpsession_start();require ‘dbConnector.php’;$dbcon = new dbConnector();$con = $dbcon->getConnection(); if ($_SERVER[“REQUEST_METHOD”] == “POST”) { $query = “SELECT * FROM user_new WHERE username = ?”; //Get imputs from users $username = $_POST[‘uname’]; $password = $_POST[‘pwd’]; try { $stmt = $con->prepare($query); $stmt->bindValue(1, $username); $stmt->execute(); //run the sql query $row = $stmt->fetch(PDO::FETCH_OBJ); if ($row) { if (password_verify($password, $row->password)) { // Set session variables $_SESSION[‘uname’] = $username; $_SESSION[‘fname’] = $row->fname; $_SESSION[‘lname’] = $row->lname; header(“Location: welcome.php”); exit(); } else { echo “Login unsuccessful: Incorrect password.”; } } else { echo “Login unsuccessful: User not found.”; } } catch (PDOException $e) { die(“Database error: ” . $e->getMessage()); }}//logout<?phpsession_start();session_unset();session_destroy();header(“Location: LoginForm.php”);exit();//Register process<?phprequire ‘dbConnector.php’;$dbcon = new dbConnector();$con = $dbcon->getConnection();if ($_SERVER[“REQUEST_METHOD”] == “POST”) { $query = “INSERT INTO user_new (username, password, fname, lname) VALUES (?, ?, ?, ?)”; // Get user inputs $username = $_POST[‘username’]; $password = $_POST[‘password’]; $fname = $_POST[‘fname’]; $lname = $_POST[‘lname’]; try { $stmt = $con->prepare($query); // Bind values $stmt->bindValue(1, $username); $stmt->bindValue(2, password_hash($password, PASSWORD_DEFAULT)); $stmt->bindValue(3, $fname); $stmt->bindValue(4, $lname); // Execute statement $rows = $stmt->execute(); if ($rows) { header(“Location: LoginForm.php”); } else { echo “Error inserting user. Please try again<br>”; } } catch (Exception $ex) { die(“Database Error: ” . $ex->getMessage()); }}//welcome<?phpsession_start();if(isset($_SESSION[‘uname’])){ $fname = $_SESSION[‘fname’]; $lname = $_SESSION[‘lname’];}else{ header(“location:LoginForm.php”); exit();}?><!DOCTYPE html><html><head> <title>Welcome</title></head><body> <h3>Welcome to My Website</h3> <p>Hello, <?php echo htmlspecialchars($fname) . ” ” . htmlspecialchars($lname); ?></p> <p><a href=”Logout.php”>Logout</a></p></body></html>

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *