( ! ) 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
<?php class session{ private $id; private $ip; private $browser; private $time; private $user; private $salt; public function __construct(){ $_COOKIE[SESSION_COOKIE] = ''; //sprawdzenie czy istnieje plik cookie. Jeśli nie ma, tworzone jest puste } else{ $this->newSession(); //Jeśli istnieje cookie sprawdzana jest jego długość i pochodzenie } } $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'); //pobieranie z bazy danych informacji o sesji np. id $stmt->bindValue(':sid', $_COOKIE[SESSION_COOKIE], PDO::PARAM_STR); $stmt->bindValue(':updated', time() - SESSION_COOKIE_EXPIRE, PDO::PARAM_INT); // porównanie czasu wygaśnięcia $stmt->bindValue(':info', $request->getInfo(), PDO::PARAM_STR); //porównanie wygenerowanego hash'a $stmt->bindValue(':ip', $request->getIp(), PDO::PARAM_STR); //porównanie danych z request $stmt->bindValue(':browser', $request->getBrowser(), PDO::PARAM_STR); $stmt->execute(); if($session = $stmt -> fetch(PDO::FETCH_ASSOC)){ //sprawdzenie, czy sesja została odnaleziona w bazie $stmt -> closeCursor(); $this->id = $_COOKIE[SESSION_COOKIE]; $this->salt = $session['salt_token']; //Jeśli sesja została odnaleziona przypisywane są parametry sesji PHP z bazy danych. $this->ip = $session['ip']; $this->browser = $session['browser']; $this->time = $session['updated_at']; $stmt = $pdo->prepare('UPDATE sessions SET updated_at = :time WHERE session_id = :sid'); $stmt->bindValue(':sid', $_COOKIE[SESSION_COOKIE], PDO::PARAM_STR); $stmt->execute(); if($session['user_id'] != 0){ // dla zalogowanego użytkownika $stmt = $pdo->prepare("SELECT login FROM users WHERE id = :uid"); $stmt->bindValue(":uid", $session['user_id'], PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetchAll(PDO::FETCH_ASSOC); $user->setLogin($row[0]['login']); } else{ $this->user = new user(true); } } else{ // Jeśli nie ma sesji w bazie, tworzona jest nowa. $stmt->closeCursor(); $this->newSession(); } } function newSession(){ //funkcja tworzaca nową sesję //tworzenie losowego identyfikatora sesji $this->id = random_session_id(); $this->salt = random_salt(10); //Wstawianie informacji o sesji do bazy danych: $stmt = $pdo->prepare('INSERT INTO sessions (session_id, updated_at, salt_token, user_id, uniq_info, browser, ip) VALUES (:session_id, :time, :salt, :user_id, :info, :browser, :ip)'); $stmt->bindValue(':session_id', $this->id, PDO::PARAM_STR); $stmt->bindValue(':salt', $this->salt, PDO::PARAM_STR); $stmt->bindValue(':user_id', 0, PDO::PARAM_INT); $stmt->bindValue(':info', $request->getInfo(), PDO::PARAM_STR); $stmt->bindValue(':browser', $request->getBrowser(), PDO::PARAM_STR); $stmt->bindValue(':ip', $request->getIp(), PDO::PARAM_STR); $stmt->execute(); $this->user = new user(true); } function updateSession(user $user){ //TWORZENIE NOWEGO SESJI DLA UŻYTKOWNIKA ZALOGOWANEGO $newId = random_session_id(); $newSalt = random_salt(10); $stmt = $pdo->prepare("UPDATE sessions SET salt_token = :salt, updated_at = :time, session_id = :newId, user_id = :uid WHERE session_id = :sid"); $stmt->bindValue(':salt', $newSalt, PDO::PARAM_STR); $stmt->bindValue(':newId', $newId, PDO::PARAM_INT); $stmt->bindValue(':uid', $user->getId(), PDO::PARAM_INT); $stmt->bindValue('sid', $this->id, PDO::PARAM_STR); $stmt->execute(); $this->id = $newId; $this->user = $user; } public function getSessionId(){ return $this->id; } public function getUser(){ return $this->user; } } ?>
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.