Re: session variable problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 3/25/2011 12:09 PM, markb wrote:
Very rusty with PHP.
We moved our web site to a new hosting service (godaddy). PHP changed
from 4x to 5.2.17.
I can no longer change $_SESSION variables after the first use.
First call to form - start session create variables
Second call - can read variables, change existing ones (but they do not
persist to next call) - cannot create new variable without an error

Using $_SESSION['varname'] always
session.auto_start off
register_globals off
session.use_only_cookies off

I assume this is setting difference but I can't seem to find it.




OK, here's is what is happening. I think my web hosting service is just really slow. I am storing the cookies in a unique directory. I create the directory when I start the session. It appears to take a long time for the directory creation to occur. The code is posted below. You can test it at this link: http://www.injection-moldings.com/upload%20test1.php

1.  First time you call,
	the session starts,
	variables assigned to $_SESSION  -
	 but no session ID ([PHPSESSID]) yet
2. Reload the page,
	 previous $_SESSION data lost
but "markdir" value appears in cookie which is the hash value for the session path.
	Still no session ID
3. Reload  -
	session ID appears [PHPSESSID]
	session data starts saving  $_SESSION['test'] will increment



Code:

<?php
ob_start(); //start output buffering



// 10-2007 added java download text to first page



if(!isset($_COOKIE['makedir']) ) {
		

		$unhashdir = startNewSession();	
		cfinitsession($_SESSION) ;		
		$_SESSION['mystep'] = 0;
		$_SESSION['SUBMIT'] == 'auto';
		$_POST = array();	
		setCookie('pagereload','0',0,'/');
		$_COOKIE['pagereload'] = 0;
		$_SESSION['pagereload']=0;
		echo '<p>starting new session';
} else {

	$unhashdir = '';
	if (!setupSession1($_COOKIE['makedir'],$unhashdir)) {
		die ('Could not start upload session');
	
		exit;
	}
	
	$sess = session_start(); //
	echo '<p>session running='.$sess;
		
}


/// hideme
echo '<pre>';
echo '<p>request uri'.$_SERVER['REQUEST_URI'];
if ($_SERVER['REQUEST_METHOD']=="POST") {
	$v = print_r($_POST,true);
	echo '<p>post:<br>'.$v;
}

	$v = print_r($_COOKIE,true);
	echo '<p>Cookie:<br>'.$v;
	
	$v = print_r($_SESSION,true);
	echo '<p>Session:<br>'.$v;
	echo '</pre>';
/// end hideme


if(isset($_COOKIE["PHPSESSID"]))
  {
    echo '<p></p>session started';
	if (isset( $_SESSION['test'])  ) {
	   $_SESSION['test'] ++;
		} else {
			$_SESSION['test'] =0;
		}
  }
  else
  {
    echo "<p>no session id";
  }

exit;

// **********************************************//
// Functions AND Sub-routines ///////////////////////////////////////////////
 // **********************************************//



function setupSession1($mydir,&$plaindir) {

 	// change the session save directory to our new folder
	// $ mydir is encrypted
	// $plaindir is unencrypted if available
	$rt = false;
	if ($plaindir != '' ) {
		$mydir = $plaindir;
	} else {
		$plaindir = 'upload/'.cfFindHashDir($mydir) ;
		$mydir = $plaindir;
	}
	if ($mydir == '') {
		$rt = false;
	} else {
$tempdir = $_SERVER['DOCUMENT_ROOT']."/".$mydir.'/tmpZrtg/fe234/dafsksfe' ;
	
			if( !is_dir($tempdir) ) {
				Rmkdir($tempdir);				
			}
		  if( is_dir($tempdir) ){
				ini_set( 'session.save_path',$tempdir );
		
ini_set('session.gc_maxlifetime',(60*3600*5.0)); //5.0 hours keep session alive X hours
		  		ini_set('session.gc_probability',1); //never empty the garbage???
		  		ini_set('session.gc_divisor',1);

		
			}
		$rt = is_dir($tempdir);
	}
	return $rt;
}	

function startNewSession() {
	$unhashdir = makeSaveDir();		
	// save SaveDir encrypted
	//log_error('New directory ='.$unhashdir);
		$newdir =cfSha256($unhashdir); //*&^
		
	//echo '<p>New session started!!:'.$sess." id:".session_id(); //
				
		$_SESSION['SaveDir'] = $newdir;
		ini_set("session.use_only_cookies", 0);  //do not use cookies only
		ini_set('session.cookie_secure',true);
		ini_set('session.use_cookies',0);
		
			setupSession1('',$unhashdir);							
		$sess = session_start();
		
		 setcookie('makedir',$newdir,0,'/');//(time()+(3600*6)),"/");
	return $unhashdir;	
}


function cfinitvar(&$var,$value) {
  if (!isset($var) ) {
  	  $var= $value;
	 }
}

//function cfinitsession(&$_SESSION) {
function cfinitsession($_SESSION) {
   cfinitvar($_SESSION['file_upload_number'],'');
  cfinitvar($_SESSION['email_fld'],'');
  cfinitvar($_SESSION['first_name_fld'],'');
  cfinitvar($_SESSION['position_fld'],'');
  cfinitvar($_SESSION['company_fld'],'');
  cfinitvar($_SESSION['addr1_fld'],'');
  cfinitvar($_SESSION['addr2_fld'],'');
  cfinitvar($_SESSION['city_fld'],'');
  cfinitvar($_SESSION['state_fld'],'');
  cfinitvar($_SESSION['zip_fld'],'');
  cfinitvar($_SESSION['country_fld'],'');
  cfinitvar($_SESSION['phone_fld'],'');
  cfinitvar($_SESSION['fax_fld'],'');
  cfinitvar($_SESSION['quote_ref_fld'],'');
  cfinitvar($_SESSION['quote_due_fld'],'');
  cfinitvar($_SESSION['last_name_fld'],'');
  cfinitvar($_SESSION['SaveDir'],'');
  cfinitvar($_SESSION['pagereload'],'');
 // cfinitvar($_SESSION[''],'');

 $_SESSION['mystep'] = intval(0) ;
		$_SESSION['SUBMIT'] = 'auto';
		$_SESSION['SUBMIT2'] = '';
		$_SESSION['goTo'] = '';
		$_SESSION['intoverride'] = false;

}


 function makeSaveDir() {
 	
	$strt = false;
	$timestamp =  strtotime("now");
	$newdir = "upload/$timestamp";
	
	if(!is_dir($newdir)) {
		$oldumask = umask();
		mkdir($newdir, 0722, true);
		umask($oldumask);
	}
	return $newdir;
}



function cfFindHashDir($dir) {
	//$dir is encrypted with cfSha256
	
	$rt = '';
	$dirar = cfscandir( PATHTOUPLOAD,true,false) ;
	//go backwards through dir names to find it
	$f = print_r($dirar,true);
	//log_error('dirar'.$f);
	//log_error('dirar size'.count($dirar));
	//log_error('Dir = '.$dir);
	foreach ($dirar as $dirname=>$size) {
		//log_error($dirname.'-'.cfSha256($dirname));
		if ( cfSha256('upload/'.$dirname) == $dir  ) { $rt = $dirname; break; }
	}
	
	return $rt;
}

function cfSha256($data) {
	return (base64_encode(bin2hex(mhash(MHASH_SHA256,$data) ) ) );
}	

	
function cfscandir( $dir,$listDirectories=false, $skipDots=true) {
    $dirArray = array();
// echo '<P>-- dir='.$dir.'!!<br>';	
	if (file_exists($dir)) {
    if ($handle = opendir($dir)) {
        while (false !== ($file = readdir($handle))) {
            if (($file != "." && $file != "..") || $skipDots == true) {
				
if($listDirectories == false) { if(is_dir($dir."/".$file)) { continue ; } }
				  $dirArray[$file]  = filesize($dir.$file) ;
            }
        }
        closedir($handle);
    }
	 }
    return $dirArray;
}

function Rmkdir($path){  //recursive make directory
    $exp=explode("/",$path);
    $way='';
    foreach($exp as $n){
        $way.=$n.'/';
        if(!file_exists($way)) {
		  //	  echo "<br>hello<br>making dir:".$way;
		 //    echo "<br>root:".$_SERVER['DOCUMENT_ROOT'];
			//  echo "<br>self:".$_SERVER["PHP_SELF"];
			
            mkdir($way, 0777);
				}
    }
}

?>

--
Mark B

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux