Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // database Connection
- function DB()
- {
- try {
- $db = new PDO("sqlite:db/login_system.db");
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- return $db;
- } catch (PDOException $e) {
- return "Error!: " . $e->getMessage();
- die();
- }
- }
- ?>
- <?php
- class DemoLib
- {
- /*
- * Register New User
- *
- * @param $name, $email, $username, $password
- * @return ID
- * */
- public function Register($name, $email, $username, $password)
- {
- try {
- //$db = DB();
- $db = new PDO("sqlite:db/login_system.db");
- $insert = "INSERT INTO users(name, email, username, password) VALUES (:name,:email,:username,:password)";
- $query = $db->prepare($insert);
- $query->bindParam("name", $name, PDO::PARAM_STR);
- $query->bindParam("email", $email, PDO::PARAM_STR);
- $query->bindParam("username", $username, PDO::PARAM_STR);
- $enc_password = hash('sha256', $password);
- $query->bindParam("password", $enc_password, PDO::PARAM_STR);
- $query->execute();
- return $db->lastInsertId();
- } catch (PDOException $e) {
- exit($e->getMessage());
- }
- }
- /*
- * Check Username
- *
- * @param $username
- * @return boolean
- * */
- public function isUsername($username)
- {
- try {
- //$db = DB();
- $db = new PDO("sqlite:db/login_system.db");
- $select = "SELECT user_id FROM users WHERE username=:username";
- $query = $db->prepare($select);
- $query->bindParam("username", $username, PDO::PARAM_STR);
- $query->execute();
- if ($query->rowCount() > 0) {
- return true;
- } else {
- return false;
- }
- } catch (PDOException $e) {
- exit($e->getMessage());
- }
- }
- /*
- * Check Email
- *
- * @param $email
- * @return boolean
- * */
- public function isEmail($email)
- {
- try {
- //$db = DB();
- $db = new PDO("sqlite:db/login_system.db");
- $select = "SELECT user_id FROM users WHERE email=:email";
- $query = $db->prepare($select);
- $query->bindParam("email", $email, PDO::PARAM_STR);
- $query->execute();
- if ($query->rowCount() > 0) {
- return true;
- } else {
- return false;
- }
- } catch (PDOException $e) {
- exit($e->getMessage());
- }
- }
- /*
- * Login
- *
- * @param $username, $password
- * @return $mixed
- * */
- public function Login($username, $password)
- {
- try {
- //$db = DB();
- $db = new PDO("sqlite:db/login_system.db");
- $select = "SELECT user_id FROM users WHERE username=:username AND password=:password";
- $query = $db->prepare($select);
- $query->bindParam("username", $username, PDO::PARAM_STR);
- $enc_password = hash('sha256', $password);
- $query->bindParam("password", $enc_password, PDO::PARAM_STR);
- $query->execute();
- if ($query->rowCount() > 0) {
- $result = $query->fetch(PDO::FETCH_OBJ);
- return $result->user_id;
- } else {
- return false;
- }
- } catch (PDOException $e) {
- exit($e->getMessage());
- }
- }
- /*
- * get User Details
- *
- * @param $user_id
- * @return $mixed
- * */
- public function UserDetails($user_id)
- {
- try {
- //$db = DB();
- $db = new PDO("sqlite:db/login_system.db");
- $select = "SELECT user_id, name, username, email FROM users WHERE user_id=:user_id";
- $query = $db->prepare($select);
- $query->bindParam("user_id", $user_id, PDO::PARAM_STR);
- $query->execute();
- if ($query->rowCount() > 0) {
- return $query->fetch(PDO::FETCH_OBJ);
- }
- } catch (PDOException $e) {
- exit($e->getMessage());
- }
- }
- }
- <?php
- include("database.php");//make connection here
- // Application library ( with DemoLib class )
- //require __DIR__ . '/lib/library.php';
- include("lib/library.php");
- $app = new DemoLib();
- $login_error_message = '';
- $register_error_message = '';
- // check Register request
- if (!empty($_POST['join'])) {
- if ($_POST['name'] == "") {
- $register_error_message = 'Name field is required!';
- } else if ($_POST['email'] == "") {
- $register_error_message = 'Email field is required!';
- } else if ($_POST['username'] == "") {
- $register_error_message = 'Username field is required!';
- } else if ($_POST['password'] == "") {
- $register_error_message = 'Password field is required!';
- } else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
- $register_error_message = 'Invalid email address!';
- } else if ($app->isEmail($_POST['email'])) {
- $register_error_message = 'Email is already in use!';
- } else if ($app->isUsername($_POST['username'])) {
- $register_error_message = 'Username is already in use!';
- } else {
- $user_id = $app->Register($_POST['name'], $_POST['email'], $_POST['username'], $_POST['password']);
- // set session and redirect user to the welcome page
- $_SESSION['user_id'] = $user_id;
- header("Location: login.php");
- }
- }
- ?>
- <!DOCTYPE html>
- <html >
- <head>
- <meta charset="UTF-8">
- <title>Daily UI 001 Sign Up Form</title>
- <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.css'>
- <link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css'>
- <link rel="stylesheet" href="css/style.css">
- </head>
- <body>
- <div class="signupSection">
- <div class="info">
- <h2>Sign Up</h2>
- <i class="icon ion-ios-ionic-outline" aria-hidden="true"></i>
- <p>The Future Is Here</p>
- </div>
- <?php
- if ($register_error_message != "") {
- echo '<script language="javascript">alert("Please enter valid fields required");</script>';
- }
- ?>
- <form action="registration.php" method="POST" class="signupForm" name="signupform">
- <ul class="noBullet">
- <li>
- <label for="name"></label>
- <input type="text" class="inputFields" id="name" name="name" placeholder="Name" value="" oninput="return userNameValidation(this.value)"/>
- </li>
- <li>
- <label for="username"></label>
- <input type="text" class="inputFields" id="username" name="username" placeholder="Username" value="" oninput="return userNameValidation(this.value)" required/>
- </li>
- <li>
- <label for="password"></label>
- <input type="password" class="inputFields" id="password" name="password" placeholder="Password" value="" oninput="return passwordValidation(this.value)" required/>
- </li>
- <li>
- <label for="email"></label>
- <input type="email" class="inputFields" id="email" name="email" placeholder="Email" value="" required/>
- </li>
- <li id="center-btn">
- <input type="submit" id="join-btn" name="join" alt="Join" value="Join">
- <input type="button" id="join-btn" name="join" alt="Join" value="Already registered" onclick="window.location='login.php';">
- </li>
- </ul>
- </form>
- </div>
- <script src="js/index.js"></script>
- </body>
- </html>
- <?php
- session_start();//session starts here
- include("database.php");
- // Application library ( with DemoLib class )
- //require __DIR__ . '/lib/library.php';
- include("lib/library.php");
- $app = new DemoLib();
- $login_error_message = '';
- $register_error_message = '';
- // check Login request
- if (!empty($_POST['join'])) {
- $username = trim($_POST['username']);
- $password = trim($_POST['password']);
- if ($username == "") {
- $login_error_message = 'Username field is required!';
- } else if ($password == "") {
- $login_error_message = 'Password field is required!';
- } else {
- $user_id = $app->Login($username, $password); // check user login
- if($user_id > 0)
- {
- $_SESSION['user_id'] = $user_id; // Set Session
- header("Location: welcome.php"); // Redirect user to the welcome.php
- }
- else
- {
- $login_error_message = 'Invalid login details!';
- }
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Daily UI 001 Sign Up Form</title>
- <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.css'>
- <link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css'>
- <link rel="stylesheet" href="css/style.css">
- </head>
- <body>
- <div class="signupSection">
- <div class="info">
- <h2>Login-Logout using PHP & SQLite-PDO</h2>
- <i class="icon ion-ios-ionic-outline" aria-hidden="true"></i>
- <p>The Future Is Here</p>
- </div>
- <?php
- if ($login_error_message != "") {
- echo '<script language="javascript">alert("Please enter valid username and password");</script>';
- }
- ?>
- <form action="login.php" method="POST" class="signupForm" name="signupform">
- <h2>Log In</h2>
- <ul class="noBullet">
- <li>
- <label for="username"></label>
- <input type="text" class="inputFields" id="username" name="username" placeholder="Username" value="" oninput="return userNameValidation(this.value)" required/>
- </li>
- <li>
- <label for="password"></label>
- <input type="password" class="inputFields" id="password" name="password" placeholder="Password" value="" oninput="return passwordValidation(this.value)" required/>
- </li>
- <li id="center-btn">
- <input type="submit" id="join-btn" name="join" alt="Join" value="Log In">
- <input type="button" id="join-btn" name="join" alt="Join" value="Create Account" onclick="window.location='registration.php';">
- </li>
- </ul>
- </form>
- </div>
- <script src="js/index.js"></script>
- </body>
- </html>
- <?php
- // Start Session
- session_start();
- // check user login
- if(empty($_SESSION['user_id']))
- {
- header("Location: login.php");
- }
- // Database connection
- require __DIR__ . '/database.php';
- $db = DB();
- // Application library ( with DemoLib class )
- //require __DIR__ . '/lib/library.php';
- include("lib/library.php");
- $app = new DemoLib();
- $user = $app->UserDetails($_SESSION['user_id']); // get user details
- ?>
- <html>
- <head>
- <title>
- Registration
- </title>
- <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.css'>
- <link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css'>
- <link rel="stylesheet" href="css/style.css">
- </head>
- <body>
- <div class="signupSection">
- <div class="info">
- <h2>Welcome</h2>
- <?php
- echo $user->name
- ?>
- </div>
- <br>
- <div class="signupForm">
- <ul class="noBullet">
- <li id="center-btn">
- <input type="button" id="join-btn" name="join" alt="Join" value="Users" onclick="window.location='view_users.php';">
- <input type="button" id="join-btn" name="join" alt="Join" value="Log out" onclick="window.location='logout.php';">
- </li>
- </ul>
- </div>
- </div>
- </body>
- </html>
- <?php
- // start session
- session_start();
- // Destroy user session
- unset($_SESSION['user_id']);
- // Redirect to index.php page
- header("Location: login.php");
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement