SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Advanced Sessions Processing

    All,

    I have this site, that does not want to trap and keep session & class vars.

    The back-end (SSH & FTP) are on a short time-out, so logging in is frequent, I wonder if the ISP also set the front-end the same, maybe explaining my problem, but not sure.

    A friend suggested I use MySQL to trap the class and session vars. He uses that method due to load balancing. Not sure if this fixes it, if front-end logout is forced.

    I have no experience doing such a thing, so am wondering where I can find good examples and HOWTOs. Did search, but didn't get much. Suppose I'm using the wrong key words.

    A little help here would be appreciated.

    Thanks!

    OMR

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How long do the session values currently persist for? How long do you need them to persist?

    do a phpinfo() and post the session.* values here(xxx out any file paths/names. Also post the Server API from phpinfo.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All,

    Working here and with IRC #php, so will try to answer all the Q's and add some more info I found:

    Q1:
    Am I on a shared web server?
    No 1.) Dev box is SO (Stand-alone) WAMP DT box, 2.) Test box is SO Ubuntu Server 7.10, 3.) Prod box is RHE4. All dedicated boxes housed at site warehouse.

    Q2:
    Do sessions work at all or do they work but are ending after a short time?
    Sessions work for about 15-30 minutes, but then go out.

    Q3:
    What exactly are the symptoms?
    Even though sessions are working vars are not holding or passing from page to page. Doing "var_dump" on dev box show all vars and values including the basket, but not passing to the "basket.php" file. On Test box the "var_dump" shows only a minimal set of vars and all shopping cart basket vars are empty.

    I've saved the screens, in both the states (toggling my checkbox) and from both the DT dev box and remote test box, to help you see my problem. First file has screens. Second file has the images.

    Q4:
    What are the settings for GC frequency in php.ini?
    Changed order as follows to sync settings on Dev DT and Test box.
    Code:
    ; variables_order = "EGPCS"
    variables_order = "GPCS"
    Found no frequency setting for these but found
    Code:
    max_execution_time = 30
    so assume sessions blow after 30 minutes, which is about when all is lost.

    Comments from LostBoy on Sessions Table:
    Its pretty simple and it should handle the time outs. I would approach this by creating a session table

    session table
    record_id PK autonumber
    user_hash user_id + current login timestamp to allow for multiple user records (unless you want to make a separate date field)
    session_data (either 1 field packed separated by some delimiter or multiple fields, its up to you)

    Then with each request, set the user_hash as a hidden field in the form or attached to the GET url in a link

    When the data comes back, look up the user based on the hash value and pull the session data out for validation

    Then generate the results adding the hash back into the page as above
    I generated the following SQL:
    Code:
    -- phpMyAdmin SQL Dump
    -- version 2.11.6
    -- http://www.phpmyadmin.net
    --
    -- Host: localhost
    -- Generation Time: Nov 18, 2008 at 01:36 PM
    -- Server version: 5.0.51
    -- PHP Version: 5.2.6
    
    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    
    
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;
    
    --
    -- Database: `whse`
    --
    
    -- --------------------------------------------------------
    
    --
    -- Table structure for table `session_trap`
    --
    
    DROP TABLE IF EXISTS `session_trap`;
    CREATE TABLE IF NOT EXISTS `session_trap` (
      `s_id`   int(11)     NOT NULL auto_increment COMMENT 'Session Unique Record',
      `s_sid`  varchar(40) NOT NULL default ''     COMMENT 'Session ID',
      `s_name` varchar(40) NOT NULL default ''     COMMENT 'Session Name',
      `s_uid`  varchar(40) NOT NULL default ''     COMMENT 'Session UserID',
      `s_uhs`  varchar(40) NOT NULL default ''     COMMENT 'Session UserID hash',
      PRIMARY KEY  (`s_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
    I know it's not complete, but a start, so chime in on what else you think I need here. Also am thinking of creating a "basket" table so all the shopping cart info goes there, then this will hold the info, even if the customer logs out and then returns.

    One of the scenarios I deal with, is the site has a printable order sheet, so customers come to the site, login, print the order sheet, go to their warehouse, hand write onto the printed order sheet, then come back to site to order. This process can span several hours, as most have large warehouses, with hundreds of the site products on their shelves.

    Thinking about my code, I have some changes to make this morning, will see what those do and get back to you here.

    Thanks!

    OMR
    Attached Files Attached Files

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All,

    I went to the DB driven session, it works better, but still have areas in my pages where session vars still are not working right.

    Mostly I have to add "if" statements to test which vars are not there and pull from DB, when not set. Still working through all this.

    Thanks!

    OMR

  5. #5
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,191
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Edit your php.ini file.

    Set error_reporting to E_ALL|E_STRICT and display_errors to On

    Most probably your problem is that your outputting something before you start the sessions or you have forgotten to start the session on the pages you got issues with. If the issue is the first, then the error reporting I mentioned above will generate and show an error when you try to load the page.

    Note. Only do this on your development server.

    Side note, if the pages are located on different sub domains, then that is your issue. Make certain the cookie is set to work across subdomains and you should be set to go.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    -You didn't need to store sessions in a database if all you wanted was for them to last longer. php's native files handler can accomodate that very easily by just changing a few settings.

    -Be aware that when using register_globals, $_SESSION['foo'] and $foo are the same variable. Changing the value of one, or unsetting one, does the same to the other. This is often a cause of "some variables not saving", because you probably changed the $foo variable, causing $_SESSION['foo'] to also be changed.

    -make sure that you call session_write_close() before redirecting a user. Otherwise you have little guarantee that any changes or additions to session values in that script will be saved if the users browser requests the new url quickly.

    -be aware that you need to solve the concurrency issue if you store sessions in a database. Otherwise you will occasionally have seemingly mysterious data loss if two seperate scripts which work with the same users session happen to execute at or near the same time.


Bookmarks

Posting Permissions

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