Results 1 to 11 of 11

Thread: Session being cleared?

  1. #1
    Join Date
    Jun 2010

    Session being cleared?

    Hey all, I am having a really strange issue.

    Recently we just moved our server across to a new platform, which has PHP 5.2.13 (While I have been using 5.2.11 previously)

    Anyhow, for some reason on the search page (and the search page alone) the session is cleared after the first viewing, the session_id() isn't being changed, session_start() is returning true. I have tried it on other pages, and the session data remains true (I added a print_r($_SESSION); and data was shown on ALL THE PAGES, even the first run of search.php but after that the session was wiped out)

    There is NO session clearing code ANYWHERE, the only session_destroy() is in logout.php, on the development server - the search.php DOES NOT wipe out the session, I can refresh the browser and it will remain logged in.

    Sorry for trying to reiterate ... Been trying to figure out whats going on, I'm giong to download 5.2.13 and see if the problem occurs on the development server.

    I haven't contacted the host yet, since I don't know what to exactly write to them ...

    edit: Ok I installed php5.2.13, and nope - it does not clear the session on the search.php page....

    and yes the live server is running all latest.
    Last edited by adamou; 07-20-2010 at 04:59 PM.

  2. #2
    Join Date
    Jun 2010
    Ok I've followed the code and it is a smaller file which is wiping out all of the session - but this small file does not touch the session at all ... I am ultimately confused, haha...

    I will try remove part by part of the code and report back if I find anything; I even tried a ctrl + f and could not find anything with the word session in it

    ~ It works perfectly on the development server however, the settings are untweaked .. and it worked fine on the previous live server, the settings were xfered over, its just running 5.2.13 now.

    The only thing this small script access is the database, but there is database access everywhere
    Last edited by adamou; 07-20-2010 at 05:37 PM.

  3. #3
    Join Date
    Jun 2010
    agh... I can't pin point where its going wrong!

    I removed securimage, and the sessions weren't cleared - then I made an AJAX request to add to watchlist, that clears the session. So I put back in my imagecaptcha and that clears out the session (So something in there is clearing it out - but I have never ever ever had a problem with it until now, just on this server)

    I still don't know what setting is different ... I might just end up copying my php.ini over to the server in the end out of desperation :x

    I contacted server host, no word yet but I assume they will look into it ... I hope...

  4. #4
    Join Date
    Jun 2010
    Ok so annoyed at this problem (host can't seem to fix it, works on other machine -.-) I decided to write my own session handler ... and I think its better off in the end.

    PHP Code:
    session implements ArrayAccess   {
    $id null;
    $arr = array();
    $any_changes false;
    __construct() {
    //first lets check if there is a cookie set    
    if(!headers_sent()) {
    $_COOKIE[SESS_COOKIE]) && is_numeric($_COOKIE[SESS_COOKIE])) {
    $r mysql_query("select id,val from sessions where sess_key = '{$_COOKIE[SESS_COOKIE]}' and ip = '{$_SERVER['REMOTE_ADDR']}'");
    $d mysql_fetch_assoc($r)) {
    //means we successfully read from it
                            //therefore update the time.
    mysql_query("update sessions set time = UNIX_TIMESTAMP(now()) + 600 where id = '{$d['id']}'");
    $this->id $d['id'];
    $d['val'])) $this->arr unserialize(stripslashes($d['val'])); 
                        } else {
    //just create another session
    mysql_query("insert into sessions (`sess_key`,`ip`,`time`) values ('{$_COOKIE[SESS_COOKIE]}','{$_SERVER['REMOTE_ADDR']}',UNIX_TIMESTAMP(now()) + 600)");
    $this->id mysql_insert_id();
                    } else {
    mysql_query("insert into sessions (`sess_key`,`ip`,`time`) values ('{$_COOKIE[SESS_COOKIE]}','{$_SERVER['REMOTE_ADDR']}',UNIX_TIMESTAMP(now()) + 600)");
                } else {
    //headers have been sent. Failed
    return false;
    save() {
    $this->any_changes) {
    $d addslashes(serialize($this->arr));
    mysql_query("update sessions set val = '{$d}', time = UNIX_TIMESTAMP(now()) + 600 where id = '{$this->id}';");
    $this->any_changes false;
    __destruct() {
            static function 
    createCookie() {
    //easier to manage
    $cid self::generateId();
    //just to avoid this annoying thing..
    $_COOKIE[SESS_COOKIE] = $cid;

            static function 
    generateId() {
    //since it is an IP based sessiobn we're just going to use a random number to set the session_key
    $id number_format( (rand() * 20000),0,'','');
    $res mysql_query("select id from sessions where sess_key = '{$id}'");
    $d mysql_fetch_assoc($res)) {
            public function 
    offsetSet($offset$value) {
    $this->arr[$offset] = $value;
    $this->any_changes true;
            public function 
    offsetExists($offset) {
                return isset(
            public function 
    offsetUnset($offset) {
    $this->any_changes true;
            public function 
    offsetGet($offset) {
                return isset(
    $this->arr[$offset]) ? $this->arr[$offset] : null;
    CREATE TABLE `sessions` (                                
                `id` int(2) unsigned NOT NULL AUTO_INCREMENT,          
                `sess_key` int(4) unsigned NOT NULL,                   
                `ip` varchar(35) NOT NULL,                             
                `val` char(100) NOT NULL,                              
                `time` int(5) unsigned NOT NULL,                       
                PRIMARY KEY (`id`,`sess_key`,`ip`)                     
              ) ENGINE=MEMORY
    Could do with some more work but its acceptable atm,

    I just run a cron job every 15 minutes to clear out all of the expired sessions.

    Can't do much flash with my session class (no foreach) but, shouldn't really have to in the first place; if you want to use it do whatever you will to it

    usage :

    PHP Code:
        $_SESSION= new session();
    $_SESSION['id'] = 6
    though edit to add; is there a particular reason why I shouldn't override $_SESSION ?
    Last edited by adamou; 07-21-2010 at 08:31 PM. Reason: changed extends ArrayObject to implements ArrayAccess

  5. #5
    Join Date
    Aug 2004
    If you override $_SESSION like that, it will no longer be "super-global" and instead will be locally scoped like any other user-defined variable.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  6. #6
    Join Date
    Jun 2010
    oh thats a bummer .. is there a way to keep it as a superglobal, I suppose I could just re-write it as the save handler thing

  7. #7
    Join Date
    Jun 2010

    PHP Code:
        $_SESSION = new session();
    $_SESSION['id'] = '6';
    test() {
    results in 6

  8. #8
    Join Date
    Jun 2010


    session data on my server now works !!!

    Sorry just overly excited, it seems $_SESSION remains a super global even if over-ridden? Regardless I am supberbly happy now.

    Also becareful of your rand() function, you might need to set the sess_key bigger
    Last edited by adamou; 07-21-2010 at 08:48 PM.

  9. #9
    Join Date
    Aug 2004
    Hmm...curious, I wonder if something changed in newer versions of PHP or if I'm just misremembering something, because I was sure I saw something in the manual warning against overwriting the entire $_SESSION array.

    Anyway, if it would be of any interest to you, you might check out my article: Implementing a Database-based Session-handler.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  10. #10
    Join Date
    Aug 2004
    PS: Another "gotcha" that can cause a difference on different hosts is to watch out for pages accessed with or without the "www." sub-domain in the URL. Depending on your PHP settings, the session cookie may not carry over from the more specific "www.example.com" to the "example.com" URL. This problem can be avoided by setting session.cookie_domain to ".example.com" (note the leading "."), or else using URL rewriting at the web server to redirect all "example.com" requests to "www.example.com".
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  11. #11
    Join Date
    Jun 2010
    Of course I suppose ideally I should just use the session handler functions, but I think I will keep it like this for a little while (Its working and just in the nick of time); I barely had to change any of my existing code, except for things like session_id()

    Though thanks for the cookie advice

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.39385 seconds
  • Memory Usage 3,007KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (3)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (11)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (11)postbit
  • (11)postbit_onlinestatus
  • (11)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates