Guest User

pdo

a guest
Oct 3rd, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.07 KB | None | 0 0
  1. File 1 ) /* class_user.php */
  2.  
  3. <?php
  4.  
  5. require_once('dbconfig.php');
  6.  
  7. class USER
  8. {
  9.  
  10. private $conn;
  11.  
  12. public function __construct()
  13. {
  14. $database = new Database();
  15. $db = $database->dbConnection();
  16. $this->conn = $db;
  17. }
  18.  
  19. public function runQuery($sql)
  20. {
  21. $stmt = $this->conn->prepare($sql);
  22. return $stmt;
  23. }
  24.  
  25. public function register($uname,$umail,$upass)
  26. {
  27. try
  28. {
  29. $new_password = password_hash($upass, PASSWORD_DEFAULT);
  30.  
  31. $stmt = $this->conn->prepare("INSERT INTO users(user_name,user_email,user_pass)
  32. VALUES(:uname, :umail, :upass)");
  33.  
  34. $stmt->bindparam(":uname", $uname);
  35. $stmt->bindparam(":umail", $umail);
  36. $stmt->bindparam(":upass", $new_password);
  37.  
  38. $stmt->execute();
  39.  
  40. return $stmt;
  41. }
  42. catch(PDOException $e)
  43. {
  44. echo $e->getMessage();
  45. }
  46. }
  47.  
  48.  
  49. public function doLogin($uname,$umail,$upass)
  50. {
  51. try
  52. {
  53. $stmt = $this->conn->prepare("SELECT user_id, user_name, user_email, user_pass FROM users WHERE user_name=:uname OR user_email=:umail ");
  54. $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
  55. $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
  56. if($stmt->rowCount() == 1)
  57. {
  58. if(password_verify($upass, $userRow['user_pass']))
  59. {
  60. $_SESSION['user_session'] = $userRow['user_id'];
  61. return true;
  62. }
  63. else
  64. {
  65. return false;
  66. }
  67. }
  68. }
  69. catch(PDOException $e)
  70. {
  71. echo $e->getMessage();
  72. }
  73. }
  74.  
  75. public function is_loggedin()
  76. {
  77. if(isset($_SESSION['user_session']))
  78. {
  79. return true;
  80. }
  81. }
  82.  
  83. public function redirect($url)
  84. {
  85. header("Location: $url");
  86. }
  87.  
  88. public function doLogout()
  89. {
  90. session_destroy();
  91. unset($_SESSION['user_session']);
  92. return true;
  93. }
  94. }
  95. ?>
  96.  
  97. File 2 : /* dbconfig.php */
  98.  
  99. <?php
  100. class Database
  101. {
  102. private $host = "localhost";
  103. private $db_name = "mysql_login2";
  104. private $username = "root";
  105. private $password = "";
  106. public $conn;
  107.  
  108. public function dbConnection()
  109. {
  110.  
  111. $this->conn = null;
  112. try
  113. {
  114. $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
  115. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  116. }
  117. catch(PDOException $exception)
  118. {
  119. echo "Connection error: " . $exception->getMessage();
  120. }
  121.  
  122. return $this->conn;
  123. }
  124. }
  125. ?>
  126.  
  127. file 3 : dblogin.sql
  128.  
  129. -- phpMyAdmin SQL Dump
  130. -- version 4.1.14
  131. -- http://www.phpmyadmin.net
  132. --
  133. -- Host: 127.0.0.1
  134. -- Generation Time: Jan 07, 2016 at 03:05 AM
  135. -- Server version: 5.6.17
  136. -- PHP Version: 5.5.12
  137.  
  138. SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
  139. SET time_zone = "+00:00";
  140.  
  141.  
  142. /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
  143. /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
  144. /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
  145. /*!40101 SET NAMES utf8 */;
  146.  
  147. --
  148. -- Database: `dblogin`
  149. --
  150.  
  151. -- --------------------------------------------------------
  152.  
  153. --
  154. -- Table structure for table `users`
  155. --
  156.  
  157. CREATE TABLE IF NOT EXISTS `users` (
  158. `user_id` int(11) NOT NULL AUTO_INCREMENT,
  159. `user_name` varchar(15) NOT NULL,
  160. `user_email` varchar(40) NOT NULL,
  161. `user_pass` varchar(255) NOT NULL,
  162. `joining_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  163. PRIMARY KEY (`user_id`)
  164. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
  165.  
  166. /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
  167. /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
  168. /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
  169.  
  170. file 4 : home.php
  171.  
  172. <?php
  173.  
  174. require_once("session.php");
  175.  
  176. require_once("class.user.php");
  177. $auth_user = new USER();
  178.  
  179.  
  180. $user_id = $_SESSION['user_session'];
  181.  
  182. $stmt = $auth_user->runQuery("SELECT * FROM users WHERE user_id=:user_id");
  183. $stmt->execute(array(":user_id"=>$user_id));
  184.  
  185. $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
  186.  
  187. ?>
  188. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  189. <html xmlns="http://www.w3.org/1999/xhtml">
  190. <head>
  191. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  192. <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
  193. <link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
  194. <script type="text/javascript" src="jquery-1.11.3-jquery.min.js"></script>
  195. <link rel="stylesheet" href="style.css" type="text/css" />
  196. <title>welcome - <?php print($userRow['user_email']); ?></title>
  197. </head>
  198.  
  199. <body>
  200.  
  201. <nav class="navbar navbar-default navbar-fixed-top">
  202. <div class="container">
  203. <div class="navbar-header">
  204. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
  205. <span class="sr-only">Toggle navigation</span>
  206. <span class="icon-bar"></span>
  207. <span class="icon-bar"></span>
  208. <span class="icon-bar"></span>
  209. </button>
  210. <a class="navbar-brand" href="http://www.website.com">site</a>
  211. </div>
  212. <div id="navbar" class="navbar-collapse collapse">
  213. <ul class="nav navbar-nav">
  214. <li class="active"><a href="http://www.website.com/login">Back to Article</a></li>
  215. <li><a href="link9">jQuery</a></li>
  216. <li><a href="link789">PHP</a></li>
  217. </ul>
  218. <ul class="nav navbar-nav navbar-right">
  219.  
  220. <li class="dropdown">
  221. <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
  222. <span class="glyphicon glyphicon-user"></span>&nbsp;Hi' <?php echo $userRow['user_email']; ?>&nbsp;<span class="caret"></span></a>
  223. <ul class="dropdown-menu">
  224. <li><a href="profile.php"><span class="glyphicon glyphicon-user"></span>&nbsp;View Profile</a></li>
  225. <li><a href="logout.php?logout=true"><span class="glyphicon glyphicon-log-out"></span>&nbsp;Sign Out</a></li>
  226. </ul>
  227. </li>
  228. </ul>
  229. </div><!--/.nav-collapse -->
  230. </div>
  231. </nav>
  232.  
  233.  
  234. <div class="clearfix"></div>
  235.  
  236.  
  237. <div class="container-fluid" style="margin-top:80px;">
  238.  
  239. <div class="container">
  240.  
  241. <label class="h5">welcome : <?php print($userRow['user_name']); ?></label>
  242. <hr />
  243.  
  244. <h1>
  245. <a href="home.php"><span class="glyphicon glyphicon-home"></span> home</a> &nbsp;
  246. <a href="profile.php"><span class="glyphicon glyphicon-user"></span> profile</a></h1>
  247. <hr />
  248.  
  249. <p class="h4">User Home Page</p>
  250.  
  251.  
  252. <p class="blockquote-reverse" style="margin-top:200px;">
  253. website themes<br /><br />
  254. <a href="link4">login here</a>
  255. </p>
  256.  
  257. </div>
  258.  
  259. </div>
  260.  
  261. <script src="bootstrap/js/bootstrap.min.js"></script>
  262.  
  263. </body>
  264. </html>
  265.  
  266. file 5 : index.php
  267.  
  268. <?php
  269. session_start();
  270. require_once("class.user.php");
  271. $login = new USER();
  272.  
  273. if($login->is_loggedin()!="")
  274. {
  275. $login->redirect('home.php');
  276. }
  277.  
  278. if(isset($_POST['btn-login']))
  279. {
  280. $uname = strip_tags($_POST['txt_uname_email']);
  281. $umail = strip_tags($_POST['txt_uname_email']);
  282. $upass = strip_tags($_POST['txt_password']);
  283.  
  284. if($login->doLogin($uname,$umail,$upass))
  285. {
  286. $login->redirect('home.php');
  287. }
  288. else
  289. {
  290. $error = "Wrong Details !";
  291. }
  292. }
  293. ?>
  294. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  295. <html xmlns="http://www.w3.org/1999/xhtml">
  296. <head>
  297. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  298. <title>Coding Cage : Login</title>
  299. <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
  300. <link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
  301. <link rel="stylesheet" href="style.css" type="text/css" />
  302. </head>
  303. <body>
  304.  
  305. <div class="signin-form">
  306.  
  307. <div class="container">
  308.  
  309.  
  310. <form class="form-signin" method="post" id="login-form">
  311.  
  312. <h2 class="form-signin-heading">Log In to WebApp.</h2><hr />
  313.  
  314. <div id="error">
  315. <?php
  316. if(isset($error))
  317. {
  318. ?>
  319. <div class="alert alert-danger">
  320. <i class="glyphicon glyphicon-warning-sign"></i> &nbsp; <?php echo $error; ?> !
  321. </div>
  322. <?php
  323. }
  324. ?>
  325. </div>
  326.  
  327. <div class="form-group">
  328. <input type="text" class="form-control" name="txt_uname_email" placeholder="Username or E mail ID" required />
  329. <span id="check-e"></span>
  330. </div>
  331.  
  332. <div class="form-group">
  333. <input type="password" class="form-control" name="txt_password" placeholder="Your Password" />
  334. </div>
  335.  
  336. <hr />
  337.  
  338. <div class="form-group">
  339. <button type="submit" name="btn-login" class="btn btn-default">
  340. <i class="glyphicon glyphicon-log-in"></i> &nbsp; SIGN IN
  341. </button>
  342. </div>
  343. <br />
  344. <label>Don't have account yet ! <a href="sign-up.php">Sign Up</a></label>
  345. </form>
  346.  
  347. </div>
  348.  
  349. </div>
  350.  
  351. </body>
  352. </html>
  353.  
  354. file 6 : logout.php
  355.  
  356. <?php
  357. require_once('session.php');
  358. require_once('class.user.php');
  359. $user_logout = new USER();
  360.  
  361. if($user_logout->is_loggedin()!="")
  362. {
  363. $user_logout->redirect('home.php');
  364. }
  365. if(isset($_GET['logout']) && $_GET['logout']=="true")
  366. {
  367. $user_logout->doLogout();
  368. $user_logout->redirect('index.php');
  369. }
  370.  
  371. file 7 : profile .php
  372.  
  373. <?php
  374.  
  375. require_once("session.php");
  376.  
  377. require_once("class.user.php");
  378. $auth_user = new USER();
  379.  
  380.  
  381. $user_id = $_SESSION['user_session'];
  382.  
  383. $stmt = $auth_user->runQuery("SELECT * FROM users WHERE user_id=:user_id");
  384. $stmt->execute(array(":user_id"=>$user_id));
  385.  
  386. $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
  387.  
  388. ?>
  389. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  390. <html xmlns="http://www.w3.org/1999/xhtml">
  391. <head>
  392. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  393. <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
  394. <link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
  395. <script type="text/javascript" src="jquery-1.11.3-jquery.min.js"></script>
  396. <link rel="stylesheet" href="style.css" type="text/css" />
  397. <title>welcome - <?php print($userRow['user_email']); ?></title>
  398. </head>
  399.  
  400. <body>
  401.  
  402.  
  403. <nav class="navbar navbar-default navbar-fixed-top">
  404. <div class="container">
  405. <div class="navbar-header">
  406. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
  407. <span class="sr-only">Toggle navigation</span>
  408. <span class="icon-bar"></span>
  409. <span class="icon-bar"></span>
  410. <span class="icon-bar"></span>
  411. </button>
  412. <a class="navbar-brand" href="http://oursite.com">Coding Cage</a>
  413. </div>
  414. <div id="navbar" class="navbar-collapse collapse">
  415. <ul class="nav navbar-nav">
  416. <li class="active"><a href="link8">Back to site</a></li>
  417. <li><a href="link9">j</a></li>
  418. <li><a href="link10">P</a></li>
  419. </ul>
  420. <ul class="nav navbar-nav navbar-right">
  421.  
  422. <li class="dropdown">
  423. <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
  424. <span class="glyphicon glyphicon-user"></span>&nbsp;Hi' <?php echo $userRow['user_email']; ?>&nbsp;<span class="caret"></span></a>
  425. <ul class="dropdown-menu">
  426. <li><a href="#"><span class="glyphicon glyphicon-user"></span>&nbsp;View Profile</a></li>
  427. <li><a href="logout.php?logout=true"><span class="glyphicon glyphicon-log-out"></span>&nbsp;Sign Out</a></li>
  428. </ul>
  429. </li>
  430. </ul>
  431. </div><!--/.nav-collapse -->
  432. </div>
  433. </nav>
  434.  
  435. <div class="clearfix"></div>
  436.  
  437. <div class="container-fluid" style="margin-top:80px;">
  438.  
  439. <div class="container">
  440.  
  441. <label class="h5">welcome : <?php print($userRow['user_name']); ?></label>
  442. <hr />
  443.  
  444. <h1>
  445. <a href="home.php"><span class="glyphicon glyphicon-home"></span> home</a> &nbsp;
  446. <a href="profile.php"><span class="glyphicon glyphicon-user"></span> profile</a></h1>
  447. <hr />
  448.  
  449. <p class="h4">Another Secure Profile Page</p>
  450.  
  451. <p class="blockquote-reverse" style="margin-top:200px;">
  452. nice<br /><br />
  453. <a href="link5">site link</a>
  454. </p>
  455.  
  456. </div>
  457.  
  458. </div>
  459.  
  460.  
  461.  
  462.  
  463. <script src="bootstrap/js/bootstrap.min.js"></script>
  464.  
  465. </body>
  466. </html>
  467.  
  468. file 8 : session.php
  469.  
  470. <?php
  471.  
  472. session_start();
  473.  
  474. require_once 'class.user.php';
  475. $session = new USER();
  476.  
  477. // if user session is not active(not loggedin) this page will help 'home.php and profile.php' to redirect to login page
  478. // put this file within secured pages that users (users can't access without login)
  479.  
  480. if(!$session->is_loggedin())
  481. {
  482. // session no set redirects to login page
  483. $session->redirect('index.php');
  484. }
  485.  
  486. file 9 : signup.php
  487.  
  488. <?php
  489. session_start();
  490. require_once('class.user.php');
  491. $user = new USER();
  492.  
  493. if($user->is_loggedin()!="")
  494. {
  495. $user->redirect('home.php');
  496. }
  497.  
  498. if(isset($_POST['btn-signup']))
  499. {
  500. $uname = strip_tags($_POST['txt_uname']);
  501. $umail = strip_tags($_POST['txt_umail']);
  502. $upass = strip_tags($_POST['txt_upass']);
  503.  
  504. if($uname=="") {
  505. $error[] = "provide username !";
  506. }
  507. else if($umail=="") {
  508. $error[] = "provide email id !";
  509. }
  510. else if(!filter_var($umail, FILTER_VALIDATE_EMAIL)) {
  511. $error[] = 'Please enter a valid email address !';
  512. }
  513. else if($upass=="") {
  514. $error[] = "provide password !";
  515. }
  516. else if(strlen($upass) < 6){
  517. $error[] = "Password must be atleast 6 characters";
  518. }
  519. else
  520. {
  521. try
  522. {
  523. $stmt = $user->runQuery("SELECT user_name, user_email FROM users WHERE user_name=:uname OR user_email=:umail");
  524. $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
  525. $row=$stmt->fetch(PDO::FETCH_ASSOC);
  526.  
  527. if($row['user_name']==$uname) {
  528. $error[] = "sorry username already taken !";
  529. }
  530. else if($row['user_email']==$umail) {
  531. $error[] = "sorry email id already taken !";
  532. }
  533. else
  534. {
  535. if($user->register($uname,$umail,$upass)){
  536. $user->redirect('sign-up.php?joined');
  537. }
  538. }
  539. }
  540. catch(PDOException $e)
  541. {
  542. echo $e->getMessage();
  543. }
  544. }
  545. }
  546.  
  547. ?>
  548. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  549. <html xmlns="http://www.w3.org/1999/xhtml">
  550. <head>
  551. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  552. <title>Coding Cage : Sign up</title>
  553. <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
  554. <link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
  555. <link rel="stylesheet" href="style.css" type="text/css" />
  556. </head>
  557. <body>
  558.  
  559. <div class="signin-form">
  560.  
  561. <div class="container">
  562.  
  563. <form method="post" class="form-signin">
  564. <h2 class="form-signin-heading">Sign up.</h2><hr />
  565. <?php
  566. if(isset($error))
  567. {
  568. foreach($error as $error)
  569. {
  570. ?>
  571. <div class="alert alert-danger">
  572. <i class="glyphicon glyphicon-warning-sign"></i> &nbsp; <?php echo $error; ?>
  573. </div>
  574. <?php
  575. }
  576. }
  577. else if(isset($_GET['joined']))
  578. {
  579. ?>
  580. <div class="alert alert-info">
  581. <i class="glyphicon glyphicon-log-in"></i> &nbsp; Successfully registered <a href='index.php'>login</a> here
  582. </div>
  583. <?php
  584. }
  585. ?>
  586. <div class="form-group">
  587. <input type="text" class="form-control" name="txt_uname" placeholder="Enter Username" value="<?php if(isset($error)){echo $uname;}?>" />
  588. </div>
  589. <div class="form-group">
  590. <input type="text" class="form-control" name="txt_umail" placeholder="Enter E-Mail ID" value="<?php if(isset($error)){echo $umail;}?>" />
  591. </div>
  592. <div class="form-group">
  593. <input type="password" class="form-control" name="txt_upass" placeholder="Enter Password" />
  594. </div>
  595. <div class="clearfix"></div><hr />
  596. <div class="form-group">
  597. <button type="submit" class="btn btn-primary" name="btn-signup">
  598. <i class="glyphicon glyphicon-open-file"></i>&nbsp;SIGN UP
  599. </button>
  600. </div>
  601. <br />
  602. <label>have an account ! <a href="index.php">Sign In</a></label>
  603. </form>
  604. </div>
  605. </div>
  606.  
  607. </div>
  608.  
  609. </body>
  610. </html>
Add Comment
Please, Sign In to add comment