Hi guys, I would ask question directly. When a user log in I setup these values. $_SESSION['name'] = $user['name']; $cookie_time = (3600 * 24 * 15); // 15 days setcookie ("name", $_SESSION['name'], time() + $cookie_time); Code (markup): The $_SESSION['name'] works as expected. But when I close a browser without logout. The next day I revisit my site and expect the cookie should work, but it's not working. The code I use to retrieve the cookie value is: $name = $_SESSION['name'] || $_COOKIE['name']; Code (markup): What's wrong with this? How do I retrieve the cookie? Thank you.
replace the line with the following: $name = (!empty($_SESSION['name'])) ? $_SESSION['name'] : ((isset($_COOKIE['name']) ? $_COOKIE['name'] : ''); Code (markup): That should make it collect the cookie if the session isn't set
Thanks @PoPSiCLe It works. Can you tell me where can I learn this coding style of a ? a : b ? b from. What is it called?
Okay. It is called shorthand if/else here: http://stackoverflow.com/questions/18474559/php-shorthand-if-else-when-using-return
http://davidwalsh.name/php-shorthand-if-else-ternary-operators has more information about it. [edit] apparently .name domains don't auto link.
It's known as a ternary operator, or a ternary statement, as mention in the above post. It's a shorthand way to express very simple structures (like most if/else-statements), and can also be nested (although it's not really recommended).