Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- File 1 ) /* class_user.php */
- <?php
- require_once('dbconfig.php');
- class USER
- {
- private $conn;
- public function __construct()
- {
- $database = new Database();
- $db = $database->dbConnection();
- $this->conn = $db;
- }
- public function runQuery($sql)
- {
- $stmt = $this->conn->prepare($sql);
- return $stmt;
- }
- public function register($uname,$umail,$upass)
- {
- try
- {
- $new_password = password_hash($upass, PASSWORD_DEFAULT);
- $stmt = $this->conn->prepare("INSERT INTO users(user_name,user_email,user_pass)
- VALUES(:uname, :umail, :upass)");
- $stmt->bindparam(":uname", $uname);
- $stmt->bindparam(":umail", $umail);
- $stmt->bindparam(":upass", $new_password);
- $stmt->execute();
- return $stmt;
- }
- catch(PDOException $e)
- {
- echo $e->getMessage();
- }
- }
- public function doLogin($uname,$umail,$upass)
- {
- try
- {
- $stmt = $this->conn->prepare("SELECT user_id, user_name, user_email, user_pass FROM users WHERE user_name=:uname OR user_email=:umail ");
- $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
- $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
- if($stmt->rowCount() == 1)
- {
- if(password_verify($upass, $userRow['user_pass']))
- {
- $_SESSION['user_session'] = $userRow['user_id'];
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- catch(PDOException $e)
- {
- echo $e->getMessage();
- }
- }
- public function is_loggedin()
- {
- if(isset($_SESSION['user_session']))
- {
- return true;
- }
- }
- public function redirect($url)
- {
- header("Location: $url");
- }
- public function doLogout()
- {
- session_destroy();
- unset($_SESSION['user_session']);
- return true;
- }
- }
- ?>
- File 2 : /* dbconfig.php */
- <?php
- class Database
- {
- private $host = "localhost";
- private $db_name = "mysql_login2";
- private $username = "root";
- private $password = "";
- public $conn;
- public function dbConnection()
- {
- $this->conn = null;
- try
- {
- $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
- $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch(PDOException $exception)
- {
- echo "Connection error: " . $exception->getMessage();
- }
- return $this->conn;
- }
- }
- ?>
- file 3 : dblogin.sql
- -- phpMyAdmin SQL Dump
- -- version 4.1.14
- -- http://www.phpmyadmin.net
- --
- -- Host: 127.0.0.1
- -- Generation Time: Jan 07, 2016 at 03:05 AM
- -- Server version: 5.6.17
- -- PHP Version: 5.5.12
- SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
- SET time_zone = "+00:00";
- /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
- /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
- /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
- /*!40101 SET NAMES utf8 */;
- --
- -- Database: `dblogin`
- --
- -- --------------------------------------------------------
- --
- -- Table structure for table `users`
- --
- CREATE TABLE IF NOT EXISTS `users` (
- `user_id` int(11) NOT NULL AUTO_INCREMENT,
- `user_name` varchar(15) NOT NULL,
- `user_email` varchar(40) NOT NULL,
- `user_pass` varchar(255) NOT NULL,
- `joining_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (`user_id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
- /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
- /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
- /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
- file 4 : home.php
- <?php
- require_once("session.php");
- require_once("class.user.php");
- $auth_user = new USER();
- $user_id = $_SESSION['user_session'];
- $stmt = $auth_user->runQuery("SELECT * FROM users WHERE user_id=:user_id");
- $stmt->execute(array(":user_id"=>$user_id));
- $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
- <link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
- <script type="text/javascript" src="jquery-1.11.3-jquery.min.js"></script>
- <link rel="stylesheet" href="style.css" type="text/css" />
- <title>welcome - <?php print($userRow['user_email']); ?></title>
- </head>
- <body>
- <nav class="navbar navbar-default navbar-fixed-top">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
- <span class="sr-only">Toggle navigation</span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <a class="navbar-brand" href="http://www.website.com">site</a>
- </div>
- <div id="navbar" class="navbar-collapse collapse">
- <ul class="nav navbar-nav">
- <li class="active"><a href="http://www.website.com/login">Back to Article</a></li>
- <li><a href="link9">jQuery</a></li>
- <li><a href="link789">PHP</a></li>
- </ul>
- <ul class="nav navbar-nav navbar-right">
- <li class="dropdown">
- <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
- <span class="glyphicon glyphicon-user"></span> Hi' <?php echo $userRow['user_email']; ?> <span class="caret"></span></a>
- <ul class="dropdown-menu">
- <li><a href="profile.php"><span class="glyphicon glyphicon-user"></span> View Profile</a></li>
- <li><a href="logout.php?logout=true"><span class="glyphicon glyphicon-log-out"></span> Sign Out</a></li>
- </ul>
- </li>
- </ul>
- </div><!--/.nav-collapse -->
- </div>
- </nav>
- <div class="clearfix"></div>
- <div class="container-fluid" style="margin-top:80px;">
- <div class="container">
- <label class="h5">welcome : <?php print($userRow['user_name']); ?></label>
- <hr />
- <h1>
- <a href="home.php"><span class="glyphicon glyphicon-home"></span> home</a>
- <a href="profile.php"><span class="glyphicon glyphicon-user"></span> profile</a></h1>
- <hr />
- <p class="h4">User Home Page</p>
- <p class="blockquote-reverse" style="margin-top:200px;">
- website themes<br /><br />
- <a href="link4">login here</a>
- </p>
- </div>
- </div>
- <script src="bootstrap/js/bootstrap.min.js"></script>
- </body>
- </html>
- file 5 : index.php
- <?php
- session_start();
- require_once("class.user.php");
- $login = new USER();
- if($login->is_loggedin()!="")
- {
- $login->redirect('home.php');
- }
- if(isset($_POST['btn-login']))
- {
- $uname = strip_tags($_POST['txt_uname_email']);
- $umail = strip_tags($_POST['txt_uname_email']);
- $upass = strip_tags($_POST['txt_password']);
- if($login->doLogin($uname,$umail,$upass))
- {
- $login->redirect('home.php');
- }
- else
- {
- $error = "Wrong Details !";
- }
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Coding Cage : Login</title>
- <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
- <link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
- <link rel="stylesheet" href="style.css" type="text/css" />
- </head>
- <body>
- <div class="signin-form">
- <div class="container">
- <form class="form-signin" method="post" id="login-form">
- <h2 class="form-signin-heading">Log In to WebApp.</h2><hr />
- <div id="error">
- <?php
- if(isset($error))
- {
- ?>
- <div class="alert alert-danger">
- <i class="glyphicon glyphicon-warning-sign"></i> <?php echo $error; ?> !
- </div>
- <?php
- }
- ?>
- </div>
- <div class="form-group">
- <input type="text" class="form-control" name="txt_uname_email" placeholder="Username or E mail ID" required />
- <span id="check-e"></span>
- </div>
- <div class="form-group">
- <input type="password" class="form-control" name="txt_password" placeholder="Your Password" />
- </div>
- <hr />
- <div class="form-group">
- <button type="submit" name="btn-login" class="btn btn-default">
- <i class="glyphicon glyphicon-log-in"></i> SIGN IN
- </button>
- </div>
- <br />
- <label>Don't have account yet ! <a href="sign-up.php">Sign Up</a></label>
- </form>
- </div>
- </div>
- </body>
- </html>
- file 6 : logout.php
- <?php
- require_once('session.php');
- require_once('class.user.php');
- $user_logout = new USER();
- if($user_logout->is_loggedin()!="")
- {
- $user_logout->redirect('home.php');
- }
- if(isset($_GET['logout']) && $_GET['logout']=="true")
- {
- $user_logout->doLogout();
- $user_logout->redirect('index.php');
- }
- file 7 : profile .php
- <?php
- require_once("session.php");
- require_once("class.user.php");
- $auth_user = new USER();
- $user_id = $_SESSION['user_session'];
- $stmt = $auth_user->runQuery("SELECT * FROM users WHERE user_id=:user_id");
- $stmt->execute(array(":user_id"=>$user_id));
- $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
- <link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
- <script type="text/javascript" src="jquery-1.11.3-jquery.min.js"></script>
- <link rel="stylesheet" href="style.css" type="text/css" />
- <title>welcome - <?php print($userRow['user_email']); ?></title>
- </head>
- <body>
- <nav class="navbar navbar-default navbar-fixed-top">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
- <span class="sr-only">Toggle navigation</span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <a class="navbar-brand" href="http://oursite.com">Coding Cage</a>
- </div>
- <div id="navbar" class="navbar-collapse collapse">
- <ul class="nav navbar-nav">
- <li class="active"><a href="link8">Back to site</a></li>
- <li><a href="link9">j</a></li>
- <li><a href="link10">P</a></li>
- </ul>
- <ul class="nav navbar-nav navbar-right">
- <li class="dropdown">
- <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
- <span class="glyphicon glyphicon-user"></span> Hi' <?php echo $userRow['user_email']; ?> <span class="caret"></span></a>
- <ul class="dropdown-menu">
- <li><a href="#"><span class="glyphicon glyphicon-user"></span> View Profile</a></li>
- <li><a href="logout.php?logout=true"><span class="glyphicon glyphicon-log-out"></span> Sign Out</a></li>
- </ul>
- </li>
- </ul>
- </div><!--/.nav-collapse -->
- </div>
- </nav>
- <div class="clearfix"></div>
- <div class="container-fluid" style="margin-top:80px;">
- <div class="container">
- <label class="h5">welcome : <?php print($userRow['user_name']); ?></label>
- <hr />
- <h1>
- <a href="home.php"><span class="glyphicon glyphicon-home"></span> home</a>
- <a href="profile.php"><span class="glyphicon glyphicon-user"></span> profile</a></h1>
- <hr />
- <p class="h4">Another Secure Profile Page</p>
- <p class="blockquote-reverse" style="margin-top:200px;">
- nice<br /><br />
- <a href="link5">site link</a>
- </p>
- </div>
- </div>
- <script src="bootstrap/js/bootstrap.min.js"></script>
- </body>
- </html>
- file 8 : session.php
- <?php
- session_start();
- require_once 'class.user.php';
- $session = new USER();
- // if user session is not active(not loggedin) this page will help 'home.php and profile.php' to redirect to login page
- // put this file within secured pages that users (users can't access without login)
- if(!$session->is_loggedin())
- {
- // session no set redirects to login page
- $session->redirect('index.php');
- }
- file 9 : signup.php
- <?php
- session_start();
- require_once('class.user.php');
- $user = new USER();
- if($user->is_loggedin()!="")
- {
- $user->redirect('home.php');
- }
- if(isset($_POST['btn-signup']))
- {
- $uname = strip_tags($_POST['txt_uname']);
- $umail = strip_tags($_POST['txt_umail']);
- $upass = strip_tags($_POST['txt_upass']);
- if($uname=="") {
- $error[] = "provide username !";
- }
- else if($umail=="") {
- $error[] = "provide email id !";
- }
- else if(!filter_var($umail, FILTER_VALIDATE_EMAIL)) {
- $error[] = 'Please enter a valid email address !';
- }
- else if($upass=="") {
- $error[] = "provide password !";
- }
- else if(strlen($upass) < 6){
- $error[] = "Password must be atleast 6 characters";
- }
- else
- {
- try
- {
- $stmt = $user->runQuery("SELECT user_name, user_email FROM users WHERE user_name=:uname OR user_email=:umail");
- $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
- $row=$stmt->fetch(PDO::FETCH_ASSOC);
- if($row['user_name']==$uname) {
- $error[] = "sorry username already taken !";
- }
- else if($row['user_email']==$umail) {
- $error[] = "sorry email id already taken !";
- }
- else
- {
- if($user->register($uname,$umail,$upass)){
- $user->redirect('sign-up.php?joined');
- }
- }
- }
- catch(PDOException $e)
- {
- echo $e->getMessage();
- }
- }
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Coding Cage : Sign up</title>
- <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
- <link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
- <link rel="stylesheet" href="style.css" type="text/css" />
- </head>
- <body>
- <div class="signin-form">
- <div class="container">
- <form method="post" class="form-signin">
- <h2 class="form-signin-heading">Sign up.</h2><hr />
- <?php
- if(isset($error))
- {
- foreach($error as $error)
- {
- ?>
- <div class="alert alert-danger">
- <i class="glyphicon glyphicon-warning-sign"></i> <?php echo $error; ?>
- </div>
- <?php
- }
- }
- else if(isset($_GET['joined']))
- {
- ?>
- <div class="alert alert-info">
- <i class="glyphicon glyphicon-log-in"></i> Successfully registered <a href='index.php'>login</a> here
- </div>
- <?php
- }
- ?>
- <div class="form-group">
- <input type="text" class="form-control" name="txt_uname" placeholder="Enter Username" value="<?php if(isset($error)){echo $uname;}?>" />
- </div>
- <div class="form-group">
- <input type="text" class="form-control" name="txt_umail" placeholder="Enter E-Mail ID" value="<?php if(isset($error)){echo $umail;}?>" />
- </div>
- <div class="form-group">
- <input type="password" class="form-control" name="txt_upass" placeholder="Enter Password" />
- </div>
- <div class="clearfix"></div><hr />
- <div class="form-group">
- <button type="submit" class="btn btn-primary" name="btn-signup">
- <i class="glyphicon glyphicon-open-file"></i> SIGN UP
- </button>
- </div>
- <br />
- <label>have an account ! <a href="index.php">Sign In</a></label>
- </form>
- </div>
- </div>
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment