I have my code for setting the session like:
if($found>0){
session_start();
$_SESSION['user_name']=$user;
session_set_cookie_params(24*60*1,'/','.localhost');
$expire=time()+60*60*24;
setcookie("cookiename", $user, $expire);
header("location:http://localhost/UI/user/userprofile.php");
} else{
$message = "Username or password is not correct.";
header("Location:index.php?message={$message}");
}
here is my header content where i put login and logout
session_start();
if (isset($_COOKIE["cookiename"])){
$unm = $_SESSION["user_name"];
echo "User : " . $_SESSION["user_name"] . "";
echo " <a href='http://localhost/UI/user/logout.php'>logout</a>";
echo " <a class='addmeeting' href='http://localhost/UI/user/createmeeting.php' title='Create New Meeting'>Create Meeting</a>";
} else{
echo "<li><a href='register.php'>Register</a></li>";
echo " User : Guest!<br />";
}
My session is working for subfolder but it is not working for the parent folder.
Here is the directory structure:
UI
user
userprofile.php
login.php
logout.php
index.php
headers.php
Please tell me what i am doing wrong ?
My guess is that it’s the cookie that’s not working, rather than the session (your session code is inside an
if()block that checks the cookie first).Cookies default to being limited to the current folder, so it won’t apply to the parent folders.
If you want it to apply to the whole site, you need to specify a
/in the cookie, like so:This will set the cookie across your entire site, so your code should work.
However, I don’t really understand why you’re not just using sessions here anyway; why have cookies and sessions in the same context? You may as well set everything in the session and be done with it. (sessions are cookie based anyway)