We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,031 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Sessions Not Working In Another Directory

Hello,

So in my root directory sessions work fine. On every page I include an initialization page like so:

<?php include "http://localhost/home/core/init.php";?>

On this page I start the session:

session_start();

Now when logged in to my site I can browse every page in the root directory and still be logged in.

I remain logged in while browsing pages like localhost/home/index.php, localhost/home/about.php etc.

Now when I want to go to a profile page I have to go to another (users) directory:

localhost/home/users/profilepage.php

When I get to this page I am not logged in. I'm presented with a log in form (this is displayed if the user is not logged in).

I have tried things like setting cookie params in the php.ini file to no avail. I have scoured the web endlessly

for days trying to solve this. I need to have many folders/directories on my site and I'm going to need to solve

this soon. I haven't progressed in 3 days and it's very frustrating.

I don't know why it's happening as I'm still including the initialization page that starts the session on the page in the new directory.

I'm fairly new to PHP and programming in general.

Thanks you very much any suggestions are apprectiated.

4
Contributors
6
Replies
1 Hour
Discussion Span
5 Months Ago
Last Updated
8
Views
webdev2012
Newbie Poster
3 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Are you running this on a Windows Server?

GliderPilot
Posting Whiz in Training
239 posts since Sep 2006
Reputation Points: 34
Solved Threads: 42
Skill Endorsements: 12

I am running it on Xammp. Could it be an Xammp configuration issue?

webdev2012
Newbie Poster
3 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Have you set up virtual hosts, e.g. in your vhosts file:

e.g. C:\xampp\apache\conf\extra\httpd-vhosts.conf

<VirtualHost *:80>
  DocumentRoot c:/xampp/htdocs
  ServerName localhost
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot c:/xampp/htdocs/diafol
  ServerName diafol.local
</VirtualHost>

and then in your hosts file:

e.g. C:\Windows\System32\drivers\etc\hosts

127.0.0.1       localhost
::1             localhost
127.0.0.1       diafol.local

However if everything is in the same parent directory ('home'), can't see why session should fail.

diafol
Keep Smiling
Moderator
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57

No, after making those changes it said I didn't have permission to view the pages so I reverted everything back to normal. Yes everything is contained within Home but when going to the new directory ('users') the session is lost, so frustrating.

webdev2012
Newbie Poster
3 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Just off the top of my head, if you're running Xammp on Windows you may have to change a couple session settings in your php.ini file:

session.save_path = c:\temp
session.cookie_path = /

If it's still not working it may have to do with the fact you're using localhost which has a very refined set of rules that can cause issues like the one you're having.You can't techinically set a proper cookie on a non registry controlled domain. You could always try using the hosts file to setup a fake "proper" domain like mytest.local which should work in everything but Chrome

GliderPilot
Posting Whiz in Training
239 posts since Sep 2006
Reputation Points: 34
Solved Threads: 42
Skill Endorsements: 12

Hi,

think of it as
localhost/ root account
localhost/domainOne/ session is only effective here and nowhere else
localhost/domainTwo/ the same thing here, session cannot be validated above this directory and below this directory

while sessions stablished, in these directories are valid in directory domainOne

localhost/domainOne/directoryone/
localhost/domainOne/directorytwo/
and the list goes on....

So, session from directory domainOne cannot be valid in domainTwo directory. It works like a VPS hosting with multiple domains in it.. sessions are processed only specific to its own working directory, space, or domain.

veedeoo
Master Poster
735 posts since Oct 2011
Reputation Points: 298
Solved Threads: 130
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0857 seconds using 2.72MB