2

Possible Duplicate:
Cookie VS Session
PHP: How do Cookies and Sessions work?

I'm trying to learn PHP. I have a project website where numbers are constantly being generated, and changed and stored in javascript variables. There is very little php involved except in storing these variables to a database when the user hits the "store to database" button. Today I was using the site, navigated to another website and went back and all my data was gone as i had not stored it to the database first. I would like to save that data so it repopulates if I leave the page. what would be the best method of doing this? php sessions, cookies, or javascript cookies? please advise, thanks in advance!

Community
  • 1
  • 1
cream
  • 1,129
  • 5
  • 16
  • 26

5 Answers5

7

php sessions, cookies, or javascript cookies?

There is either a session or cookie so there are two things not three.

Now a session is also a cookie but is saved on server unlike simple JS cookie which is saved in user's machine.

I would like to save that data so it repopulates if I leave the page

If it is sensitive information, always use database to store it and if it is not sensitive data:

  • Use cookies or localStorage but it can be deleted by user
  • Use session which can't be deleted by a user but will expire based on php.ini settings

On the other hand, to save it permanently, use the database instead.

Blaster
  • 9,414
  • 1
  • 29
  • 25
3

PHP and Javascript cookies are the same thing, they are just data stored client side, php and javascript are the technology used to store them, nothing more.

Since PHP cookies can only be set before an output is sent to the page, it seems Javascript cookies would be best.

You would use cookies instead of a session because you mention you would leave the page, in which case the session would terminate and you would lose your data.

1321941
  • 2,139
  • 5
  • 26
  • 49
1

Use sessions when you want to temporarly store some data (for one session - until user closes his browser).

Use cookies when you want to store data for longer (like login cereditials).

You should also have on your mind that user can change value of stored cookies, but can't for sessions, since sessions are stored on a server, but cookies are stored on client's computer.

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
1

I believe cookies is the answer you need, as php session is only stored between page loads, so you are effectively sending the data back to the server already (not what you want) and as far as I know, javascript cookies are just cookies set with javascript.

So to clarify, I think you should set a cookie (by using javascript) every time some data is created - which will store locally on the browser (still fairly volatile) until the user presses the save button, where it will be sent back to the server.

Billy Moon
  • 57,113
  • 24
  • 136
  • 237
-2

PHP cookies if you want to store long term, but don't care whether the user changes the values or not.

PHP sessions if you don't want the user to have the ability to change values but don't need long term storage (this sounds like what you want)

Both session and cookies if you want to store long term and don't want users to have access to changing the values. You would want to use a database with this so that you could check the cookie information with the database to see if it was correct, and then store the data in sessions for easy access.

This is how many sites 'remember users'.. They store a cookie with the username and password, and then when the user visits the site (if a session is not set) they check the username and password with the database and then if it is correct, they create a session specific to that user.

Max Hudson
  • 9,961
  • 14
  • 57
  • 107
  • 2
    by your logic, by using sessions and cookies, the user can manipulate the data after the session expires..? – Billy Moon Jul 06 '12 at 20:29
  • 1
    'PHP sessions if you don't want the user to have the ability to change values' session hijacking? – 1321941 Jul 06 '12 at 20:31
  • use cookies to store information about the user, check it with a database, then use sessions to access the information you confirmed was correct quickly without using many recources. as for hijacking the chance of session hijacking is next to none.. you would need to guess a 20 digit random code! – Max Hudson Jul 06 '12 at 20:34