Sessions Lost if DEFINE used (WIN7 64 SP1) 1.7.7 [SOLVED]

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Sessions Lost if DEFINE used (WIN7 64 SP1) 1.7.7 [SOLVED]

Postby bigorangemachine » 04. December 2011 23:01

Hi all.
This has been a problem for a while on my computer. I don't know what is going on.

I'm running Xampp on Windows 7 64bit. What would happen is that the PHPSESSID cookie was getting lost. Everything was showing the cookie was getting dropped (debugging saved cookies). I disabled javascript to be sure but I kept losing my session id. I checked the session files and they were there (and saving user credentials).

This is the test that helped discover the issue.

Code: Select all

   if(!defined('INC_DIR')){
      define('INC_DIR',str_replace("\\","/",dirname(__FILE__))."/");}

session_start();
               
if(count($_SESSION['asdf'])>10){array_pop($_SESSION['asdf']);}
$_SESSION['asdf'][]='yes '.rand(1,8109);
exit("<PRE>".print_r($_COOKIE,true)."</PRE>"."<PRE>".print_r($_SESSION,true)."</PRE>");

If you go from a 'fresh start' of xampp above won't work. The PHPSESSID cookie would be lost and the session variables wouldn't last that page view.

Code: Select all
session_start();
               
if(count($_SESSION['asdf'])>10){array_pop($_SESSION['asdf']);}
$_SESSION['asdf'][]='yes '.rand(1,8109);


   if(!defined('INC_DIR')){
      define('INC_DIR',str_replace("\\","/",dirname(__FILE__))."/");}

exit("<PRE>".print_r($_COOKIE,true)."</PRE>"."<PRE>".print_r($_SESSION,true)."</PRE>");

This will work. But I haven't read anywhere that you have to start your session before you define your variables. Its especially weird because it'll start working again later only to randomly stop working again.


I discovered this in my previous install of xampp. I updated and tried again. I then used a virtual host to ensure it wasn't a firefox/cookie issue. I also checked the session files and there was 1 session file for every 'include file' that used a 'define'.

Whats going on here? Is there a php.ini setting I can set to allow session_start and define() to play nice together?
bigorangemachine
 
Posts: 10
Joined: 14. July 2011 04:20
Operating System: Windows 7 64 Home Premium SP1

Re: [BUG?] Sessions Lost if DEFINE used

Postby Altrea » 05. December 2011 12:09

Hi bigorangemachine,

bigorangemachine wrote:What would happen is that the PHPSESSID cookie was getting lost. Everything was showing the cookie was getting dropped (debugging saved cookies).

Thats not correct. The Cookie is setted correctly, but will be sent to the HTTP-Daemon the very first time with the next request.

Read the common pitfalls at the setcookie() php documentation:
Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.

Cookies must be deleted with the same parameters as they were set with. If the value argument is an empty string, or FALSE, and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the remote client. This is internally achieved by setting value to 'deleted' and expiration time to one year in past.

Because setting a cookie with a value of FALSE will try to delete the cookie, you should not use boolean values. Instead, use 0 for FALSE and 1 for TRUE.

Cookies names can be set as array names and will be available to your PHP scripts as arrays but separate cookies are stored on the user's system. Consider explode() to set one cookie with multiple names and values. It is not recommended to use serialize() for this purpose, because it can result in security holes.



bigorangemachine wrote:If you go from a 'fresh start' of xampp above won't work. The PHPSESSID cookie would be lost and the session variables wouldn't last that page view.

In my development environment your example works as expected. The Cookie will be set (but you can check that the first time with the next request) and the Session value will be setted and are accessible on the next requests too.

This is the output i get for the first request:
Code: Select all
Notice: Undefined index: asdf in C:\xampp\htdocs\constant.php on line 6
Array
(
)
Array
(
    [asdf] => Array
        (
            [0] => yes 6396
        )

)


And this for the second request:
Code: Select all
Array
(
    [PHPSESSID] => pfpdabgksklr2r2rt82lregap0
)
Array
(
    [asdf] => Array
        (
            [0] => yes 6396
            [1] => yes 7828
        )

)





bigorangemachine wrote:
Code: Select all
session_start();
               
if(count($_SESSION['asdf'])>10){array_pop($_SESSION['asdf']);}
$_SESSION['asdf'][]='yes '.rand(1,8109);


   if(!defined('INC_DIR')){
      define('INC_DIR',str_replace("\\","/",dirname(__FILE__))."/");}

exit("<PRE>".print_r($_COOKIE,true)."</PRE>"."<PRE>".print_r($_SESSION,true)."</PRE>");

This will work.

That example works the same like the first one (tested).
Which can make a difference is, when you put the session_start() line down after the if(count($_SESSION['asdf'])>10){ line, because this line will throw the notice at the very first request.
And like it is described in the php documentation, sessions have to be started and cookies have to be setted BEFORE any output (including any error messages).

bigorangemachine wrote:Its especially weird because it'll start working again later only to randomly stop working again.

Not so weird. Maybe you have changed something more or different then described here. Just test it out.

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 10 Pro x64

Re: [BUG?] Sessions Lost if DEFINE used

Postby bigorangemachine » 05. December 2011 18:20

Altrea,
Thanks for the reply. Maybe I wasn't clear enough. I've been using PHP for many-many years now and I've never encountered this. I don't think this is cookie related as I am setting cookies later in the code and they are fine.

Everytime I do a define before a session start.... xampp will start a new session id resulting in the PHPSESSID getting dropped (its not in the browser cookie-cache its in the session temp files too).

Doing:
Code: Select all
//define_some_vars1.php
define('INC_DIR','abc/xyz/');
define('ROOT',$_SERVER['DCOUMENT_ROOT']);


Code: Select all
//define_some_vars2.php
define('SQL_STAMP','Y-m-d H:i:s');
define('SQL_USER','username');
define('SQL_PASS','&^*(&^DFSDF^A&^%&');
define('SQL_HOST','localhost');

... you get the idea ...
Code: Select all
include('define_some_vars1.php');
include('define_some_vars2.php');
include('define_some_vars3.php');
session_start();

will cause 3 session temp files to be created in one page view. Thus 3 sessions will be started and the browser will plum lose the session. The example I provided was basic version of the problem. My localhost is consistently doing this now.
I don't see why defining the variables as a constant before starting a session is screwing with the sessions.

However I do the following once:
Code: Select all
//THE FIX!
session_start();
include('define_some_vars1.php');
include('define_some_vars2.php');
include('define_some_vars3.php');

and the server is back to normal again.
Code: Select all
include('define_some_vars1.php');
include('define_some_vars2.php');
include('define_some_vars3.php');
session_start();

The above will work after you do 'THE FIX!'

I don't get would could be causing this. I assume I have my sessions configured in correctly but I am assume I am using the default php.ini session settings. Could it be unclosed PHP tags?
bigorangemachine
 
Posts: 10
Joined: 14. July 2011 04:20
Operating System: Windows 7 64 Home Premium SP1

Re: [BUG?] Sessions Lost if DEFINE used

Postby bigorangemachine » 09. December 2011 02:55

**BUMP**
Am I doing something wrong or did I find a bug?
bigorangemachine
 
Posts: 10
Joined: 14. July 2011 04:20
Operating System: Windows 7 64 Home Premium SP1

Re: [BUG?] Sessions Lost if DEFINE used

Postby JonB » 09. December 2011 10:01

DO NOT BUMP THREADS!

A. Someone has already begun helping you, and addressed some of your issues and asked for your feedback.

B. It is fairly unlikely you have run into a bug, but it is possible. If you have uncovered a bug, it's actually unlikely to be a bug that the XAMPP developers can do anything about, as they use the same PHP project source as everyone else - a thought - did you Google on your error???

C. All the 'staff' here are volunteers who gladly contribute their valuable time. Most of us have busy IT/Consulting/Development careers/jobs, lives and other commitments to honor. The 3 major contributors on 'this' board are from Australia, the United States, and Germany, so we may not be in 'your time zone'.

Capisce?

Thanks - I'm sure when he is back up and about Altrea will have some ideas.
8)
User avatar
JonB
AF Moderator
 
Posts: 3210
Joined: 12. April 2010 16:41
Location: Land of the Blazing Sun
Operating System: Windows XP/7 - Fedora 15 1.7.7

Re: [BUG?] Sessions Lost if DEFINE used

Postby JonB » 09. December 2011 10:14

B. It is fairly unlikely you have run into a bug, but it is possible. If you have uncovered a bug, it's actually unlikely to be a bug that the XAMPP developers can do anything about, as they use the same PHP project source as everyone else - a thought - did you Google on your error???


This gave me a thought - why don't you simply install a different XAMPP version and run the same code against it???

Which gives rise to the question - Where is your XAMPP version and OS version info, or did you think it simply wasn't relevant??? I had to read through the whole nine yards again to extract that you were on Win 7, but still don't know the PHP version, as I don't know your XAMPP version.
viewtopic.php?f=16&t=48626

Thanks and good luck
8)
User avatar
JonB
AF Moderator
 
Posts: 3210
Joined: 12. April 2010 16:41
Location: Land of the Blazing Sun
Operating System: Windows XP/7 - Fedora 15 1.7.7

Re: [BUG?] Sessions Lost if DEFINE used

Postby bigorangemachine » 09. December 2011 20:22

Thanks for following up. I'm sorry for not being more clear. I was trying to give as much information as possible in as few words as possible and make clear how I discovered the issue as not to waste other peoples time.

I understand other peoples commitments and timezones etc. My gut reaction is I'm doing something wrong so if the feedback was that I would be more inclined to go through some extreme measures to ensure I can share my discovery of the issue. I am sure I'm not the only person experiencing this.

It took me 16hrs just to narrow down the issue. I spent at least 4 hours googling the issue trying combinations that didn't involve xampp and called my host to update my PHP version to verify the issue which involved me making a full back up of my server with other delays/issues. In all I spent about 3 days just finding what the problem was and getting it to replicate consistently. Please don't accuse me of not researching the issue thoroughly as I know how valuable peoples time can be to them especially when they are offering free help.

I believe this is only my second time posting so I wasn't sure if this was a situation where I implied the issue was fixed nor was I aware that OS Version & XAMPP version were mandatory profile information. I've been on other message boards when a null reply means you are doing something wrong and they don't want to tell you what is wrong.

Since I've found a way to get it to work for the day, I thought that maybe I had implied the issue was closed. I thought maybe they'd want phpinfo() or run some real-time debugging software :S. Believe me I am eagar to help! I thought they were maybe waiting for more information.

JonB wrote:This gave me a thought - why don't you simply install a different XAMPP version and run the same code against it???

This was the first thing I tried after I eliminated a browser issue.
JonB wrote:... is your XAMPP version and OS version info...

I apologize I wasn't clear enough. I updated to the current version (ApacheFriends XAMPP version 1.7.7, Apache 2.2.21, MySQL 5.5.16 (Community Server), PHP 5.3.8 (VC9 X86 32bit thread safe) + PEAR) from XAMPP 1.7.4 (I also added the settings I needed to PHP.ini rather than keeping the old one). I don't know how to keep an old version of PHP so I did a new install (Not a package install a Zip) of XAMPP.

I am running Windows 7-64 Bit home Premium SP1.

=============

Today I was able to verify that it is PHP and/or my code.
I'll see if I can further narrow down the issue and post a response here for others that may have the same issue.

I'm sure I'll have it resolved in a few hours.
bigorangemachine
 
Posts: 10
Joined: 14. July 2011 04:20
Operating System: Windows 7 64 Home Premium SP1

Re: [BUG?] Sessions Lost if DEFINE used

Postby bigorangemachine » 09. December 2011 21:52

After a brief PM with Altrea he informed me he was having computer issues and we agree its probably not an issue with XAMPP if it is an issue at all. I'm posting my progess on the issue to assist anyone else who is having this issue.

I've replicated with my own host (with lunar pages).

I've gone through and cleaned up all the white space after ?> and added ?> where they weren't before and probably should have been. It appears that wasn't an issue.

I've exhausted every possible issue I can think it is in regards to my code or my browser. The issue must lay in my code or my computer.
bigorangemachine
 
Posts: 10
Joined: 14. July 2011 04:20
Operating System: Windows 7 64 Home Premium SP1

[BUG?] Sessions Lost if TOO DEEP INCLUDES

Postby bigorangemachine » 10. December 2011 04:18

Ok... I overhauled my code. I have a framework I've setup over the years and I was attempting to create a script that would 'optionally start sessions'. Previously I was using defines to test the session start 'flag' and then functions associated with session start would trigger based off that definition.

The issue:
A user visits a site, an .htaccess catch all filters all non-file page hits through a script (visit.handle.php). Inside visit.handle.php is some logic on how to refer the file base of the URL request. I am trying to trigger 'optional sessions' for things like images or file transfers (IE has a habit of hating cookies & mime-type/content-type headers). Not putting session start into init.php causes issues with the session.
Code: Select all
//visit.handle.php
include_once("inc/init.php");
define('DO_SESSION',true);

if($_SERVER['request_uri']=='home'){
   define('PACKAGE','default');//all packages use default but default can be called
   include("inc/pack.handle.php");
   include("above_content.php");
   include("home.php");
   include("below_content.php");
}else if($_SERVER['request_uri']=='news'){
   define('PACKAGE','news');//news will include files and create OOP instances needed for news only
   include("inc/pack.handle.php");
   include("above_content.php");
   include("news.php");
   include("below_content.php");
}


inside inc/init.php:
Code: Select all
//defines!
// AND !
define('INC_DIR',str_replace("\\","/",dirname(__FILE__))."/");

//GLOBAL VARIABLE INLCUDE
//FUNCTION INLCUDES
//OOP-CLASS INLCUDES
include(INC_DIR."pack.handle.php");


Inside inc/pack.handle.php:
Code: Select all

$files=array(
   'default'=>array(.....),
   'home'=>array(.....),
   'news'=>array(.....)
);

if(defined('PACKAGE')){
   if(PACKAGE=='news'){
      foreach($files['news'] as $k => $v){
         include_once(INC_DIR.$v);
      }
      $newsObj=new myNews;
      $newsObj->populate();
   }else if(PACKAGE=='home'){
      //you get the idea
   }
   foreach($files['default'] as $k => $v){
      include_once(INC_DIR.$v);
   }
}

if(defined('DO_SESSION')){
   if(DO_SESSION===true && !defined('SESSION_DONE')){
      session_start();
      foreach($_SESSION_FUNCTIONS as $k => $v){//assume $_SESSION_FUNCTIONS is declared in the inc/init.php somewhere or an include within there
         call_user_func_array($v);
      }
      define('SESSION_DONE',true);
   }
}


I went through all my code, disabled every cookie I could find. Got it back down to just the essentials and I think the issue lays within either:
a) Including a file that contains a session_start twice
b) Including the file too late in the game
c) Including the session_start too deeply???


My previous assertions that the issue was creating a new session_id each time was wrong but the session cookie was not being created/retained.


I've been on this too long. I'm going to see if I can replicated it without my framework and I'll post my results.

you might need to use session_save_path('session_folder/'); to ensure your sessions aren't corrupted by a previous session (if you are not using a virtual host/domain name). I also started & stopped xampp, cleared my cookies and cache each time to ensure a clean test.


When I was testing, I had removed session start back in to init.php and that gave the illusion it was the constant definitions
bigorangemachine
 
Posts: 10
Joined: 14. July 2011 04:20
Operating System: Windows 7 64 Home Premium SP1

Re: [BUG?] Sessions Lost if DEFINE used

Postby bigorangemachine » 15. December 2011 03:03

I was able to briefly replicate the issue. When I started testing to see if the issue is defines or includes depth the issue flared up and then disappeared. When I restored it back to when I was having the problem originally, I could not replicate it :(.

Now it appears to be working as it should.
bigorangemachine
 
Posts: 10
Joined: 14. July 2011 04:20
Operating System: Windows 7 64 Home Premium SP1

Re: [BUG?] Sessions Lost if DEFINE used

Postby bigorangemachine » 17. December 2011 02:24

CLOSE THE THREAD!
The issue is I had ini_set('session.cookie_domain', ...) incorrectly! When I stopped my defines from working it stopped the session cookie definition.
bigorangemachine
 
Posts: 10
Joined: 14. July 2011 04:20
Operating System: Windows 7 64 Home Premium SP1

Re: [BUG?] Sessions Lost if DEFINE used

Postby Sharley » 17. December 2011 02:28

Thanks for the feed back and I will close it and mark it solved. 8)

Good luck. :)
User avatar
Sharley
AF Moderator
 
Posts: 3316
Joined: 03. October 2008 05:10
Location: Yeppoon, Australia Time Zone: GMT/UTC+10
Operating System: Win 7 Pro 32bit/XP Pro SP3


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 162 guests