开发者

session having different session ids on different pages of single domain, non secure pages!

开发者 https://www.devze.com 2023-02-19 04:21 出处:网络
I\'m pulling my hair out over this one. I have tried to make a simple script to store sessions so when a user closes there browser, they can come back later and their shopping basket will still be in

I'm pulling my hair out over this one. I have tried to make a simple script to store sessions so when a user closes there browser, they can come back later and their shopping basket will still be in tact. This all seemed to be going fine until i noticed that on some items the basket was containing same items as the previous. After some checks I noticed the session id was different on these odd pages! Here's my code which sits at the top of my framework.

<?php

session_start();
function sessions(){
    if( ! isset( $_COOKIE['PHPSESSID'] ) ) {
        setcookie( "PHPSESSID", session_id(), strtotime('+ 30 days') );
    }else{
        $con = Database::getInstance();
        if( session_id() != $_COOKIE['PHPSESSID'] ) {
            $re = $con->query( "SELECT * FROM `" . TABLE_PREFIX . "_tbl_sessions` WHERE session_id = '" . $_COOKIE['PHPSESSID'] . "'" );
            if( $re->num_rows != 0 ) {
                $ar = $re->fetch_assoc();
                $id = $ar['id'];
                $_SESSION['basket'] = unserialize( stripslashes( $ar['basket'] ) );
                $con->query("UPDATE `" . TABLE_PREFIX . "_tbl_sessions` SET session_id = '" . session_id() . "' WHERE id = '$id' " );
            }
            unset($_COOKIE['PHPSESSID']);
            setcookie( "PHPSESSID", session_id(), strtotime('+ 30 days') );
            header('Location: ' . get_base_url() );
        }else{
            $re = $con->query( "SELECT * FROM `" . TABLE_PREFIX . "_tbl_sessions` WHERE session_id = '" . $_COOKIE['PHPSESSID'] . "'" );
            if( $re->num_rows != 0 ) {
                $ar = $re->fetch_assoc();
                $id = $ar['id'];
                if( ! empty( $_SESSION['basket'] ) ) {
                    $con->query("UPDATE `" . TABLE_PREFIX . "_tbl_sessions` SET session_id = '" . $_COOKIE['PHPSESSID'] . "', data = '" . addslashes( serialize( $_SESSION['basket'] ) ) . "' WHERE id = '$id'" );
                }else{
                    $con->query( "DELETE FROM `" . TABLE_PREFIX . "_tbl_sessions` WHERE id = '$id'" );  
                }
            }else{
                if( ! empty( $_SESSION['basket'] ) ) {
                    $con->query( "INSERT INTO `" . TABLE_PREFIX . "_tbl_sessions` ( `session_id`, `stamp`, `data`) VALUES ( '" . $_COOKIE['PHPSESSID'] . "', NOW(), '" . addslashes( serialize( $_SESSION['basket'] ) ) . "' )" );  
     开发者_JAVA百科           }
            }
        }
    }
    echo 'cookie: ' . $_COOKIE['PHPSESSID'] . ' : session(): ' . session_id();
}

?>

any help on this matter is much appreciated.

** EDIT **

i've tried to make it more simpler but still same problem

<?php

function sessions(){
    $con = Database::getInstance();
    if( session_id() == '' ) {
        if( isset( $_COOKIE['session_id'] ) ) {
            session_start();
            $re = $con->query( "SELECT * FROM `" . TABLE_PREFIX . "_tbl_sessions` WHERE session_id = '" . $_COOKIE['session_id'] . "'" );
            if( $re->num_rows != 0 ) {
                $ar = $re->fetch_assoc();
                $id = $ar['id'];
                if( session_id() != $_COOKIE['session_id'] ) {
                    $_COOKIE['session_id'] = session_id();
                    $con->query("UPDATE `" . TABLE_PREFIX . "_tbl_sessions` SET session_id = '" . session_id() . "' WHERE id = '$id' " );
                    $_SESSION['basket'] = unserialize( stripslashes( $ar['data'] ) );
                }else{
                    if( isset( $_SESSION['basket'] ) ) {
                        $con->query("UPDATE `" . TABLE_PREFIX . "_tbl_sessions` SET data = '" . addslashes( serialize( $_SESSION['basket'] ) ) . "' WHERE id = '$id' " );
                    }
                }   
            }else{
                $con->query( "INSERT INTO `" . TABLE_PREFIX . "_tbl_sessions` ( `session_id`, `stamp`, `data`) VALUES ( '" . $_COOKIE['session_id'] . "', NOW(), '' )" );       
            }
        }else{
            session_start();
            setcookie( "session_id", session_id(), strtotime('+ 30 days') );
            $_COOKIE['session_id'] = session_id();
        }
    }else{
        die('session has previously been created'); 
    }
    echo 'cookie: ' . $_COOKIE['session_id'] . ' : session(): ' . session_id();
}

?>


<?php

function sessions(){
    $con = Database::getInstance();
    if( session_id() == '' ) {
        session_start();
        if( isset( $_COOKIE['session_id'] ) ) {
            $re = $con->query( "SELECT * FROM `" . TABLE_PREFIX . "_tbl_sessions` WHERE session_id = '" . $_COOKIE['session_id'] . "'" );
            display_error( $con );
            if( $re->num_rows != 0 ) {
                $ar = $re->fetch_assoc();
                $id = $ar['id'];
                if( session_id() != $_COOKIE['session_id'] ) {
                    setcookie( "session_id", '', strtotime('- 30 days'), '/', 'localhost' );
                    setcookie( "session_id", session_id(), strtotime('+ 30 days'), '/', 'localhost' );
                    $con->query("UPDATE `" . TABLE_PREFIX . "_tbl_sessions` SET session_id = '" . session_id() . "' WHERE id = '$id' " );
                    display_error( $con );
                    $_SESSION['basket'] = unserialize( stripslashes( $ar['data'] ) );
                }else{
                    if( isset( $_SESSION['basket'] ) ) {
                        $con->query("UPDATE `" . TABLE_PREFIX . "_tbl_sessions` SET data = '" . addslashes( serialize( $_SESSION['basket'] ) ) . "' WHERE id = '$id' " );
                        display_error( $con );
                    }
                }   
            }else{
                $con->query( "INSERT INTO `" . TABLE_PREFIX . "_tbl_sessions` ( `session_id`, `stamp`, `data`) VALUES ( '" . $_COOKIE['session_id'] . "', NOW(), '' )" );       
                display_error( $con );
            }
        }else{
            setcookie( "session_id", session_id(), strtotime('+ 30 days'), '/', 'localhost' );
            $_COOKIE['session_id'] = session_id();
        }
    }else{
        die('session has previously been created'); 
    }
    echo 'cookie: ' . $_COOKIE['session_id'] . ' : session(): ' . session_id();
}

function display_error( $con ) {
    if( isset( $con->error ) && $con->error != '' ) {
        die( $con->error );
    }
}

?>

The Above works! $_COOKIES['foo'] = 'bar' *does not* re-value the cookie in the browser only during the script.

The other problem was needing to set the path and domain of the cookie to stop multipul cookies being created. Works and runs smoothly!

0

精彩评论

暂无评论...
验证码 换一张
取 消