Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. <?php
  2. global $my_var;
  3.  
  4. // login_functions.php
  5.  
  6. require('./db_data.php');
  7. session_start();
  8.  
  9. function isLoggedIn() {
  10. if (!isset($_SESSION['email'])) {
  11. return FALSE;
  12. }
  13. else {
  14.  
  15. global $my_var;
  16. $my_var = 'Logado como <i>' . $_SESSION['email'] .
  17. '</i>.' . ' <a href="?logout">Sair do site.</a></p>';
  18.  
  19. return TRUE;
  20. }
  21. }
  22.  
  23. function logUserIn() {
  24.  
  25. include('./formlogin.html.php');
  26.  
  27. db_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DBNAME) or
  28. die('Erro ao conectar com a base de dados.');
  29. /* Import $db_con from the function called above. */
  30. global $db_con;
  31.  
  32. if (!empty($_POST['email']) && !empty($_POST['password'])) {
  33.  
  34. $email = mysqli_real_escape_string($db_con, $_POST['email']);
  35. //var_dump($email);
  36. $password = mysqli_real_escape_string($db_con, md5($_POST['password']));
  37. //var_dump($password);
  38. $resutl_set = mysqli_query($db_con, "SELECT * FROM TBL_romario_admin
  39. WHERE email='$email' AND password='$password'");
  40.  
  41. if (mysqli_num_rows($resutl_set) <= 0) {
  42. $error = 'Login inválido.';
  43. include('./error.html.php');
  44. exit();
  45. }
  46. if (mysqli_num_rows($resutl_set) == 1) {
  47. $found_this_user = mysqli_fetch_array($resutl_set);
  48. $_SESSION['email'] = $found_this_user['email'];
  49. }
  50.  
  51. /* Don't put header('Location: ./') in the index.php. */
  52. header('Location: ./');
  53. }
  54. return TRUE;
  55. }
  56.  
  57. require_once('./db_data.php');
  58. /**
  59. * This function attempts to connect to a database, set the connecton encoding,
  60. * and select the database on the server. You must pass it the parameters:
  61. * 1. hostname - the hostname of the server.
  62. * 2. username - the user login on the server.
  63. * 3. password - the user password for the server.
  64. * 4. db_name - the database name to be used.
  65. **/
  66. function db_connect($db_hostname, $db_username, $db_password, $db_dbname) {
  67. /* To make $db_con available in the main script, since this is
  68. * a include file, and, by default $db_con would only be available
  69. * here, inside this function. */
  70. global $db_con;
  71. //$db_con = mysqli_connect('localhost', 'nando', 'senHa010');
  72. $db_con = mysqli_connect($db_hostname, $db_username, $db_password);
  73.  
  74. if (!$db_con) {
  75. $error = 'Não foi possível conectar a base de dados.';
  76. include('./error.html.php');
  77. exit();
  78. }
  79.  
  80. if (!mysqli_set_charset($db_con, 'utf8')) {
  81. $error = 'Não foi possíel estabelecer o encoding da conexão.';
  82. include('./error.html.php');
  83. exit();
  84. }
  85.  
  86. if (!mysqli_select_db($db_con, $db_dbname)) {
  87. $error = 'Impossível localizar database.';
  88. include('./error.html.php');
  89. exit();
  90. }
  91.  
  92. return TRUE;
  93. }
  94.  
  95. function logUserOut() {
  96. unset($_SESSION['email']);
  97. header('Location: ./');
  98. }
  99. if (isset($_GET['logout'])) {
  100. logUserOut();
  101. }
  102.  
  103. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement