Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Wylogowanie i zapisanie do bazy danych ostatniego logowania.
Forum PHP.pl > Forum > Przedszkole
woxala123
Witam.
Chcę wam sie poradzić w jednej rzeczy. Zrobiłem taki plik wylogowania i chciałbym aby zapisywał datę jego wylogowania w kolumnie endlogin. Wiem że muszę tu update napisać
tylko jak? Jest jeszcze plik login.php w którym owszem zapisuje datę logowania ale może tu jest jeszcze opcja żeby zapisywał datę ostatniego logowania w kolumnie endlogin.
  1.  
  2. login.php
  3.  
  4. [php]
  5. <?php require_once('Connections/local.php'); ?>
  6. <?php
  7. if (!function_exists("GetSQLValueString")) {
  8. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  9. {
  10. if (PHP_VERSION < 6) {
  11. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  12. }
  13.  
  14. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  15.  
  16. switch ($theType) {
  17. case "text":
  18. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  19. break;
  20. case "long":
  21. case "int":
  22. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  23. break;
  24. case "double":
  25. $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  26. break;
  27. case "date":
  28. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  29. break;
  30. case "defined":
  31. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  32. break;
  33. }
  34. return $theValue;
  35. }
  36. }
  37.  
  38. mysql_select_db($database_local, $local);
  39. $query_Login = "SELECT * FROM userlogin";
  40. $Login = mysql_query($query_Login, $local) or die(mysql_error());
  41. $row_Login = mysql_fetch_assoc($Login);
  42. $totalRows_Login = mysql_num_rows($Login);
  43. ?>
  44. <?php
  45. // *** Validate request to login to this site.
  46. if (!isset($_SESSION)) {
  47. }
  48.  
  49. $loginFormAction = $_SERVER['PHP_SELF'];
  50. if (isset($_GET['accesscheck'])) {
  51. $_SESSION['PrevUrl'] = $_GET['accesscheck'];
  52. }
  53.  
  54. if (isset($_POST['UserName'])) {
  55. $loginUsername=$_POST['UserName'];
  56. $password=$_POST['Password'];
  57. $MM_fldUserAuthorization = "";
  58. $MM_redirectLoginSuccess = "profile.php";
  59. $MM_redirectLoginFailed = "register.php";
  60. $MM_redirecttoReferrer = false;
  61. mysql_select_db($database_local, $local);
  62.  
  63. $LoginRS__query=sprintf("SELECT username, password FROM userlogin WHERE username=%s AND password=%s",
  64. GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
  65.  
  66. $LoginRS = mysql_query($LoginRS__query, $local) or die(mysql_error());
  67. $loginFoundUser = mysql_num_rows($LoginRS);
  68. if ($loginFoundUser) {
  69. $loginStrGroup = "";
  70.  
  71. if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
  72. //declare two session variables and assign them
  73. $_SESSION['MM_Username'] = $loginUsername;
  74. $_SESSION['MM_UserGroup'] = $loginStrGroup;
  75. $ip = $_SERVER['REMOTE_ADDR'];
  76. $update = "UPDATE userlogin SET lastlogin = NOW() , lastip = '$ip' WHERE username = '$loginUsername'";
  77. $result = mysql_query($update) or die (mysql_error());
  78.  
  79. if (isset($_SESSION['PrevUrl']) && false) {
  80. $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
  81. }
  82. header("Location: " . $MM_redirectLoginSuccess );
  83. }
  84. else {
  85. header("Location: ". $MM_redirectLoginFailed );
  86. }
  87. }
  88. ?>


logout.php

  1. <?php require_once('Connections/local.php'); ?>
  2. <?php
  3. // *** Logout the current user.
  4. $logoutGoTo = "index.php";
  5. if (!isset($_SESSION)) {
  6. }
  7. $_SESSION['MM_Username'] = NULL;
  8. $_SESSION['MM_UserGroup'] = NULL;
  9. unset($_SESSION['MM_Username']);
  10. unset($_SESSION['MM_UserGroup']);
  11. if ($logoutGoTo != "") {header("Location: $logoutGoTo");
  12. }
  13. ?>
  14. <?php
  15. if (!function_exists("GetSQLValueString")) {
  16. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  17. {
  18. if (PHP_VERSION < 6) {
  19. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  20. }
  21.  
  22. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  23.  
  24. switch ($theType) {
  25. case "text":
  26. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  27. break;
  28. case "long":
  29. case "int":
  30. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  31. break;
  32. case "double":
  33. $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  34. break;
  35. case "date":
  36. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  37. break;
  38. case "defined":
  39. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  40. break;
  41. }
  42. return $theValue;
  43. }
  44. }
  45.  
  46. mysql_select_db($database_local, $local);
  47. $query_Recordset1 = "SELECT * FROM userlogin";
  48. $Recordset1 = mysql_query($query_Recordset1, $local) or die(mysql_error());
  49. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  50. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  51.  
  52. mysql_free_result($Recordset1);
  53. ?>
  54.  


Mam takie zapytanie czy ciężko jest tu komuś podpowiedzieć? Wiem tylko tyle że można to zapisać w sesji a potem to wyświetlić tylko nie wiem w którym miejscu dac zmienne sesji na lastlogin i potem je wyświetlić. Może jeszcze dam plik profil.php pozdrawiam
vokiel
Ale chcesz zapisywać datę ostatniego logowania czy wylogowania (endlogin sugeruje jako koniec aktywności)?

1. Ostatniego logowania - czyli np w celu wyświetlenia na stronie po zalogowaniu daty poprzedniego (ostatniego) logowania
2. Daty wyglowania - żeby sobie policzyć czas trwania sesji czy czas spędzony w systemie.

Ad. 1.
W pliku logout.php po session_start() linia 8 wstawiasz zapytanie w stylu:
  1. $sql = "UPDATE users SET endlogin = lastlogin WHERE username = :username";
  2. $stmt = $pdo->prepare($sql);
  3. $stmt->bindParam(':username', $_SESSION['MM_Username'], PDO::PARAM_STR);
  4. $stmt->execute();


Ad. 2.
W pliku logout.php po session_start() linia 8 wstawiasz zapytanie w stylu:
  1. $sql = "UPDATE users SET endlogin = :datetime WHERE username = :username";
  2. $stmt = $pdo->prepare($sql);
  3. $stmt->bindParam(':datetime', date('Y-m-d H:i:s'), PDO::PARAM_STR);
  4. $stmt->bindParam(':username', $_SESSION['MM_Username'], PDO::PARAM_STR);
  5. $stmt->execute();


P.S. mysql_* jest wycofywany, nie zaleca się korzystanie z niego, zatem proponuję przesiadkę na PDO.
Cytat
Please, do not use the mysql extension for new developments.
woxala123
powiem tak robie to w dreamwearze i dobrze by było to jeszcze kontynuować w msql. Ale prosiłbym tu o podpowiedż w myqsl
com
a co ma do tego ide? kolega dobrze sugeruje, zmień puki możesz na pdo lub mysqli smile.gif a odp masz do tego przecież w drugim temacie już smile.gif
woxala123
Okey masz rację. Proszę o przykłąd wstawienia pdo w mysql.

Ciężko to już dziś idzie. Tracę myśl: ze to dziś rozwiąże

No dobra zacząłem idż tą drogą że dopisałem w pliku login.php jedną linijkę
  1. $_SESSION['MM_Username'] = $loginUsername;
  2. $_SESSION['MM_UserGroup'] = $loginStrGroup;
  3. $_SESSION['lastlogin'] = $lastlogin;
  4. $ip = $_SERVER['REMOTE_ADDR'];
  5. $update = "UPDATE userlogin SET lastlogin = NOW() , lastip = '$ip' WHERE username = '$loginUsername'";
  6. $result = mysql_query($update) or die (mysql_error());

Wcześniej było bez tego
$_SESSION['ip'] = $ip;
$_SESSION['lastlogin'] = $lastlogin;

czyli pobieram wcześniejszą datę i co dalej żeby wyświetlał datę ostatniego logowania a nie bieżącą.?

I nadal nikt nie umie poprawnie nakierować. Proszę poradę.



Witam. Jak wstawię ten pierwszy plik to wywalaja błędy.
Fatal error: Call to a member function prepare() on null in D:\xampp\htdocs\livebet\logout.php on line 10
a plik po wstawieniu wygląda tak

  1. pobierz, plaintext
  2. [php]
  3. <?php require_once('Connections/local.php'); ?>
  4. <?php
  5. // *** Logout the current user.
  6. $logoutGoTo = "login.php";
  7. if (!isset($_SESSION)) {
  8.  
  9. }
  10. $sql = "UPDATE userlogin SET endlogin = lastlogin WHERE username = :username";
  11. $stmt = $pdo->prepare($sql);
  12. $stmt->bindParam(':username', $_SESSION['MM_Username'], PDO::PARAM_STR);
  13. $stmt->execute();
  14. $_SESSION['MM_Username'] = NULL;
  15. $_SESSION['MM_UserGroup'] = NULL;
  16. unset($_SESSION['MM_Username']);
  17. unset($_SESSION['MM_UserGroup']);
  18. if ($logoutGoTo != "") {header("Location: $logoutGoTo");
  19. }
  20. ?>
  21. <?php
  22. if (!function_exists("GetSQLValueString")) {
  23. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  24. {
  25. if (PHP_VERSION < 6) {
  26. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  27. }
  28.  
  29. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  30.  
  31. switch ($theType) {
  32. case "text":
  33. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  34. break;
  35. case "long":
  36. case "int":
  37. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  38. break;
  39. case "double":
  40. $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  41. break;
  42. case "date":
  43. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  44. break;
  45. case "defined":
  46. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  47. break;
  48. }
  49. return $theValue;
  50. }
  51. }
  52.  
  53. mysql_select_db($database_local, $local);
  54. $query_Recordset1 = "SELECT * FROM userlogin";
  55. $Recordset1 = mysql_query($query_Recordset1, $local) or die(mysql_error());
  56. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  57. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  58. ?>
  59. <form id="form1" name="form1" method="post" action="">
  60. <input type="hidden" name="hiddenField" id="hiddenField" />
  61. </form>
  62. <?php
  63. mysql_free_result($Recordset1);
  64. ?>




com
no masz odp vokiel tylko zamiast
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':datetime', date('Y-m-d H:i:s'), PDO::PARAM_STR);
$stmt->bindParam(':username', $_SESSION['MM_Username'], PDO::PARAM_STR);
$stmt->execute(); robisz to w mysql i tyle wink.gif
woxala123
com nie potrafie tego zrobić w mysql. Sorry nie wiem jak to przerobić.
com
no do zapytania wstawiasz zamiast :datetime zmienna w której jest date('Y-m-d H:i:s') i analogicznie dla reszty wink.gif

$sql = "UPDATE users SET endlogin = :datetime WHERE username = :username"; Zapytanie tak wygl, w mysql bd miał
$datatime = date('Y-m-d H:i:s');
$sql = "UPDATE users SET endlogin = $datetime WHERE username = '$username'";

itd

polecam w takim razie manual, ale skoro już będziesz się uczyć to naucz sie pdo/mysqli smile.gif
woxala123
Dzięki już za małe wskazóki zrobiłem już tak w pliku logout.php. Ale nadal nie zapisuje przy wylogowaniu datę w endlogin.
  1. <?php
  2. // *** Logout the current user.
  3. $logoutGoTo = "login.php";
  4. if (!isset($_SESSION)) {
  5. $datetime = date('Y-m-d H:i:s');
  6. $username = $_SESSION['MM_Username'];
  7. $sql = "UPDATE userlogin SET endlogin = $datetime WHERE username = '$username'";
  8. }
  9.  
  10. $_SESSION['MM_Username'] = NULL;
  11. $_SESSION['MM_UserGroup'] = NULL;
  12. unset($_SESSION['MM_Username']);
  13. unset($_SESSION['MM_UserGroup']);
  14. if ($logoutGoTo != "") {header("Location: $logoutGoTo");
  15. }
  16. ?>
  17. <?php
  18. if (!function_exists("GetSQLValueString")) {
  19. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  20. {
  21. if (PHP_VERSION < 6) {
  22. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  23. }
  24.  
  25. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  26.  
  27. switch ($theType) {
  28. case "text":
  29. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  30. break;
  31. case "long":
  32. case "int":
  33. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  34. break;
  35. case "double":
  36. $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  37. break;
  38. case "date":
  39. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  40. break;
  41. case "defined":
  42. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  43. break;
  44. }
  45. return $theValue;
  46. }
  47. }
  48.  
  49. mysql_select_db($database_local, $local);
  50. $query_Recordset1 = "SELECT * FROM userlogin";
  51. $Recordset1 = mysql_query($query_Recordset1, $local) or die(mysql_error());
  52. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  53. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  54. ?>
vokiel
Cytat(woxala123 @ 31.01.2016, 23:04:06 ) *
Dzięki już za małe wskazóki zrobiłem już tak w pliku logout.php. Ale nadal nie zapisuje przy wylogowaniu datę w endlogin.


A jaki błąd wyświetla?

Może składniowy w zapytaniu?
  1. // Masz
  2. $sql = "UPDATE userlogin SET endlogin = $datetime WHERE username = '$username'";
  3. // Sprawdź
  4. $sql = "UPDATE userlogin SET endlogin = '".$datetime."' WHERE username = '".$username."';";



woxala123
Zrobiłęm tak z Waszą pomocą i bardzo dziękuje. zapisuje to jak ulał.

  1. if (!isset($_SESSION)) {
  2. $datetime = date('Y-m-d H:i:s');
  3. $username = $_SESSION['MM_Username'];
  4. $update = "UPDATE userlogin SET endlogin = '$datetime' WHERE username = '$username'";
  5. $result = mysql_query($update) or die (mysql_error());


Okey vokiel jeszzce trochę podciągnę temat bo teraz pokazuje kiedy się wylogował ale jak to zrobić kiedy się zalogował? C\y coś podobnego dać w pliku profile.php?

Dobra to już też rozwiązałem. poprostu endlogin aktualizuje się z lastlogin. Kuśwa ale to teraz wydaje się łatwe .....

Okey mam jeszcze jedno zapytanie. Chce dodać z jakiej strony user został wylogowany i stworzyłem taki kod w logout.php
  1. $logrefe = $_SERVER['REQUEST_URI'];
  2. $datetime = date('Y-m-d H:i:s');
  3. $username = $_SESSION['MM_Username'];
  4. $update = "UPDATE userlogin SET endlogin = lastlogin, logout = '$datetime', logrefer = '$logrefer') WHERE username = '$username'";
  5. $result = mysql_query($update) or die (mysql_error());
  6.  

Ale wyskakuje mi błąd
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') WHERE username = 'test'' at line 1
Stworzyłęm kolumnę refer niby nadałem zmienną i nie chcę zapisywać. Gdzie robie błąd?
nospor
A po co dales tam to zamkniecie nawiasu ) ? Przeciez nawet komunikat bledu wyraznie ci pokazuje ze tam masz blad. No czytaj bledy co dostajesz
woxala123
Właśnie czytałem tylko kuna nie wychwyciłem nawiasu. Wielkie dzięki nospor. Szybki jesteś w odpowiedziach i taki człowiek jest na właściwym miejscu.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.