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-
User-agent: * Disallow: /cgi-bin/ Disallow: /admin/ Disallow: /*?*
Code of my navigation bar –
<nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <a class="navbar-brand" href=""><?php echo website_name; ?></a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav mr-auto"> <li class="nav-item "> <a class="nav-link" href="<?php echo location; ?>">Home </a> </li> <li class="nav-item "> <a class="nav-link" href="dashboard.php">Dashboard</a> </li> <li class="nav-item"> <a class="nav-link " href="about-us.php" >About Us</a> </li> <li class="nav-item"> <a class="nav-link " href="contact-feedback.php" >Contact Us / Feedback</a> </li> <li class="nav-item"> <a class="nav-link " href="privacy-policy.php" >Privacy Policy</a> </li> <?php session_start(); if (isset($_SESSION['loggined'])){ echo '<li class="nav-item"> <a class="nav-link " href="wins.php">My Wins</a> </li>'; } ?> <?php if (isset($_SESSION['admin'])) { echo ' <li class="nav-item"> <a class="nav-link " href="admin/Web_ManageMent" >Admin Panel</a> </li> '; } ?> </ul> <?php if (!isset($_SESSION['loggined']) ) { echo ' <div class="row"> <button class="btn btn-sm btn-primary mx-3 my-2" data-toggle="modal" data-target="#loginModal" id="loginBtn">Login</button> <button id="signupBtn" class="btn btn-sm btn-primary my-2" data-toggle="modal" data-target="#signupModal" >Sign Up </button> </div> '; } else { echo ' <hr> <span class="text-white">Welcome , '.$_SESSION['name'].'</span> <br> <button class="btn btn-sm btn-primary mx-3 my-2"><a class="text-white" href="registration/logout.php">Log Out</a></button> '; } ?> </div> </nav> <?php include("modals/signup.php"); include("modals/login.php"); ?>
The place where I am setting sessions in login.php
$_SESSION["loggined"] = true; $_SESSION["username"] = $username; $_SESSION["name"] = $row['display_name']; $_SESSION['id'] = $row['user_id']; header("location: dashboard.php");
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:
if (session_status() === PHP_SESSION_NONE) { session_start(); }
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: