Advertisement
Guest User

Untitled

a guest
Nov 8th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. <?php
  2.  
  3. function dbConnect(){
  4. try
  5. {
  6. $db = new PDO('mysql:host=localhost;dbname=armada;charset=utf8', 'root', '');
  7. return $db;
  8. }
  9. catch(Exception $e)
  10. {
  11. die('Erreur : '.$e->getMessage());
  12. }
  13. }
  14.  
  15. function checkUserPresence($mail){
  16. $presenceUser = false;
  17.  
  18. $db = dbConnect();
  19. $request = $db->prepare('SELECT mail FROM user WHERE mail = ?');
  20. $request->execute(array($mail));
  21.  
  22. while ( $request->fetch() )
  23. {
  24. $presenceUser = true;
  25. }
  26.  
  27. $request->closeCursor();
  28.  
  29. return $presenceUser;
  30. }
  31.  
  32. function addUser($mail, $password, $fName, $sName){
  33.  
  34. $db = dbConnect();
  35.  
  36. $request = $db->prepare("INSERT INTO user VALUES(0, :mail , :nom , :prenom , :passwordd ,null )");
  37.  
  38. $request->execute(array(
  39. 'mail' => $mail,
  40. 'nom' => $sName,
  41. 'prenom' => $fName,
  42. 'passwordd' => password_hash($password, PASSWORD_DEFAULT),
  43.  
  44. ));
  45.  
  46.  
  47. $request->closeCursor();
  48.  
  49. }
  50.  
  51. function checkLogin($mail, $password, $passwordIsHashed){
  52.  
  53. $connexion = false;
  54. $db = dbConnect();
  55. $request = $db->prepare('SELECT password FROM user WHERE mail = ?');
  56. $request->execute(array($mail));
  57.  
  58.  
  59. if ( $data = $request->fetch() )
  60. {
  61. if($passwordIsHashed){
  62. if( $password == $data['password'] ){
  63. $connexion = true;
  64. setSessionInformation($mail);
  65. }
  66. }
  67. else{
  68. if( password_verify($password, $data['password']) ){
  69. $connexion = true;
  70. setSessionInformation($mail);
  71. }
  72. }
  73.  
  74. }
  75.  
  76. $request->closeCursor();
  77.  
  78. return $connexion;
  79. }
  80.  
  81.  
  82. function setSessionInformation($mail){
  83. $db = dbConnect();
  84. $request = $db->prepare('SELECT * FROM user WHERE mail = ?');
  85. $request->execute(array($mail));
  86.  
  87. $data = $request->fetch();
  88.  
  89. $_SESSION['id']=$data['id'];
  90. $_SESSION['mail']=$data['mail'];
  91. $_SESSION['name']=$data['nom'];
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement