下面是我的货币转换器代码,如果没有cookie,它将设置一个新的cookie和变量,检查是否已请求更改cookie,并检查现有的cookie并为其设置变量。我使用了一个3rd party变量,因为在新的cookie集上,PHP在刷新之前无法看到并因此读取新的cookie。请检查是否可以简化:D
/* No Cookie (Default Currency GBP) */
if(!isset($_COOKIE['currency'])) {
setcookie(currency, GBP, time() + (86400 * 365), "/");
$currency = "GBP";
$currencyid = "1";
}
/* Set Varible Based On Exsiting Cookie and Change Varibles */
if (isset($_COOKIE['currency']) == "GBP" ){
$currency = "GBP";
$currencyid = "1";
}
elseif (isset($_COOKIE['currency']) == "USD" ){
$currency = "USD";
$currencyid = "2";
}
/* Change Cookie and Change Varibles (Because Check Below Wont Find New Varible of Cookie) */
if (isset($_GET['GBP'])) {
setcookie(currency, GBP, time() + (86400 * 365), "/");
$currency = "GBP";
$currencyid = "1";
}
elseif (isset($_GET['USD'])) {
setcookie(currency, USD, time() + (86400 * 365), "/");
$currency = "USD";
$currencyid = "2";
}
答案 0 :(得分:2)
您已将两个条件合并为一个表达式并破坏了预期的逻辑...
if (isset($_COOKIE['currency']) == "GBP" ){
应该是:
if (isset($_COOKIE['currency']) && $_COOKIE['currency'] == "GBP" ){
或其他一些单独的版本来维护逻辑。如前所述,您的代码正在询问是否true|false
== GBP
。
...实际上,根据您的第一个条件块,isset()
是毫无意义的。