Page 1 of 1

session problem

Posted: Fri Jul 02, 2004 7:57 am
by udwo
Bech100 | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

i am using the following code to insert new user into my db file:

Code: Select all

function sanitize($variable){
	$variable = str_replace("",";",$variable);
	if(!get_magic_quotes_gpc()){
		$variable = addslashes($variable);
	}
	return $variable;
}
function checkPassword($password1,$password2,$minPasswordLength){
	$result =0;
	if(strlen($password1) >= $minPasswordLength){
		if($password1 == $password2){
			$result = $password1;
		}
	}
	return $result;
}

mysql_select_db($database_slovakvideo, $slovakvideo);
$query_rsChannels = "SELECT tblchannels.videoSourceID, tblchannels.stationName FROM tblchannels WHERE tblchannels.stationActive = '1' ORDER BY tblchannels.stationName DESC";
$rsChannels = mysql_query($query_rsChannels, $slovakvideo) or die(mysql_error());
$row_rsChannels = mysql_fetch_assoc($rsChannels);
$totalRows_rsChannels = mysql_num_rows($rsChannels);

// PHPLS10 - Check for Unique Username and Insert - Part 2
if($HTTP_POST_VARS['action'] == "check"){
	
	$minPasswordLength = 6;
	$pPassword1 = sanitize($HTTP_POST_VARS['password']);
	$pPassword2 = sanitize($HTTP_POST_VARS['passConfirm']);
	$pUsername = sanitize($HTTP_POST_VARS['userid']);
	$pPassword = checkPassword($pPassword1, $pPassword2, $minPasswordLength);
	if((strlen($pUsername)>0) && ($pPassword !== 0)){
		$rsCheckSQL = "SELECT userid FROM users WHERE userid='" . $pUsername . "'";	
		mysql_select_db($database_slovakvideo, $slovakvideo);
		$rsCheck = mysql_query($rsCheckSQL, $slovakvideo) or die(mysql_error());
		$totalRows_rsCheck = mysql_num_rows($rsCheck);
		if($totalRows_rsCheck < 1){
			$rsCheckSQL = "INSERT INTO users (userid, PASSWORD) VALUES ('" . $pUsername . "',MD5('" . $pPassword . "'))";
			$rsCheck = mysql_query($rsCheckSQL, $slovakvideo) or die(mysql_error());
			session_start();
			session_register("userid");
			$HTTP_SESSION_VARS['userid'] = $pUsername;
			header("Location: newUserDetails.php");
		} else {
			mysql_free_result($rsCheck);
			$phplsMessage = "User name already exists.";
		}
	}else{
		$phplsMessage = "Password has to be at least 6 characters.";
	}
}
Problem is that after the record is inserted into the db the code should set a session variable and redirect to a page where user sets the details for the account like address and so on... this variable doesn't always get set. i have to reload the page in order to set the variable... does this have something to do with cache? i am at lost...

Bech100 | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Fri Jul 02, 2004 8:03 am
by anjanesh
If this works OK after you refresh the page then its definitely because of cache. Include this in the first line (before you start outputing anything - even <HTML>,<HEAD> tags etc)

Code: Select all

<?php
header("Cache-Control: no-store, no-cache, must-revalidate");
?>
.
.
.
.
Rest of script code

Posted: Fri Jul 02, 2004 9:15 am
by udwo
ok, i tried that and did not help...

i also noticed something i did not see before. when i preview the output of the browser i see that there are som session IDs showing that i do not have in my code:

<a href="newUser.php?PHPSESSID=8f145070c5b91342dc675f464fa4fa91">

or:

<input type="hidden" name="PHPSESSID" value="8f145070c5b91342dc675f464fa4fa91" />

i don't even have this field in my code to beggin with...

Posted: Fri Jul 02, 2004 10:45 am
by feyd
those are added by the core when cookies (for sessions) are determined to be off, it can happen on the first page too, as cookies generally take a page refresh to be available for the document to use.

Posted: Fri Jul 02, 2004 10:53 am
by udwo
ok, thanx for claryfing that. do you have any additional sugestions as far as why my second page is not capturing the session (it works once i insert one record and then come to the same page and inser another one) then it will pass the variable.