How to retain sessions when changing directories

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 602 Views - Last Post: 29 November 2012 - 08:58 PM Rate Topic: -----

#1 Esco94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-November 12

How to retain sessions when changing directories

Posted 29 November 2012 - 01:36 PM

Hello there,

I have website that I am testing locally on Xammp. So the homepage of my website is localhost/root/index.php

I have a user login and registration system implemented and it works fine. If a user is logged in they can click

an account link and visit their profile and if a user isn't logged in a login form is displayed. While logged in,

I can successfully browse any of the pages in my root directory and the session will not get lost.

However, if I then try to visit my profile page (Which is in a different folder) the session is lost and the login

form is presented on the profile page. The URL for the profile page would be something like this:

localhost/root/user/profilepage.php


As you can see, to get to the profile page you must leave the root directory and go to the "user" directory.

Once you leave the root directory, however, the session is lost.

I'm 100% sure, that every page on my site includes the session start function:

<?php session_start(); ?>



This is written in a "database.php" file which is subsequently included in every page on the site.

That is my problem and it's seriously hindering my progress on my website.

I have been on hundreds of threads about this (no exaggeration) and related problems and as of yet have no solution.


I would appreciate any replies with suggestions or even a solution because I'm tearing my hair out over this, so frustrating.

Is This A Good Question/Topic? 0
  • +

Replies To: How to retain sessions when changing directories

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2890
  • View blog
  • Posts: 10,019
  • Joined: 08-August 08

Re: How to retain sessions when changing directories

Posted 29 November 2012 - 01:51 PM

Your problem is that you're thinking of PHP files as pages. They are not. Pages are what gets sent to the browser.
Users should NOT have their own folders. That's a source of trouble.
Was This Post Helpful? 0
  • +
  • -

#3 Esco94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-November 12

Re: How to retain sessions when changing directories

Posted 29 November 2012 - 01:55 PM

View PostCTphpnwb, on 29 November 2012 - 01:51 PM, said:

Your problem is that you're thinking of PHP files as pages. They are not. Pages are what gets sent to the browser.
Users should NOT have their own folders. That's a source of trouble.


Users don't have their own folder. One folder, "user", contains all profile pages. However these profile pages lose the session
because of the directory change.

I really appreciate any replies.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2890
  • View blog
  • Posts: 10,019
  • Joined: 08-August 08

Re: How to retain sessions when changing directories

Posted 29 November 2012 - 01:59 PM

So don't change directories. Include the appropriate file from a higher directory.

Alternatively, you could follow the rules and show us precisely what is going on.
Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,963
  • Joined: 08-June 10

Re: How to retain sessions when changing directories

Posted 29 November 2012 - 02:01 PM

PHP sessions are usually shared across an entire domain. The file structure you described should not cause any problems in that regard. I'm guessing there is another problem, likely something to do with how the session is started in the sub-directory.

Could you show us the code in the second page, or at least whatever part of it is responsible for opening the session?
Was This Post Helpful? 0
  • +
  • -

#6 Esco94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-November 12

Re: How to retain sessions when changing directories

Posted 29 November 2012 - 02:09 PM

View PostAtli, on 29 November 2012 - 02:01 PM, said:

PHP sessions are usually shared across an entire domain. The file structure you described should not cause any problems in that regard. I'm guessing there is another problem, likely something to do with how the session is started in the sub-directory.

Could you show us the code in the second page, or at least whatever part of it is responsible for opening the session?



Yeah sure,

So Each profile page is unique but includes a template, profiletemplate.php

On profiletemplate.php I include my database.php file:

include "http://localhost/home/base/database.php";



Inside the database.php file:

session_start();



This makes sure that every single page contains session_start() as every page includes the database.php file.

As I say, it works fine in the root directory until I go in to the "user" folder.

The include definitely works because when I run this:

if (include "http://localhost/home/base/database.php"){
echo "included";
}




Included is echoed out.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2890
  • View blog
  • Posts: 10,019
  • Joined: 08-August 08

Re: How to retain sessions when changing directories

Posted 29 November 2012 - 02:48 PM

Include should be done using the path to the file, not a URL:
include "home/base/database.php";

Using a URL tells the server that you are from outside the server, so you get no access to server variables.
Was This Post Helpful? 0
  • +
  • -

#8 Esco94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-November 12

Re: How to retain sessions when changing directories

Posted 29 November 2012 - 02:59 PM

View PostCTphpnwb, on 29 November 2012 - 02:48 PM, said:

Include should be done using the path to the file, not a URL:
include "home/base/database.php";

Using a URL tells the server that you are from outside the server, so you get no access to server variables.


Ok I changed the include to:

<?php include "home/base/database.php";?>



Now it gives me an error:

include(home/core/init.php) failed to open stream: No such file or directory
Was This Post Helpful? 0
  • +
  • -

#9 Esco94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-November 12

Re: How to retain sessions when changing directories

Posted 29 November 2012 - 03:13 PM

I've got the base/database.php include to work properly now without using the full URL (http://...)

However it doesn't solve the problem, the session still ends.

I tried "re-setting" the session by adding a get variable to the profile page link.

I tried to pass the $user_id (basically an auto-increment value from the db) in order to retrieve the

value on the profile page and set the session again. Even that didn't work. I used the isset function to check

if the GET variable was set and it wasn't. For some reason it didn't pass over.

I have a feeling this whole problem has to do with the way I'm handling the profile pages.

Basically every profile page is just one line:

<?php include "home/user/profiletemplate.php";?>



And then the page will be filled with variables from the db that I use the user Id to find.

This post has been edited by Dormilich: 29 November 2012 - 11:29 PM
Reason for edit:: removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2890
  • View blog
  • Posts: 10,019
  • Joined: 08-August 08

Re: How to retain sessions when changing directories

Posted 29 November 2012 - 03:16 PM

If that file has anything including a space or other HTML that gets sent before session_start() it's not going to work.
Was This Post Helpful? 1
  • +
  • -

#11 Esco94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-November 12

Re: How to retain sessions when changing directories

Posted 29 November 2012 - 03:32 PM

View PostCTphpnwb, on 29 November 2012 - 03:16 PM, said:

If that file has anything — including a space or other HTML —  that gets sent before session_start() it's not going to work.


This is the start of the profile template:

<!DOCTYPE html>
<?php 
include "../core/init.php";


function logged_in() {
	
	return (isset($_SESSION['user_id'])) ? true : false;
	}
	
	//$id = $_SESSION['user_id'];
	
	if (logged_in() === false){
	echo "Not logged in";
	
	}



__________________________________

After that there's html and the includes for the header and footer.

The not logged in block of code is always executed.



if you see the include as:

core/init.php

That is ok, it's the actual file names.

I used base/database.php here for demo purposes.

This post has been edited by Dormilich: 29 November 2012 - 11:30 PM
Reason for edit:: removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#12 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,963
  • Joined: 08-June 10

Re: How to retain sessions when changing directories

Posted 29 November 2012 - 03:56 PM

Like CTphpnwb said: If there is any output before the session_start() call, then it won't work. For the session_start to work, it must set a HTTP header for the response. As soon as you add anything to the response, the headers are locked and can not be modified.

So, in your profile template, the fact that you declare he HTML Doctype before you start the PHP code will cause the session to fail.
// This won't work!
<!DOCTYPE HTML>
<?php
    session_start();
?>


// You must do it like this
<?php
    session_start();
?>
<!DOCTYPE HTML>


Was This Post Helpful? 1
  • +
  • -

#13 Esco94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-November 12

Re: How to retain sessions when changing directories

Posted 29 November 2012 - 05:54 PM

I made the change, but it didn't solve the problem.

This post has been edited by Dormilich: 29 November 2012 - 11:31 PM
Reason for edit:: removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#14 Esco94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-November 12

Re: How to retain sessions when changing directories

Posted 29 November 2012 - 06:01 PM

Ok I just had somewhat of a breakthrough.

It is now partially working.

When I go to localhost/home/user/profiletemplate.php (i.e, just accessing the template) the session stays intact,

and the login form is not presented. It's when I go to the actual profile that includes this template, I'm not logged in.
Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2890
  • View blog
  • Posts: 10,019
  • Joined: 08-August 08

Re: How to retain sessions when changing directories

Posted 29 November 2012 - 08:56 PM

<?php
// You must do it like this
session_start();
?>
<!DOCTYPE HTML>


Nothing can go before session_start(). Not even the comment if it's in the HTML scope.

This post has been edited by CTphpnwb: 29 November 2012 - 08:56 PM

Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2