Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP]Uncaught exception 'PDOException'
kris01
post
Post #1





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 4.06.2015

Ostrzeżenie: (0%)
-----


Mam problem z błędem:

( ! ) SCREAM: Error suppression ignored for
( ! ) Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'ff89ccf6eb5cf4cbd6a3e85a6d952dee' for key 'uniq_info'' in C:\wamp\www\sklep\sessions.php on line 84
( ! ) PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'ff89ccf6eb5cf4cbd6a3e85a6d952dee' for key 'uniq_info' in C:\wamp\www\sklep\sessions.php on line 84


  1. <?php
  2.  
  3. class session{
  4.  
  5. private $id;
  6. private $ip;
  7. private $browser;
  8. private $time;
  9. private $user;
  10. private $salt;
  11.  
  12. public function __construct(){
  13. global $pdo, $request;
  14.  
  15. if(!isset($_COOKIE[SESSION_COOKIE])){
  16. $_COOKIE[SESSION_COOKIE] = ''; //sprawdzenie czy istnieje plik cookie. Jeśli nie ma, tworzone jest puste
  17. }
  18. else{
  19. if(strlen($_COOKIE[SESSION_COOKIE]) != SESSION_ID_LENGHT){
  20. $this->newSession(); //Jeśli istnieje cookie sprawdzana jest jego długość i pochodzenie
  21. }
  22. }
  23.  
  24. $stmt = $pdo->prepare('SELECT session_id, updated_at, salt_token, user_id, uniq_info, ip, browser FROM sessions WHERE session_id = :sid AND uniq_info = :info AND updated_at > :updated AND ip = :ip AND browser = :browser');
  25. //pobieranie z bazy danych informacji o sesji np. id
  26. $stmt->bindValue(':sid', $_COOKIE[SESSION_COOKIE], PDO::PARAM_STR);
  27. $stmt->bindValue(':updated', time() - SESSION_COOKIE_EXPIRE, PDO::PARAM_INT); // porównanie czasu wygaśnięcia
  28. $stmt->bindValue(':info', $request->getInfo(), PDO::PARAM_STR); //porównanie wygenerowanego hash'a
  29. $stmt->bindValue(':ip', $request->getIp(), PDO::PARAM_STR); //porównanie danych z request
  30. $stmt->bindValue(':browser', $request->getBrowser(), PDO::PARAM_STR);
  31. $stmt->execute();
  32.  
  33. if($session = $stmt -> fetch(PDO::FETCH_ASSOC)){ //sprawdzenie, czy sesja została odnaleziona w bazie
  34. $stmt -> closeCursor();
  35. $this->id = $_COOKIE[SESSION_COOKIE];
  36. $this->salt = $session['salt_token']; //Jeśli sesja została odnaleziona przypisywane są parametry sesji PHP z bazy danych.
  37. $this->ip = $session['ip'];
  38. $this->browser = $session['browser'];
  39. $this->time = $session['updated_at'];
  40.  
  41. setcookie(SESSION_COOKIE, $this->id, time() + SESSION_COOKIE_EXPIRE); //Aktualizowanie cookie w przypadku znalezienia istniejącego. Wstawiane jest id sesji i czas wygaśniecia.
  42.  
  43. $stmt = $pdo->prepare('UPDATE sessions SET updated_at = :time WHERE session_id = :sid');
  44. $stmt->bindValue(':sid', $_COOKIE[SESSION_COOKIE], PDO::PARAM_STR);
  45. $stmt->bindValue(':time', time(), PDO::PARAM_INT);
  46. $stmt->execute();
  47.  
  48. if($session['user_id'] != 0){
  49. // dla zalogowanego użytkownika
  50. $stmt = $pdo->prepare("SELECT login FROM users WHERE id = :uid");
  51. $stmt->bindValue(":uid", $session['user_id'], PDO::PARAM_INT);
  52. $stmt->execute();
  53.  
  54. $row = $stmt->fetchAll(PDO::FETCH_ASSOC);
  55. $user->setLogin($row[0]['login']);
  56. }
  57. else{
  58. $this->user = new user(true);
  59. }
  60. }
  61. else{ // Jeśli nie ma sesji w bazie, tworzona jest nowa.
  62. $stmt->closeCursor();
  63. $this->newSession();
  64. }
  65. }
  66.  
  67. function newSession(){ //funkcja tworzaca nową sesję
  68. global $pdo, $request;
  69. //tworzenie losowego identyfikatora sesji
  70. $this->id = random_session_id();
  71. $this->salt = random_salt(10);
  72. setcookie(SESSION_COOKIE, $this->id, time() + SESSION_COOKIE_EXPIRE);
  73.  
  74. //Wstawianie informacji o sesji do bazy danych:
  75. $stmt = $pdo->prepare('INSERT INTO sessions (session_id, updated_at, salt_token, user_id, uniq_info, browser, ip)
  76. VALUES (:session_id, :time, :salt, :user_id, :info, :browser, :ip)');
  77. $stmt->bindValue(':session_id', $this->id, PDO::PARAM_STR);
  78. $stmt->bindValue(':time', time(), PDO::PARAM_INT);
  79. $stmt->bindValue(':salt', $this->salt, PDO::PARAM_STR);
  80. $stmt->bindValue(':user_id', 0, PDO::PARAM_INT);
  81. $stmt->bindValue(':info', $request->getInfo(), PDO::PARAM_STR);
  82. $stmt->bindValue(':browser', $request->getBrowser(), PDO::PARAM_STR);
  83. $stmt->bindValue(':ip', $request->getIp(), PDO::PARAM_STR);
  84. $stmt->execute();
  85. $this->user = new user(true);
  86. }
  87.  
  88. function updateSession(user $user){
  89. global $pdo, $request;
  90. //TWORZENIE NOWEGO SESJI DLA UŻYTKOWNIKA ZALOGOWANEGO
  91. $newId = random_session_id();
  92. $newSalt = random_salt(10);
  93. setcookie(SESSION_COOKIE, $newId, time() + SESSION_COOKIE_EXPIRE);
  94.  
  95. $stmt = $pdo->prepare("UPDATE sessions SET salt_token = :salt, updated_at = :time, session_id = :newId, user_id = :uid WHERE session_id = :sid");
  96.  
  97. $stmt->bindValue(':salt', $newSalt, PDO::PARAM_STR);
  98. $stmt->bindValue(':time', time(), PDO::PARAM_INT);
  99. $stmt->bindValue(':newId', $newId, PDO::PARAM_INT);
  100. $stmt->bindValue(':uid', $user->getId(), PDO::PARAM_INT);
  101. $stmt->bindValue('sid', $this->id, PDO::PARAM_STR);
  102. $stmt->execute();
  103.  
  104. $this->id = $newId;
  105. $this->user = $user;
  106. }
  107. public function getSessionId(){
  108. return $this->id;
  109. }
  110.  
  111. public function getUser(){
  112. return $this->user;
  113. }
  114. }
  115. ?>


Ponieważ dopiero raczkuję w PHP, nie wiem jak rozwiązać problem. Przejrzałem kod kilka razy i wydaje się poprawny. Gdy w bazie MySQL nie ma wpisu sesji, wskazany bład nie występuje.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 16:06