Everything You Need to Know About PHP Session

Yunus Emre Adas
Developers’ Corner
4 min readJul 28, 2024

--

Did you ever think about how I can use $_SESSION more efectivly? So I was.

If you are not a member, you can access to full text here.

Using sessions in PHP is inevitable. Most of PHP devs already know that. Lets dive into what is sessions and how do we use it better.

Sessions are used to control user data and identity. For example, an e-commerce application uses sessions to remember a user’s cart, or a social media application uses sessions to keep users online.

1. How We Use It?

In PHP we use it with session_start() function. It starts the session for each user who log into website.

Session starting
Session starting

Hint: You must add session_start() to top of the html page. It should be above of <head> tag.

Now we have a fresh-born session on our website. It will help us to store user specific data. You can add user_id, website current language or many etc. to session global variable.

2. Adding Datas to Session

You can dynamicly add values to session. It works like json. You should use keys for each…

--

--

Yunus Emre Adas
Developers’ Corner

Web Developer, Part-Time blogger, Solopreneur, Tech Lover. Join my journey and find out who we really are.