Solved

why do the session variables not get unset/destroyed? 0o

Posted on 2011-09-14
10
300 Views
Last Modified: 2012-05-12
hi there,

the page queries can be summarized as follows:

Query      Description
50      Retail Including VAT via s=SupplierID, p=PartNumber
51      Retail Including VAT + Discount via s=SupplierID, p=PartNumber & n=PercentageOff
52      Retail Including VAT Session Total
53      Retail Including VAT + Discount Session Total
54      Session Comments
55      Empty Session Variables then Clear Session

and this is how they are supposed to work in the following section of qtpq.php:

// unused functions, variable preperation & earlier working queries without sessions
$s = $_GET["s"];
if (!is_numeric($s)) {
	$s = 141;	
}
$q = $_GET["q"];
if (is_numeric($q)) {
	$q = intval($q);
	if ($q < 0) {
		$q = 0;
	} elseif ($q > 8 && $q < 50) {
		$q = 50;
	} elseif ($q > 55 && $q < 100) {
		$q = 100;
	} elseif ($q > 107) {
		$q = 100;
	}
} else {
	$q = 0;
}
$n = $_GET["n"];
if (!is_numeric($n)) {
	$n = 0;
}
$p = $_GET["p"];
// queries < 100 (50-55 as specified in reference)
} elseif ($q < 100) {
	session_start();
	if (!isset($_SESSION['comment'])) $_SESSION['comment'] = array();
	$tsql = "SELECT 
		CASE ".$q." 
		WHEN 50 THEN 									# Retail Including VAT
			(28_pricelists.decimal_RetailExcluding * 1.14) 
		WHEN 51 THEN 									# Retail Including VAT + Discount
			(28_pricelists.decimal_RetailExcluding * 1.14) * (1 - ".$n." / 100) 
		WHEN 52 THEN 									# Retail Including VAT Session Total
			\"".floatval($_SESSION['rtlvat'])."\" 
		WHEN 53 THEN 									# Retail Including VAT + Discount Session Total
			\"".floatval($_SESSION['rtlvatdisc'])."\" 
		WHEN 54 THEN 									# Return Comments
			\"".implode("<br/>\n",$_SESSION['comment'])."<br/>\n |||||||||| Retail Total R ".number_format(floatval($_SESSION['rtlvat']),2)."<br/>\n |||||||||| Discounted Total R ".number_format(floatval($_SESSION['rtlvatdisc']),2)."\" 
		WHEN 55 THEN 									# Empty Session Variables then Clear Session
			\"\" 
		END AS text_Result, 
			28_pricelists.text_PartNumber, 
			28_pricelists.text_Description 
	FROM 
		28_pricelists".(($q>51)?";":" 
	WHERE 
		28_pricelists.text_PartNumber = \"".$p."\" 
	LIMIT 1;");
	//echo $tsql;
	$result = mysql_query_errors($tsql , $conn , __FILE__ , __LINE__ , true );
	if ($result) {
		if ($row = mysql_fetch_array($result)) {
			if ($q == 50) {										// create / add session total incl. vat
				array_push($_SESSION['comment'], "||||| ".$row["text_PartNumber"]." || ".$row["text_Description"]." || R ".number_format(floatval($row["text_Result"]),2))." || ";
				if (!isset($_SESSION['rtlvat'])) {
					$_SESSION['rtlvat'] = floatval($row["text_Result"]);
				} else {
					$_SESSION['rtlvat'] = floatval($_SESSION['rtlvat']) + floatval($row["text_Result"]);
				}
			} elseif ($q == 51) {									// create / add session total incl. vat + discount, Empty Session Variables & Clear Session
				$_SESSION['comment'][count($_SESSION['comment'])-1] = $_SESSION['comment'][count($_SESSION['comment'])-1]." || ".$n."% || R ".number_format(floatval($row["text_Result"]),2)." ";
				if (!isset($_SESSION['rtlvatdisc'])) {
					$_SESSION['rtlvatdisc'] = floatval($row["text_Result"]);
				} else {
					$_SESSION['rtlvatdisc'] = floatval($_SESSION['rtlvatdisc']) + floatval($row["text_Result"]);
				}
			} elseif ($q - 52) {
				// do nothing. returns session total incl vat from mysql
			} elseif ($q = 53) {
				// do nothing, returns session total incl vat + disc from mysql
			} elseif ($q = 54) {
				// do nothing, returns session comments from mysql
			} elseif ($q = 55) {
				// Empty Retail Incl Vat Total
				unset($_SESSION['rtlvat']);
				// Empty Retail Incl Vat + Discount Total
				unset($_SESSION['rtlvatdisc']);
				// Empty Comments
				unset($_SESSION['comment']);
				// Finally, destroy the session.
				session_destroy();
			}
			echo (is_numeric($row["text_Result"]))?number_format(floatval($row["text_Result"]),2):$row["text_Result"];
		}
		mysql_free_result($result);
	}
// following query possibilities without sessions < 150

Open in new window


the url's where these can be seen in the order they should be used and how they act are:

Clear/Destroy Session Before Starting: http://www.intellisource.co.za/qtpq.php?q=55
Get Retail + Discount Total: http://www.intellisource.co.za/qtpq.php?q=53
Clear/Destroy Session When Done: http://www.intellisource.co.za/qtpq.php?q=55
now all seems as its supposed to, trying this procession once does seem to work. but try it for a second round and you will notice the session variables are NOT being cleared/destroyed. why is this and how can i resolve this issue?

many thanks in advance
0
Comment
Question by:intellisource
  • 7
  • 2
10 Comments
 
LVL 4

Expert Comment

by:h4hardy
ID: 36535216
you have to mentation the session_start() on the top of the pages.. else the session varibale not take place it's affect... also you can try like,

$_SESSION['YOUR_VARIBALE'] = '';

hope it will work.
0
 

Author Comment

by:intellisource
ID: 36535276
it is above any content being written - except the header tags of the page which are before the if $q's are processed.
0
 

Author Comment

by:intellisource
ID: 36535287
oh and i did attempt clearing with $_SESSION['VARIABLE'] = ''; before the session_destroy(); but to no avail.
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:intellisource
ID: 36535350
oki moved session_start() to the top of the page but to no avail -_-
0
 
LVL 4

Expert Comment

by:h4hardy
ID: 36535353
you can try with the below code.. it's working fine..

<?php
session_start();
// unused functions, variable preperation & earlier working queries without sessions
$s = $_GET["s"];
if (!is_numeric($s)) {
        $s = 141;       
}
$q = $_GET["q"];
if (is_numeric($q)) {
        $q = intval($q);
        if ($q < 0) {
                $q = 0;
        } elseif ($q > 8 && $q < 50) {
                $q = 50;
        } elseif ($q > 55 && $q < 100) {
                $q = 100;
        } elseif ($q > 107) {
                $q = 100;
        }
} else {
        $q = 0;
}
$n = $_GET["n"];
if (!is_numeric($n)) {
        $n = 0;
}
$p = $_GET["p"];
// queries < 100 (50-55 as specified in reference)
} elseif ($q < 100) {
        
        if (!isset($_SESSION['comment'])) $_SESSION['comment'] = array();
        $tsql = "SELECT 
                CASE ".$q." 
                WHEN 50 THEN                                                                    # Retail Including VAT
                        (28_pricelists.decimal_RetailExcluding * 1.14) 
                WHEN 51 THEN                                                                    # Retail Including VAT + Discount
                        (28_pricelists.decimal_RetailExcluding * 1.14) * (1 - ".$n." / 100) 
                WHEN 52 THEN                                                                    # Retail Including VAT Session Total
                        \"".floatval($_SESSION['rtlvat'])."\" 
                WHEN 53 THEN                                                                    # Retail Including VAT + Discount Session Total
                        \"".floatval($_SESSION['rtlvatdisc'])."\" 
                WHEN 54 THEN                                                                    # Return Comments
                        \"".implode("<br/>\n",$_SESSION['comment'])."<br/>\n |||||||||| Retail Total R ".number_format(floatval($_SESSION['rtlvat']),2)."<br/>\n |||||||||| Discounted Total R ".number_format(floatval($_SESSION['rtlvatdisc']),2)."\" 
                WHEN 55 THEN                                                                    # Empty Session Variables then Clear Session
                        \"\" 
                END AS text_Result, 
                        28_pricelists.text_PartNumber, 
                        28_pricelists.text_Description 
        FROM 
                28_pricelists".(($q>51)?";":" 
        WHERE 
                28_pricelists.text_PartNumber = \"".$p."\" 
        LIMIT 1;");
        //echo $tsql;
        $result = mysql_query_errors($tsql , $conn , __FILE__ , __LINE__ , true );
        if ($result) {
                if ($row = mysql_fetch_array($result)) {
                        if ($q == 50) {                                                                         // create / add session total incl. vat
                                array_push($_SESSION['comment'], "||||| ".$row["text_PartNumber"]." || ".$row["text_Description"]." || R ".number_format(floatval($row["text_Result"]),2))." || ";
                                if (!isset($_SESSION['rtlvat'])) {
                                        $_SESSION['rtlvat'] = floatval($row["text_Result"]);
                                } else {
                                        $_SESSION['rtlvat'] = floatval($_SESSION['rtlvat']) + floatval($row["text_Result"]);
                                }
                        } elseif ($q == 51) {                                                                   // create / add session total incl. vat + discount, Empty Session Variables & Clear Session
                                $_SESSION['comment'][count($_SESSION['comment'])-1] = $_SESSION['comment'][count($_SESSION['comment'])-1]." || ".$n."% || R ".number_format(floatval($row["text_Result"]),2)." ";
                                if (!isset($_SESSION['rtlvatdisc'])) {
                                        $_SESSION['rtlvatdisc'] = floatval($row["text_Result"]);
                                } else {
                                        $_SESSION['rtlvatdisc'] = floatval($_SESSION['rtlvatdisc']) + floatval($row["text_Result"]);
                                }
                        } elseif ($q - 52) {
                                // do nothing. returns session total incl vat from mysql
                        } elseif ($q = 53) {
                                // do nothing, returns session total incl vat + disc from mysql
                        } elseif ($q = 54) {
                                // do nothing, returns session comments from mysql
                        } elseif ($q = 55) {
                                // Empty Retail Incl Vat Total
								$_SESSION['rtlvat'] = '';
								$_SESSION['rtlvatdisc'] = '';
								$_SESSION['comment'] = '';
                                unset($_SESSION['rtlvat']);

                                // Empty Retail Incl Vat + Discount Total
                                unset($_SESSION['rtlvatdisc']);
                                // Empty Comments
                                unset($_SESSION['comment']);
                                // Finally, destroy the session.
                                session_destroy();
                        }
                        echo (is_numeric($row["text_Result"]))?number_format(floatval($row["text_Result"]),2):$row["text_Result"];
                }
                mysql_free_result($result);
        }
// following query possibilities without sessions < 150

?>

Open in new window

0
 

Author Comment

by:intellisource
ID: 36535521
i wasnt going to paste the whole page's code here, just the section that uses these session variables, i forgot to add the header info which is as follows:
header('Cache-Control: no-cache');
header('Pragma: no-cache');
header("Content-type: text/plain; charset=utf-8");

Open in new window

the session_start() has been moved right to the top of the page, below the page identifying php comment and above the functions and variables as follows:
<?php
/*
Quicktext Pro AJAX XML RESPONSE
Version 2.2.3
*/
session_start();

Open in new window

yet it's still not working when i open http://www.intellisource.co.za/qtpq.php?q=55 -  yet this STILL does not clear or destroy the three session values despite adding the following above the unset's.
} elseif ($q = 55) {
	// set session vars to ''
	$_SESSION['rtlvat'] = '';
	$_SESSION['rtlvatdisc'] = '';
	$_SESSION['comment'] = '';
	// Empty Retail Incl Vat Total
	unset($_SESSION['rtlvat']);
	// Empty Retail Incl Vat + Discount Total
	unset($_SESSION['rtlvatdisc']);
	// Empty Comments
	unset($_SESSION['comment']);
	// Finally, destroy the session.
	session_destroy();
}

Open in new window

i am at a total loss here :(
0
 

Author Comment

by:intellisource
ID: 36535560
here is the session info on the server - perhaps anybody can relate anything meaningful in my experience from this:
session.jpg
0
 

Accepted Solution

by:
intellisource earned 0 total points
ID: 36535723
OMW!!! decided to inspect the flow of code, and i found the issue! the if statements NEVER went into 55 after the sql query! from 52 till 55 had typos: -, = instead of ==:
                        } elseif ($q - 52) {
                                // do nothing. returns session total incl vat from mysql
                        } elseif ($q = 53) {
                                // do nothing, returns session total incl vat + disc from mysql
                        } elseif ($q = 54) {
                                // do nothing, returns session comments from mysql
                        } elseif ($q = 55) {
                                // Empty Retail Incl Vat Total
                                unset($_SESSION['rtlvat']);
                                // Empty Retail Incl Vat + Discount Total
                                unset($_SESSION['rtlvatdisc']);
                                // Empty Comments
                                unset($_SESSION['comment']);
                                // Finally, destroy the session.
                                session_destroy();
                        }

Open in new window

0
 

Author Closing Comment

by:intellisource
ID: 36558733
thanks for the efforts guys, seems not all of our focus'/clarities are where they need to be hehe :)
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36536572
The if/else constructs are confusing when you get more than one or two choices.  Learn about switch/case structures.  Your programming will be MUCH easier to understand, I promise!
http://us2.php.net/manual/en/control-structures.switch.php
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question