FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


RESOLVED: Debugging help with PHP $_SESSION...





richard270384
Gday,

I have a script that I have been working on. Took a couple of weeks of due to personal issues, I've come back to it and found that something isn't working as it should.

Is it possible for me to add some sort of 'catch' to the $_SESSION variable that will log a message everytime the session variable is altered?

I am saving some data to the session variable but it is disappearing in between page refreshes. Other variables I have saved in the session are fine, so I can only assume that I have made some coding error which is overwriting my previously saved values for certain variables.

I have a var_dump of the $_SESSION variable at the end of my script and the session data is there. I have another var_dump at the start of the following script and the variables still exist but they have no data.

The script is a little too complex to post here or I would have done it.

If I can just monitor where the variables are being altered, I can find my goofy code.

This is driving me nuts!

Please help me before I go insane!
urtrivedi
Following are the steps using which you can track your session variables.
1) You first find that how many php files is accessing/editing $_SESSION['yourvariable'], by searching for that text in your php files.

2) find file *.php with text $_SESSION['yourvariable']

3) then open all found files one by one having $_SESSION['yourvariable'] in your php editor

4) then search for $_SESSION['yourvariable']

5) then see whether any code line is unexpected

6) also look for session_start() in every page
rvec
instead of changing the value of the session variable directly use a session class to do it. That way you can use logging, tracing and debugging will become much easier.

Instead of this:
Code:
$_SESSION['var'] = 'val';

You'd have one class like this:
Code:
class Session {
  function __construct() {
    session_start();
  }

  function set_data($id, $val) {
    $_SESSION[$id] = $val;
    $this->log($id .' is set to '. $val);
  }

  function get_data($id) {
    if (isset($_SESSION[$id])) {
      return $_SESSION[$id];
    }
    else {
      return FALSE;
    }
  }

  function log($message) {
    //use mysql or some txt file here to log your messages
  }
}


And you'd use that to put data in your session variable. That way you can enable logging with a couple of lines, add flash-sessions or other nice session stuff easily. Also protection of sessions would be easier (if the ip or browser of the user changes or something like that).
richard270384
I like it!

Thanks for the suggestion.
richard270384
urtrivedi wrote:
Following are the steps using which you can track your session variables.
1) You first find that how many php files is accessing/editing $_SESSION['yourvariable'], by searching for that text in your php files.

2) find file *.php with text $_SESSION['yourvariable']

3) then open all found files one by one having $_SESSION['yourvariable'] in your php editor

4) then search for $_SESSION['yourvariable']

5) then see whether any code line is unexpected

6) also look for session_start() in every page


session_start is definitely there. I did do a search through all my files before I posted here, but couldnt find it in any of the wrong places...

I think rvec's suggestion will work best (although be time consuming)... but thanks for taking the time to write.
Stubru Freak
If it's your own server you can also try watching the file where PHP saves it. Normally this is in your tmp directory.
richard270384
Stubru Freak wrote:
If it's your own server you can also try watching the file where PHP saves it. Normally this is in your tmp directory.


I never thought of that... and yesit is my own server.
jmraker
I like to use a small program that does nothing but dumps the session to the screen (and put it where it's hard for site users to find)

Code:
<?php
session_start();

echo '<h1>Session ' . session_id() . '</h1><pre>';
var_dump($_SESSION);
echo '</pre>';
?>


Where you can run your program, then take a look at the session.

In the class you can add some magic constants like __LINE__ and __FUNCTION__ as parameters so you can log more data to narrow it down.
like
Code:
$sess->set_data('user_id', '123', __LINE__, __FUNCTION__);

http://us2.php.net/manual/en/language.constants.predefined.php

and also the session functions can be replaced to include logging, but it probably won't help much cause it replaces the start and end of the session functions, not the where was it changed.
http://us.php.net/manual/en/function.session-set-save-handler.php
Code:
richard270384
Thanks for all the help guys.

I went with rvec's suggestion...

I also used my new classes log function to log a whole crap load of program data to a log file....

Here is the funny part.... this is the offending code:

Code:
<body style="background: url('bg2.png'); background-repeat: repeat-x; margin:0px;" bgcolor="#963a3a">


It wasn't even php code that was causing me problems...

The image file bg2.png didnt exist... When bg2.png was requested a custom file-not-found page was being executed and returned to the browser...

The file-not-found page was overwriting the session data with blank values...

Smile
Related topics
How To : Improve Your PHP Programming
[RESOLVED] Need Help with pphlogger
[Resolved] Object Orientated PHP errors
[HELP] PHP -> Scripting
Needing a Bit Og Help {PHP}!
need some help - php switch code
[man][help]PHP...
help,php books
[RESOLVED] heady233: Help Setup My Home Network [RESOLVED]
Auto create tables
Please help. php script installation error
[HELP] PHP (and some MyBB) Administration
Session directory permissions issue
[Help] php script to post to twitter
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.