Devilix Devilix - 3 months ago 10
PHP Question

PHP - redirect if there isn't a cookie or another cookie

I have cookie A and cookie B, and want to redirect to Google if no cookies are present, but if one of the two is present must not redirect.

I have made/try this one:

if (!isset($_COOKIE['Access']) || !isset($_COOKIE['Guest'])) {
header ("location: https://www.google.com"); }


but without success... How can i solve it? Thanks

Answer

If you want it to redirect when NONE of your cookies are present your need the AND (&&) operator not the OR (||).

|| => OR

and

&& => AND

therefore

if (!isset($_COOKIE['Access']) || !isset($_COOKIE['Guest'])) {

should be

if (!isset($_COOKIE['Access']) && !isset($_COOKIE['Guest'])) {

Reading Material

Logical Operators

Comments