Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Session Problem

  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Session Problem

    All,

    Having a problem with sessions.

    I created the following files:
    Code:
    <?php
    /*******************************************************************************/
    /*    File: reg_vars.php                 Usage: include ('reg_vars.php');      */
    /*  Author: Nyle E. Davis          Create Date: 11/04/08                       */
    /*  Mod by: Nyle E. Davis             Mod Date: 12/02/08                       */
    /* Purpose: Register the Class and Session variable and their values.          */ 
    /*******************************************************************************/
    /* Mod History: 12/02/08                                                       */  
    /* Attempt to correct problem with 'session_start();' errors.                  */
    /*******************************************************************************/
    	if (!class_exists('customer')) include 'classes.inc.php';
    	if (!function_exists('dbConnect')) include 'dblib.inc.php';
    	include 'header.inc.php';
    	$newOrderID = date("ymdHis");
    	$sesNameID =  "C-Wines".date("ymd");
    	session_name($sesNameID);
    	if (!session_is_registered ($sesNameID)) {
    		session_start();		// Moved here 11/24/08 to avoid load issues.
    	}
    	$sn = session_name($sesNameID);
    	$si = session_id();
    	$tn = $_SESSION['name'];
    	$ti = $_SESSION['sid'];
    echo "SN=> $sn SI=> $si TN=> $tn TI=> $ti <br>";
    	$login = $_POST["login"];
    	$passwd = $_POST["passwd"];
    	if(empty($login) || empty($passwd)) {
    		unset($status);
    	} else {
    		$status 		  = 'logged';
    	}
    	if ($status=='logged') {
    		$auth = "$TRUE";
    		$l_aid = $login;
    		$l_pid = $passwd;
    		$l_sta = $status;
    		$l_ses = $sesNameID;
    		$log  = new login();
    		$log->setVar($login, $LOGID);
    	    $log->setVar($passwd, $LOGPD);
    		$log->setVar($status, $LOGST);
    	    $log->setVar($sesNameID, $LOGSES);
    		$_SESSION["log"] = $log;
    		$_SESSION["auth"] = $auth;
    		dbConnect();
    		$query = sprintf("select * from customers where userid = %s and passwd = '%s'", $login, $passwd);
    		$result = dbQuery($query);
    		$found = dbNRows($result);
    		if($found==1) {
    			$cust = new customer();
    			$basket = array();
    		    $items = array();
    			$c_aid = dbRes($result, 0, "userid");
    			$c_nm1 = dbRes($result, 0, "name1");
    			$c_nm2 = dbRes($result, 0, "name2");
    			$c_1ad = dbRes($result, 0, "address1");
    			$c_2ad = dbRes($result, 0, "address2");
    			$c_zsc = dbRes($result, 0, "state_zip");
    			$c_tat = dbRes($result, 0, "active");
    			$c_vid = dbRes($result, 0, "division");
    			$c_dio = $newOrderID;
    			$c_pyt = dbRes($result, 0, "price_type");
    			$cust->setVar($c_aid, $ACCTNUMBER);
    		    $cust->setVar($c_nm1, $ACCTNAME);
    		    $cust->setVar($c_nm2, $ACCTNAME2);
    		    $cust->setVar($c_1ad, $ACCTADDRESS);
    		    $cust->setVar($c_2ad, $ACCTADDRESS2);
    		    $cust->setVar($c_zsc, $ACCTSTATEZIP);
    		    $cust->setVar($c_tat, $ACCTACTIVE);
    		    $cust->setVar($c_vid, $ACCTDIVISION);
    		    $cust->setVar($c_dio, $ORDERID);
    		    $cust->setVar($c_pyt, $ACCTTYPE);
    			$_SESSION["cust"] = $cust;
    			$_SESSION["basket"] = $basket;
    			$_SESSION["items"] = $items;
    			$_SESSION["custItems"] = $custItems;
    		}	// end if $found
    	}		// end if $status
    	session_write_close();
    ?>
    Code:
    <?php
    /*******************************************************************************/
    /*    File: get_vars.php                 Usage: include ('get_vars.php');      */
    /*  Author: Nyle E. Davis          Create Date: 11/04/08                       */
    /*  Mod by: Nyle E. Davis             Mod Date: 12/02/08                       */
    /* Purpose: Get the registered Class and Session variables and their values.   */ 
    /*******************************************************************************/
    /* Mod History: 12/02/08                                                       */  
    /* Attempt to correct problem with 'session_start();' errors.                  */
    /*******************************************************************************/
    //	session_start();
    	if (!class_exists('customer')) include 'classes.inc.php';
    	if (class_exists('customer')) {
    		$cust 	= new customer();
    		$c_cid  = $cust->getVar($ACCTNUMBER);
    		$c_div  = $cust->getVar($ACCTDIVISION);
    		$c_nam  = $cust->getVar($ACCTNAME);
    		$c_nm2  = $cust->getVar($ACCTNAME2);	
    		$c_ad1  = $cust->getVar($ACCTADDRESS);
    		$c_ad2  = $cust->getVar($ACCTADDRESS2);
    		$c_csz  = $cust->getVar($ACCTSTATEZIP);
    		$c_typ  = $cust->getVar($ACCTTYPE);
    		$c_oid  = $cust->getVar($ORDERID);
    	}
    	if (!$cust->getVar($ACCTNUMBER)) {
    		$cust = $_SESSION['cust'];
    		foreach ($cust as $key => $val) {
    //			echo "K=> $key V=> $val <br>";
    			switch($key) {
    				case 'acctNumber':
    					$c_cid  = $val;
    					break;
    				case 'acctDivision':
    					$c_div  = $val;
    					break;
    				case 'acctName':
    					$c_nm1  = $val;
    					break;
    				case 'acctName2':
    					$c_nm2  = $val;
    					break;
    				case 'acctAddress':
    					$c_ad1  = $val;
    					break;
    				case 'acctAddress2':
    					$c_ad2  = $val;
    					break;
    				case 'acctStateZip':
    					$c_csz  = $val;
    					break;
    				case 'acctType':
    					$c_typ  = $val;
    					break;
    				case 'orderID':
    					$c_oid  = $val;
    					break;
    			}	// end switch
    		}		// end foreach
    	}			// end if !$cust
    	$cust_inf = array('acctNumber'=>$c_cid,'acctDivision'=>$c_div,'acctName'=>$c_nm1,
    					  'acctName2'=>$c_nm2,'acctAddress'=>$c_ad1,'acctAddress2'=>$c_ad2,
    					  'acctStateZip'=>$c_csz,'acctType'=>$c_typ,'orderID'=>$c_oid);
    	$_SESSION['$cust_inf'] = $cust_inf;
    	
    	if (class_exists('login')) {
    		$log  	= new login();
    		$l_uid	= $log->getVar($LOGID);
    		$l_pwd	= $log->getVar($LOGPD);
    		$l_sta	= $log->getVar($LOGST);
    		$l_ses	= $log->getVar($LOGSES);
    	}
    	if (!$log->getVar($LOGID)) {
    		$log = $_SESSION['log'];
    		foreach ($log as $key => $val) {
    //			echo "K=> $key V=> $val <br>";
    			switch($key) {
    				case 'log_id':
    					$l_uid  = $val;
    					break;
    				case 'log_pd':
    					$l_pwd  = $val;
    					break;
    				case 'log_status':
    					$l_sta  = $val;
    					break;
    				case 'log_session':
    					$l_ses  = $val;
    					break;
    			}	// end switch
    		}		// end foreach
    	}
    	$log_inf = array('log_id'=>$l_uid,'log_pd'=>$l_pwd,'log_status'=>$l_sta,
    					  'log_session'=>$l_ses);
    	$_SESSION['$log_inf'] = $log_inf;
    	session_write_close();
    	include 'reg_vars.php';
    ?>
    I'm developing on Windows box running WAMP, but host test site on Ubuntu 7.10 server. Production environment is RHE4, so have to ensure PHP4 compatibility.

    Code works fine on dev box, but get the errors:
    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /var/www/test/html/reg_vars.php:15) in /var/www/test/html/reg_vars.php on line 9
    when running on test box.

    I put in session test (lines 17-19) to attempt making sure the session only opens once
    Code:
    	if (!session_is_registered ($sesNameID)) {
    		session_start();		// Moved here 11/24/08 to avoid load issues.
    	}
    but still get the errors. I even changed all my 'include ()' statements for these files to 'include_once()'.

    I have to call the reg_vars and get_vars routines several times as my shopping cart is housed in an IFRAME and vars do not pass well without these being re-called. I had to add the 'session_write_close();' commands to each file, as the IFRAME would not work without them.

    Not sure what to look for next. Wondering what I need to add/edit to fix this? Would changing my 'include()' statements to 'require()' statement resolve this since there is less error checking?

    Will appreciate all your help.

    Thanks!

    OMR

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You may be able to bypass this warning by buffering any and all output until the very end of the script. Stick a call to ob_start() at the beginning of the script to start output buffering. You don't really need to flush the buffer explicitly; the end of the script will flush it, but you can if you want.

    http://us2.php.net/manual/en/function.ob-start.php

  • #3
    New Coder
    Join Date
    Dec 2005
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    You may be able to bypass this warning by buffering any and all output until the very end of the script. Stick a call to ob_start() at the beginning of the script to start output buffering. You don't really need to flush the buffer explicitly; the end of the script will flush it, but you can if you want.

    http://us2.php.net/manual/en/function.ob-start.php
    Cool, worked great, see where to dump in the howto link you sent as well!

    Kudos!!!!

    Thanks again!!!

    OMR

  • #4
    New Coder
    Join Date
    Dec 2005
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    One more error

    Fumigator,

    Your post worked for most but now have this PHP4 compatibility error:
    Warning: session_write_close() [function.session-write-close]: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in /var/www/test/html/reg_vars.php on line 81
    Any suggestions on clearing this?

  • #5
    New Coder
    Join Date
    Dec 2005
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Found

    Duh,

    Found these in the php.ini file and changed from 1 to 0.

    OMR

  • #6
    New Coder
    Join Date
    Dec 2005
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Partially Working

    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


  •  

    Posting Permissions

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