In virtually every browser I've tried, the cookie for my application is created normally and persists as it should. On Opera for android however, the browser doesn't see the cookie. Either it doesn't recognize it, the cookie isn't created, or it's deleted at some point. Does anybody see a problem with my code?
This is a snippet from the login page,
require("inc/dbinfo.inc");
$fc_username = trim($_POST['username']);
$fc_password = trim($_POST['password']);
$hashedPass = hash( "sha256", $fc_password );
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare(" << AUTHENTICATE USER >> ");
$stmt->execute(array($fc_username,$hashedPass));
// set the resulting array to associative
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetchAll();
if(sizeof($result) == 1){
// Login success!
// Bake up a cookie
$cookie_expire = time()+86400; // 24 hr life
//time() - 3600; // overdue expiration date. deletes cookie
$cookie_domain = $_SERVER['HTTP_HOST'];
setcookie("userid", $result[0]["id"], $cookie_expire, $cookie_domain);
setcookie("name", $result[0]["name"], $cookie_expire, $cookie_domain);
header("Location: welcome.php");
exit;
}else{
// Login failure
header("Location: login.php?msg=badlogin");
exit;
}
}catch(PDOException $e){
echo "Error: " . $e->getMessage();
}
$conn = null;
And from the homepage:
<?php
if(!isset($_COOKIE['name']) && !isset($_COOKIE['userid'])){
header("Location: login.php?msg=nocookie");
exit;
}
?>
You might notice that my implementation of setcookie differs from the official documentation. I've detailed this in a previous question, but this is the only implementation that works for me. The problem may lie here, but I haven't found any other working way to use setcookie. Not sure what is causing this issue.
Thanks in advance.