I'm using the setcookie
php function to set some cookies in my browser.
I try to set cookies in my php code and then I check it using print_r($_COOKIE)
.
Cookies are not displayed, however if I try to set cookie in another file they
will be displayed correctly.
if (isset($_POST['username']) && isset($_POST['password']))
{
global $username,$password;
$username = $_POST['username'];
$password = sha1($_POST['password']);
setcookie('username', $username, time()+3600); //cookie not set
setcookie('password', $password, time()+3600); //cookie not set
$database = connect_to_database($db_path);
$result = $database->query("SELECT * FROM users WHERE username = '$username' AND password = '$password'");
while (true)
{
$response = $result->fetchArray(SQLITE3_ASSOC);
if (!$response)
{
unset($_COOKIE['username']);
unset($_COOKIE['password']);
break;
}
if (($response['username'] == $username) && $response['password'] == $password)
{
header("location: ../index.php");
}
}
}
I expected cookies to be set, but using print_r($_COOKIE);
returns me array()
Notes
- Cookies are allowd in my browser settings
$_POST['username']
&$_POST['password']
sent to this page via form in another page$_POST['username']
&$_POST['password']
are set with true value.