Cookies are similar to sessions only that sessions are stored on the server, but for PHP to recognize which session is from which client, a cookie is stored in the client called PHPSESSID, this has the session number and links to the client with the server.
The sessions allow you to store information that may be useful during a user's navigation through the website. While the session lasts, the information will be available for the PHP code every time the user requests a page.
Some examples of information that may be interesting to keep in session:
- Whether the user has authenticated or not.
- Identifier of the user who has been authenticated.
- Products added to a shopping cart.
- Billing and address information to place an order.
Cookies also allow you to store information during the user's browsing, but in order to use it in future connections (future sessions), rather than in the same session.
Examples of information that might be interesting to keep in the cookie:
- If the user has checked the check to remember user and password.
- The user and password entered by the user, so that when he returns to our website, he automatically authenticates without requesting another username and password.
- The language selected by the user the last time he accessed, to show him directly the page in that language and not to ask him to choose the language each time he enters.
- The last product you bought (if the website is an online store) to show you related product offers.
Now if you want to know where a client is connecting from as I read you were looking to store or any other information regarding the client's connection you can use the variable $ _ SERVER , for example you can get the ip of the client like this:
$_SERVER['HTTP_CLIENT_IP'];
Once obtained, you can store it in a cookie, example with your code:
function set_cookie() {
$this->load->helper('cookie');
$cookie= array(
'name' => 'ipCliente',
'value' => $_SERVER['HTTP_CLIENT_IP'],
'expire' => '3600',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
echo json_encode(array("status" => TRUE));
}
I hope this information will help you.