Jump to content
Larry Ullman's Book Forums

Chapter 13 - Is_Administrator


Recommended Posts

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

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

  • 2 years later...

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

  • 2 weeks later...

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

 Share

×
×
  • Create New...