Store data in Cookies CodeIgniter

0

I am working with the Cookies in CodeIgniter but I have doubts about what I have to send the Cookie. This is my Cookie example, it does not mark me an error but I do not know how it stores the user's information.

function set_cookie() {
  $this->load->helper('cookie');

  $cookie= array(
    'name'   => 'NombreCookie',
    'value'  => 'Demostracion de Cookie en CI',
    'expire' => '3600',
    'secure' => TRUE
  );

  $this->input->set_cookie($cookie);

  echo json_encode(array("status" => TRUE));

}

Then I bring what I store the Cookie with the following function

function get_cookie() {
  $data['cookie'] = $this->input->cookie('NombreCookie', true);
  echo json_encode($data);
}

Finally I make an alert with JavaScript and it brings me what I saved in the value of the Cookie thinking that it was going to bring me more information such as where it was connected from ...

I hope you can support me on the data I have to tell you to store. Thanks

    
asked by Javier fr 23.10.2018 в 18:37
source

1 answer

1

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.

    
answered by 24.10.2018 / 06:05
source