SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Everyone:

    This is regarding session_destroy().

    I have read that session_destroy() is unreliable and does not work properly often times.

    Nevertheless I tested it out, but I get an error message. I did session_start() before it (at the very beginning) and I did session_unregister() all my variables, but I get this:

    Code:
    Warning: Session object destruction failed in c:\phpdev2\www\php autho\logout.php on line 9
    line 9:
    Code:
    session_destroy()
    Any ideas? (or alternate ways to get rid of sessions)
    Jon.

  2. #2
    SitePoint Evangelist
    Join Date
    May 2000
    Location
    Canada
    Posts
    533
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i wouldnt know why that wouldnt work .. never really had too many problems with sessions ... you could always store session id's in database , check them periodically, expire them in the database, if expired, it deletes the files in the session directory
    cogito, ergo sum

  3. #3
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Vinay. How do I delete the files in the session directory? I need this for a user logging out.

  4. #4
    Non-Member
    Join Date
    Apr 2000
    Location
    Waco, Texas.
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have had the same problem as you, and have worked around it a little.

    I use:
    session_unset($username);
    session_unset($password);
    header("Location: $somewhere_else");

    In all of my login scripts, I always validate to make sure $username and $password are valid session variables, and if not, the script will not go on. With the code above, the script will not go on because $username and $password are now null.


  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or use MySQL to store session data, you can get a script to do this off hotscripts.com Then on your login page you can just delete from sessions any data that has the current session id.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies everyone.

    I didn't know about session_unset()...thanks. I think I will try robp's suggestion. I'd rather not get another mysql table involved .

    Jon.


  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem.. Just think about this, it takes about 5 times as long to open a file on the server to read session data than it does to read it from a database.

    BTW you don't have to change any of your scripts, you just change the way that PHP natively handles sessions. By default when you call session_start() it reads the data in from the file, PHP knows what to do wit hit, you can alternatively set how php calls the data so you can have php get data from a mysql table when you call session_start() you wouldn't even need to change any of your scripts, you would merely just include one file that would tell PHP how to handle sessions.
    <Edited by freddydoesphp on 01-21-2001 at 02:39 PM>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Freddy, I'm interested in knowing how you would do that?
    Thanks
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Son here is one that I wrote using the mysql db class from phplib

    name this session.php (note the $db object that I use in my session functions, if you are familiar with PHPlib and know how to use the db class then this will work if not you may need to adjust the functions a bit.

    session.php
    <?


    $lifetime = get_cfg_var("session.gc_maxlifetime");


    function sess_open($save_path, $session_name) {
    global $db;
    return true;
    }

    function sess_close() {
    return true;
    }

    function sess_read($key) {
    global $db;
    $q = sprintf("SELECT value FROM sessions WHERE sesskey = '%s' AND expire > %s", $key, time());
    $db->query($q);
    if ($db->num_rows()) {
    $db->next_record();
    return stripslashes($db->f(value));
    }
    else {
    return false;
    }
    }

    function sess_write($key, $val) {
    global $db, $lifetime;
    $expire = time() + $lifetime;
    $value = addslashes($val);
    $q = sprintf("UPDATE sessions SET expire = %s, value = '%s' WHERE sesskey = '%s' AND expire > %s", $expire, $value, $key, time());
    $db->query($q);

    if (!$db->affected_rows()) {
    $q = sprintf("INSERT INTO sessions VALUES ('%s', %s, '%s')", $key, $expire, $value);
    $db->query($q);
    }

    return $db->affected_rows();
    }

    function sess_destroy($key) {
    global $db;
    $db->query(sprintf("DELETE FROM sessions WHERE sesskey = '%s'", $key));;
    return $db->affected_rows();
    }


    function sess_gc($maxlifetime) {
    global $db;
    $db->query(sprintf("DELETE FROM sessions WHERE expire < %s", time()));;
    return $db->affected_rows();
    }

    session_set_save_handler(
    "sess_open",
    "sess_close",
    "sess_read",
    "sess_write",
    "sess_destroy",
    "sess_gc");

    ?>



    Then on pages where I need session management

    include("db.php");
    include("session.php");
    $db = new DB;
    session_start();



    You need to have a table named sessions with the following defs



    CREATE TABLE sessions (
    sesskey varchar(32) NOT NULL default '',
    expire int(11) unsigned NOT NULL default '0',
    value text NOT NULL,
    PRIMARY KEY (sesskey)
    )
    <Edited by freddydoesphp on 01-21-2001 at 07:24 PM>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •