Good day! I am new to programming. I have a theoretical question. There is an array with values. I want to show each time a new value from the array after each reload of the page until these values run out. I know that after reloading the page, the connection with the server is broken and data is lost. Question: how can I make so that from the array (1, 2, 3, 4) After four reloads of the page 1, 2, 3, 4 were sequentially derived. Thank you.
3 answers
At the beginning of the page session_start(); , after that we have an array of $_SESSION[] , which remains after reloading the page. In general, do something like this:
Create a session.
Check for $_SESSION['last'] .
If not, create and set the value '0' .
If there is, we increase by one.
We use $_SESSION['last'] as we need.
- This option has a very limited scope. - rjhdby
- This option fully covers the conditions of the problem. - uk141
- This option does not cover the conditions of the problem in 3 cases. 1) If the request comes from different users, 2) if the session is foul, 3) if the client has cookies disabled - rjhdby
- Good. You're right. My version covers only those tasks that were born in my imagination. - uk141
You need to save the last number displayed on the server. There are many options.
- As said @ uk141, session variables can be used. For several reasons, this option has an extremely narrow scope. Since it will be tied to only one session with all the consequences.
- You can use the cache and its increment function . Accordingly, each time the server is restarted, the value will be lost.
- You can store the value in the database. Cons in that it is necessary to use the database. But this option seems to me the most correct.
- You can store the value in a local file. But here you can dance on the rake of competing access.
- You can use the $ _SERVER superglobal array. Bad option, see also point 2
Considering that server requests can be from many clients, so that for each client it works, the data must be stored on the client, that is, in a cookie, respectively, the method with the session is best suited. Otherwise, if someone shows 1, then the next client will already show 2. Or, if stored in a database or file, then you need to save from the user's IP and then do a selection by IP, check, etc. This way you can do without sessions and cookies.