We had a website created via WordPress by some devs years ago.
I have created a subdirectory to deploy another website to be used in-company. I have created website locally and everythiung works.
Problem is, When I login to subfolder website, I am able to login and succesfully session being created and I am seing it in console, but session kills automatically if I go to index of subfolder. I deleted redirecting codes from login page to not be directed to index. With this My Navbar shows items that supposed to be seen when logged in. BUT INDEX DOES NOT CARRY MY SESSION IN ANYWAY. Pastin codes below.
I tried many things that thought. at first I thought session not starting because login was not redirecting me to index with header(); then I used JS codes to see session and redirect. etc.
**
my nav.php**
<?php
/* error_reporting(E_ALL);
ini_set('display_errors', 1);
*/
if (!isset($_SESSION)) {
session_start();
}
require "conn.php";
if (!isset($_SESSION['sesUser'])) {
$_SESSION['sesUser'] = null;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="" rel="stylesheet"
integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<script src="" crossorigin="anonymous"></script>
<!-- jQuery library -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav me-auto mb-2 mb-md-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="index.php">Home</a>
</li>
<?php if (isset($_SESSION['sesUser'])) :?>
<li class="nav-item">
<a class="nav-link" aria-current="page" href="pricing.php?s=custList">Customers</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button"
data-bs-toggle="dropdown" aria-expanded="false">Pricing Sheets</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="pricing.php?s=kitchen">Kitchen Pricing</a></li>
<li><a class="dropdown-item" href="bathroom.php?s=kitchen">Bathroom Pricing</a></li>
</ul>
</li>
<?php endif;?>
</ul>
<ul class="navbar-nav navbar-right">
<?php if (!isset($_SESSION['sesUser'])) :?>
<li class="nav-item">
<a class="nav-link fa-regular fa-user" style="font-size: 20px; color: green;"
href="login.php">Login</a>
</li>
<?php else :?>
<li class="nav-item">
<a class="nav-link fa-regular fa-user" style="font-size: 20px; color: red;"
href="logout.php"><?php echo ' ' . $_SESSION['sesUser']?></a>
</li>
<li></li>
<?php endif;?>
</ul>
</div>
</div>
</nav>
<script> console.log("<?php echo $_SESSION['sesUser']; ?>");
console.log("<?php echo $_SESSION['sesRole']; ?>");</script>
</body>
</html>
my login.php
<?php
require "nav.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<title>BSKB</title>
<style>
#btn {
position: relative;
}
</style>
<!-- jQuery library -->
<script src=""></script>
<!-- Latest compiled JavaScript -->
<script src=""></script>
</head>
<body>
<div class="container">
<?php if (empty($_POST['username']) && empty($_POST['password'])) { ?>
<div class="d-flex align-items-center justify-content-center" style="height: 80vh;">
<form action="" method="post">
<div class="form-group row ">
<div class="col-xm-5">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Enter username." >
</div>
</div>
<div class="form-group row">
<div class="col-xm-3">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Enter password.">
</div>
</div>
<div id="btn">
<button type="submit" class="btn btn-success mt-3 mb-3" id="button">Login</button>
</div>
</form>
</div>
</div>
<?php } else {
$loginsql = $db->prepare("SELECT * FROM user WHERE username=? AND password=?");
$loginsql->execute(array($_POST['username'], $_POST['password']));
$login = $loginsql->fetch(PDO::FETCH_ASSOC);
$_SESSION['sesUser'] = $login['username'];
$_SESSION['sesRole'] = $login['role'];
?>
<script>
console.log("<?php echo $_SESSION['sesUser']; ?>");
console.log("<?php echo $_SESSION['sesRole']; ?>");
/* $(document).ready(function() {
setTimeout(function() {
window.location.href = "/pricing/index.php";
}, 1000);
}); */
</script>
<?php }?>
<script src="" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
</body>
</html>
my index.php
<?php require "nav.php";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BSKB</title>
<link href="" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous"> <!-- jQuery library -->
<script src=""></script>
<!-- Latest compiled JavaScript -->
<script src=""></script>
<style>
body{
height: 90vh;
}
.btn-secondary,
.btn-secondary:focus {
color: #333;
text-shadow: none;
}
.btn-secondary:hover{
transform: scale(1.2);
transition: 0.3s;
text-shadow: none;
color: #333;
}
</style>
<body class="bg-dark">
<div class="d-flex h-100 text-center text-white">
<div class="cover-container d-flex w-100 h-100 p-3 mx-auto flex-column mb-auto">
<header class="mb-auto" style="background-color:red ;">
</header>
<main class="px-3">
<h1> </h1>
<h2>PRICING SYSTEM</h2>
<?php if(!isset($_SESSION['sesUser'])): ?>
<p class="lead pt-5">
<a href="login.php" class="btn btn-lg btn-secondary fw-bold border-white bg-white">LOGIN</a>
</p>
<?php else: ?>
<p class="lead pt-5">
<a href="pricing.php?s=custList" class="btn btn-lg btn-secondary fw-bold border-white bg-white">JOBS</a>
</p>
<?php endif; ?>
</main>
<footer class="mt-auto text-white-50">
<p>Github <a href="" class="text-white">Portfolyo</a>, by <a href="#" class="text-white">Egemen Aydin</a>.</p>
</footer>
</div>
</div>
<script src="" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
</body>
</head>
</html>
index.php shows only welcome page. Main page is my pricing.php but I can acces there via link. Session works in there.