phpRob Posted December 4, 2011 Share Posted December 4, 2011 Just started working through the final chapter. I'm struggling to understand the is_administrator function on page 384, in particular this line (in bold): function is_administrator($name = 'Samuel', $value = 'Clemens') { if (isset($_COOKIE['name']) && $_COOKIE['name'] == $value) { return true; } else { return false; } } Now I understand that it tests to see whether the cookie name 'Samuel' is set but not why it tests whether 'Samuel' is equel to 'Clemens'?? Can someone please explain this? It's probably very simple!! Link to comment Share on other sites More sharing options...
Larry Posted December 5, 2011 Share Posted December 5, 2011 First of all, shouldn't it be $name within the function (isset($_COOKIE[$name]) and not 'name'? Link to comment Share on other sites More sharing options...
phpRob Posted December 5, 2011 Author Share Posted December 5, 2011 Ooops, sorry Larry that is correct. if ( isset($_COOKIE[$name]) && ($_COOKIE[$name] == $value) ) { return true; } else { return false; } Could you explain the if statement, I still don't fully understand the logic. Link to comment Share on other sites More sharing options...
Larry Posted December 6, 2011 Share Posted December 6, 2011 Okay, so the purpose of the function is to confirm that a specific cookie has a specific value, which in this case proves that the person is an administrator. To do that, you'd use isset($_COOKIE['XXX']) to make sure the cookie exists, and $_COOKIE['XXX'] == 'value', to confirm the value. That's what the logic is. To make the function flexible, both the cookie's name and value are represented by variables, so that the function can be called in many ways. Does that make more sense? Link to comment Share on other sites More sharing options...
AprilSwenby Posted December 6, 2011 Share Posted December 6, 2011 I too had this question and I thank you for your answer - I am trying to find a way to answer the first review question on chapter 414. Do you mean - how would we re-write rather than how would the the function be called? Link to comment Share on other sites More sharing options...
Larry Posted December 8, 2011 Share Posted December 8, 2011 No, no. I definitely mean be called. With the function defined as is, you can use it to check ANY cookie with ANY name and ANY value, just by changing the function call. Link to comment Share on other sites More sharing options...
phpme5 Posted March 12, 2014 Share Posted March 12, 2014 Okay, so the purpose of the function is to confirm that a specific cookie has a specific value, which in this case proves that the person is an administrator. To do that, you'd use isset($_COOKIE['XXX']) to make sure the cookie exists, and $_COOKIE['XXX'] == 'value', to confirm the value. That's what the logic is. To make the function flexible, both the cookie's name and value are represented by variables, so that the function can be called in many ways. Does that make more sense? Hello, First, this is one of the best books regarding php (for beginners) I ever read. Thank you very much. I understood every bit of it except this function, which seems really weird to me. The part where ...($_COOKIE[$name] == $value) is very confusing, how could 'Samuel' ever be == to 'Clemens'. or why would $name be == to $value? Can you explain in more detail, please?(english is not my first language and it seems I am missing something). Thank you! Link to comment Share on other sites More sharing options...
Larry Posted March 24, 2014 Share Posted March 24, 2014 Sorry for the delayed reply. So basically the goal of the function is to make it very flexible to check a specific cookie's value. The name of the cookie corresponds to the index of the $_COOKIE array. By taking that as an argument, the function could refer to $_COOKIE['samuel'] or $_COOKIE['user'] or $_COOKIE['whatever']. Also, it's not that "Samuel" would equal "Clemens" or that $name would equal $value, it's that the value stored at $_COOKIE[$name] would equal $value. Does that make more sense now? Thanks again for the nice words! Link to comment Share on other sites More sharing options...
Recommended Posts