简体   繁体   中英

Undefined index only at first request

I have created a cart for a Font Library localhost, but whenever the user sign in for example he goes straight to 'mycart.php' it shows undefined index for $_SESSION['cart'] = $cart; but if he clicks it next time the error won't be there. The php is working perfectly fine though. Why does this happens?

Assigment won't cause the undefined index error. This can happen when you want to retrieve the value of $_SESSION['cart'] which wasn't set.

The problem is when you want to use the value $_SESSION['cart'] you should check its existance with isset() function.

You wrote that when he click again there's no error. It's because this variable was set and it's in the session. Check if you use $_SESSION['cart'] before you assign it.

You can check what variables are assigned to session with print_r($_SESSION);

使用$_SESSION变量之前,请确保调用session_start()

像这样first time使用isset()

$cart=(isset($_SESSION['cart']) and $_SESSION['cart']!="") ? $_SESSION['cart'] : "";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM