0

I followd a tutorial on the internet for a php inlog whit session but i want to add cookies to it only im struggling getting mine query items into the cookies

    if (count($errors) == 0) {
        $password = md5($password);
        $query = "SELECT * FROM users WHERE username='$username' AND 
        password='$password'";
        $results = mysqli_query($db, $query);
        if (mysqli_num_rows($results) == 1) {

            $secondQuery    = "SELECT * FROM users WHERE userid='$emptyUserid' 
            AND hash='$emptyHash'";
            $result         = mysqli_query($db,$secondQuery);
            $row            = mysqli_fetch_row($result);
            $emptyUserid    = $row['userid'];
            $emptyHash      = $row['hash'];
            setcookie("userid","$emptyUserid",time()+ (3600 * 24 * 7));
            setcookie("hash","$emptyHash",time()+ (3600 * 24 * 7));

            $_SESSION['username']   = $username;
            $_SESSION['success']    = "You are now logged in";
            header('location: index.php');
        }else {
            array_push($errors, "Wrong username/password combination");
        }
    }
}

im struggling whit the second query the other one works fine i need the cookies to later check on the webpage if the user closed there page if gis cookies matches the data base so i can remake his session whit out him having to relog in (its for school project im kinda new to php)

the issue is when i var dump or echo the cookies they are empty no matter what

  • 1
    Possible duplicate of [What is the difference between Sessions and Cookies in PHP?](https://stackoverflow.com/questions/6339783/what-is-the-difference-between-sessions-and-cookies-in-php) – abr Apr 16 '18 at 09:17
  • **Danger**: You are using [an unsuitable hashing algorithm](http://php.net/manual/en/faq.passwords.php) and need to [take better care](https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet) of your users' passwords. – Quentin Apr 16 '18 at 09:18
  • can you elaborate your question and tell us what issue exactly you are facing? – Lelio Faieta Apr 16 '18 at 09:19

0 Answers0