Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Ostatnie logowanie (data i ip)
Forum PHP.pl > Forum > Przedszkole
Napster
Witam. Mam problem ze zrobieniem ostatniego logowania (data i ip). Gdy poprawnie zalogujemy się poprzez formularz:

  1. <?php
  2. $data = date("Y-m-d");
  3. $czas = date("G:i");
  4. $ip = $_SERVER['REMOTE_ADDR'];
  5.  
  6.  
  7. $lastlogin = mysql_query
  8. ("UPDATE uzytkownicy SET `lastlogin`='$data ' '$czas' WHERE `login`='$usname' LIMIT 1");
  9.  
  10. $lastip = mysql_query
  11. ("UPDATE uzytkownicy SET `lastip`='$ip' WHERE `login`='$usname' LIMIT 1");
  12. ?>


to aktualizauje się kolumna lastlogin i lastip do aktualnych danych, po czym są wyświetlane w panelu użytkownika.
To bezsensu bo zawsze będzie pojawiać się aktualna data a nie np. data logowania i ip z wczoraj czy chociażby kilka godzin wcześniej.

Jak to rozwiązać?
rybik
rozwiązać tam gdzie problem, problemem jest to, że zapisujesz inną datę niż wyświeltasz, zatem masz dwie daty, czyli musisz zapisać obie, czyli pamiętać dwa ostatnie logowania

1. Przepisz nowe na stare, zapamiętaj
2. Nadpisz nowe
Crozin
Przed uaktualnieniem tych danych musisz je sobie pobrać
  1. <?
  2.  
  3. //gdy uzytkownik poprawnie przeszedl etap logowania
  4. $r = mysql_query('SELECT lastlogin, lastip FROM uzytkownicy WHERE userID = xxxx');
  5. $_SESSION['lastlogin'] = $r['lastlogin'];
  6. $_SESSION['lastip'] = $r['lastlip'];
  7.  
  8. mysql_query('UPDATE uzytkownicy SET lastip = "' . $_SERVER['REMOTE_ADDR'] . '", lastlogin = ' . date('Y-m-d G:i') . ' WHERE userID = xxx');
  9. ?>
Napster
Za bardzo nie rozumiem, kiedy ma być zmieniana data?
Crozin
Najpierw pobierasz sobie do zmiennych sesyjnych dane o ostatnim logowaniu (data i ip) a dopiero potem to nadpisujesz.
woxala123
No dobrze! Ja mam podobny temat ale jak je wyswietlić?
com
sprecyzuj i daj kod smile.gif
woxala123
Witam Ciebie com. Temat podobny jest tutaj i kod też Temat: PHPWylogowanie i zapisanie do bazy danych ostatniego logowania. Zależy mnie by w profil.php gdzie jest zalogowany user wyświetlał mi datę ostatniego logowania. Dodam jeszcze plik profile.php
  1. <?php require_once('Connections/local.php'); ?>
  2. <?php
  3. if (!isset($_SESSION)) {
  4. }
  5. $MM_authorizedUsers = "";
  6. $MM_donotCheckaccess = "true";
  7.  
  8. if (isset($_SESSION['username']))
  9. ?>
  10.  
  11.  
  12.  
  13. <?php echo $_SESSION['username'];
  14.  
  15. // *** Restrict Access To Page: Grant or deny access to this page
  16. function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
  17. // For security, start by assuming the visitor is NOT authorized.
  18. $isValid = False;
  19.  
  20. // When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
  21. // Therefore, we know that a user is NOT logged in if that Session variable is blank.
  22. if (!empty($UserName)) {
  23. // Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
  24. // Parse the strings into arrays.
  25. $arrUsers = Explode(",", $strUsers);
  26. $arrGroups = Explode(",", $strGroups);
  27. if (in_array($UserName, $arrUsers)) {
  28. $isValid = true;
  29. }
  30. // Or, you may restrict access to only certain users based on their username.
  31. if (in_array($UserGroup, $arrGroups)) {
  32. $isValid = true;
  33. }
  34. if (($strUsers == "") && true) {
  35. $isValid = true;
  36. }
  37. }
  38. return $isValid;
  39. }
  40.  
  41. $MM_restrictGoTo = "register.php";
  42. if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
  43. $MM_qsChar = "?";
  44. $MM_referrer = $_SERVER['PHP_SELF'];
  45. if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  46. if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0)
  47. $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
  48. $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  49. header("Location: ". $MM_restrictGoTo);
  50. }
  51. ?>
  52.  
  53. <?php echo $_SESSION['lastlogin']; ?>
  54.  
  55. <?php
  56. if (!function_exists("GetSQLValueString")) {
  57. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  58. {
  59. if (PHP_VERSION < 6) {
  60. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  61. }
  62.  
  63. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  64.  
  65. switch ($theType) {
  66. case "text":
  67. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  68. break;
  69. case "long":
  70. case "int":
  71. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  72. break;
  73. case "double":
  74. $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  75. break;
  76. case "date":
  77. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  78. break;
  79. case "defined":
  80. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  81. break;
  82. }
  83. return $theValue;
  84. }
  85. }
  86.  
  87. $colname_Recordset1 = "-1";
  88. if (isset($_SESSION['MM_Username'])) {
  89. $colname_Recordset1 = $_SESSION['MM_Username'];
  90. }
  91. mysql_select_db($database_local, $local);
  92. $query_Recordset1 = sprintf("SELECT * FROM userlogin WHERE username = %s", GetSQLValueString($colname_Recordset1, "text"));
  93. $Recordset1 = mysql_query($query_Recordset1, $local) or die(mysql_error());
  94. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  95. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  96.  
  97. $colname_Recordset2 = "-1";
  98. if (isset($_SESSION['lastlogin'])) {
  99. $colname_Recordset2 = $_SESSION['lastlogin'];
  100. }
  101. mysql_select_db($database_local, $local);
  102. $query_Recordset2 = sprintf("SELECT lastlogin FROM userlogin WHERE lastlogin = %s", GetSQLValueString($colname_Recordset2, "date"));
  103. $Recordset2 = mysql_query($query_Recordset2, $local) or die(mysql_error());
  104. $row_Recordset2 = mysql_fetch_assoc($Recordset2);
  105. $totalRows_Recordset2 = mysql_num_rows($Recordset2);
  106.  
com
no to poco 2 tematy?
woxala123
Szukałem i tutaj rozwiązania i chciałem ten temat trochę ruszyć. Ale możemy wrócić do tego 1 tematu.
com
tu jest odpowiedź na twoje pytania dla mysql, tam gdzie masz w pdo wstaw to w mysql i gotowe biggrin.gif
woxala123
Nie wiem jak wstawić pdo w mysql. Tym zajmę się sie póżniej. Na razie szukam rozwiązania w mysql. Pozdrawiam
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.