Skip to content
Advertisement

Why sessions are not working in home directory of website

In my website, After login the sessions are being set but they are not working in files of home directory but they are working in the other directories like /admin.

Maybe its some problem of my robots.txt file. this is the code of robots.txt file-

JavaScript

Code of my navigation bar –

JavaScript

The place where I am setting sessions in login.php

JavaScript

Navigation bar in /admin – Navigation Bar

Navigation bar in the files of home directory – Navigation Bar

So the sessions are not working in home directory

Advertisement

Answer

Your robots.txt file has absolutely nothing to do with PHP. It is purely on how to tell ethical robots what files to index.

The usual problem with sessions “not working” in PHP is due to session_start(); not being called.

Please add the following code to the top of pages where they are not functioning:

JavaScript

Honestly? it’s been over 11 years since I have typed session_start(). Why? because, if you’re coding this naked in PHP in 2021, you’re doing something incredibly wrong.

Please, please, learn Laravel or any modern framework. You, your project, your coworkers, the world, will all be better off.

I code PHP daily and I literally haven’t messed with $_SESSION[] directly since the probably 2010. It’s like doing document.getElementById('foo'); in JavaScript. Please don’t do that, either 😉

P.S. You might consider learning how to design stateless PHP apps, relying on the frontend AJAX to store state, via localStorage.getItem('key'), using JWT Tokens (tymon/jwt-auth) as the authentication system.

That’s how we’ve all been rolling for the last 5 years, actually.

Please read this article:

Goodbye PHP Sessions, Hello JSON Web Tokens

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement