PHP Session Unset

Posted on November 28th, 2022
Share this post:

PHP Session Unset

Session unset works well when you're looking to end particular sessions under certain circumstances. This example will explain how, along with a conditional statement which returns a particular message, depending on the session status.


Here, we have a basic form with a 'name' variable assigned to a text field. Upon entering their name into the form and submitting it, a session will be set for the end user. We will need this next code block as well though.


This code block would go at the top of the page with the form. As is always the case, we begin the file with 'session_start()', followed by a current time definition. The current time will be displayed in the event the 'name' session is set.

Also included in the session greeting is a countdown for an upcoming podcast, which is 3 hours ahead of the current time. This isn't an example you would use in real life I don't think, but gives you an idea of what kinds of messages you can create in the event a session is set or unset.

In order to add the 3 hours to the current time, we added 10800 seconds to it after converting it to 'strtotime'. This converts the current time into a long number. From there, we then convert the new time back to our original format so it becomes readable to the end user. Although a rather strange way of carrying this out, I don't think there are many ways around it. Not that I am aware of, anyway. I will go into further detail about 'strtotime' and other similar time functions in the near future.

Let's say I enter the name 'Dave' into the form. This would be my result at this current time.

Hello Dave. The current time is 07:30pm. The next podcast will be at 10:30pm.

Now, of course we will need a file that sets and unsets the sessions. When the end user enters in their name and sets the form, their 'name' session is set - as I have discussed up top. The second form, the 'reset', unsets the 'name' session and starts the session anew.


The code block up top is quite self-explanatory, making up a series of Conditional 'if' Statements which check to see if:

a) ...the name has been posted and if so, sets the 'name' session.

b) ...the session is set at all and if so, ensures the session 'name' is carried over.

c) ...the 'reset' button has been set and if so, unsets the 'name' session.

If my name is currently set in the session, after submitting the 'reset' button I get the message below.

Hello there. It would appear you haven't entered in your name yet. Use the form above.

Now, you could include the session setting and unsetting code on the same page as the form, however, that might be inconvenient for the end user. We have all tried to reload a page which posts a form and end up with that annoying message asking us if want to resent the information. If you only want a person to enter the information in once, the last thing you want happening is them re-sending the form.

This can also be done with AJAX, in conjunction with PHP, but for simplicity's sake, PHP can handle it just fine. We just have to create two files, instead of one. Personally, I don't mind doing that, but it's matter of perspective.

I hope this article has been helpful to you. Set up a development server on your PC and have a go with PHP sessions. You can do some impressive things with them.

Posted on:
November 28th, 2022

Comments:

There are still no comments posted
Write a comment