phpmysqldatabaseopenidlightopenid

OpenID duplicate results in database


I am trying to make a webiste with Yahoo OpenID. Everything works fine until I log out and then log in again. In my sql database I get duplicated results. Only the hash is changed. It seems that $sql = $db->query("SELECT * FROM users WHERE steamid = '" . $steamid . "'"); can't find any users with the steamid to it creates a new entry in the db. I tried this with Steam too and it's working. With Yahoo, I get duplicate results with my email adress(here $steamid), and name(here $name). The hashes are different.

case 'login':
    include 'openid.php';
    try
    {
        $openid = new LightOpenID('http://'.$_SERVER['SERVER_NAME'].'/');
        if (!$openid->mode) {
            $openid->identity = 'https://me.yahoo.com/a/6eqERecwyZfHsDm6VBa7H2uWNu3W5.UvCw--'; //http://steamcommunity.com/openid/
            $openid->required = array(
                'contact/email',
                'namePerson',
            );
            header('Location: '.$openid->authUrl());
        } elseif ($openid->mode == 'cancel') {
            echo '';
        } else {
            if ($openid->validate()) {

                //$id = $openid->identity;
                //$ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
                //preg_match($ptn, $id, $matches);

                //$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=78DC279A43117B222DDEE0FCCCAD38FD&steamids=$matches[1]";
                //$json_object = file_get_contents($url);
                //$json_decoded = json_decode($json_object);
                //foreach ($json_decoded->response->players as $player) {
                    $data = $openid->getAttributes();
                    $steamid = $data['contact/email'];
                    $name = $data['namePerson'];
                    //$avatar = $player->avatar;
                //}

                $hash = md5($steamid . time() . rand(1, 50));
                $sql = $db->query("SELECT * FROM `users` WHERE `steamid` = '" . $steamid . "'");
                $row = $sql->fetchAll(PDO::FETCH_ASSOC);
                if (count($row) == 0) {
                    $db->exec("INSERT INTO `users` (`hash`, `steamid`, `name`) VALUES ('" . $hash . "', '" . $steamid . "', " . $db->quote($name) . ")");
                } else {
                    $db->exec("UPDATE `users` SET `hash` = '" . $hash . "', `name` = " . $db->quote($name) . "' WHERE `steamid` = '" . $steamid . "'");
                }
                setcookie('hash', $hash, time() + 3600 * 24 * 7, '/');
                header('Location: http://45.55.69.74/');
            }
        }
    } catch (ErrorException $e) {
        exit($e->getMessage());
    }
    break;

Solution

  • The problem was that the steamid length in the database was smaller. Everytime it checked if the entire steamid is the same as the smaller steamid.