Page 1 of 1

Trouble with Sessions

Posted: Sat Mar 14, 2009 4:03 pm
by braveryonions
I have written a script that tells the user if the input trinomial can be factored, for use on school work. Today, I tried to make it so that it remembers all the expressions entered in that session, and shows them at the end of the page. For some reason, this is not working. Here is the code:

The processing and input page:

Code: Select all

<?
 
// Start session
session_start();
 
// If user has clicked the button to clear session, do so
if($_GET['session'] == 'delete') {
session_destroy(); // Delete session
session_start(); // Start fresh session
}
 
// Set variables
 
if(isset($_POST['submit'])) {
 
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
 
if(is_numeric($a) && is_numeric($b) && is_numeric($c)) {
$a = round($a);
$b = round($b);
$c = round($c);
$a = intval($a);
$b = intval($b);
$c = intval($c);
}
 
}
 
else {
 
$a = 'a';
$b = 'b';
$c = 'c';
 
}
 
// Process
 
if(isset($_POST['submit'])) {
 
$equation = $a.'<i>x</i><sup>2</sup>+'.$b.'<i>x</i>+'.$c;
$save = true; // Denotes that this equation should be saved in session
 
if((!is_int($a)) || (!is_int($b)) || (!is_int($c))) {
 
$ans = 'One or more of your inputs were not numbers.<br /><br />I pity da fool.';
$save = false; // Denotes that his equation should NOT be saved in session
 
}
 
else {
 
$disc = pow($b, 2) - (4 * $a * $c);
$root = sqrt($disc);
 
if(floor($root) == $root) {
 
$ans = 'Hoorah! That expression can be factored!';
 
}
 
else {
 
$ans = 'Sorry! That expression cannot be factored!';
 
}
 
}
 
}
 
?>
<html>
<head>
<title>Can it be Factored?</title>
<link href="factor.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
<h1>Can it be Factored?</h1>
</div>
<?
 
if(isset($ans)) {
 
?>
<div id="ans">
<br />
<? echo $equation.'<br /><br />'.$ans; ?>
<br /><br />
</div>
<?
 
}
 
?><div id="form">
<br />Please fill in the equation with your <i>a</i>, <i>b</i>, and <i>c</i> values.<br />Any decimals will be rounded to the nearest integer.<br /><br />
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="text" size="3" maxlength="5" name="a" value="a" /><i>x</i><sup>2</sup>+<input type="text" size="3" maxlength="5" name="b" value="b" /><i>x</i>+<input type="text" size="3" maxlength="5" name="c" value="c" /><br />
<input type="submit" name="submit" value="Go!" />
</form>
</div>
<?
 
// Make object for old answers
 
class oldAns {
 
    var $oldquation;
    var $oldans;
    
    function display() {
    
    echo '<div class="oldans">'.$this->oldquation.'<br /><br />'.$this->oldans.'</div>';
    
    }
 
}
 
// Display old answers
if(isset($_SESSION['set'])) {
 
echo '<div id="oldcontainer">';
echo '<div class="close"><a href="'.$_SERVER['PHP_SELF'].'?session=delete"><font color="black">[x]</font></a></div>';
$continue = true;
$count = 0;
 
while($continue == true) {
 
if(isset($_SESSION[$count])) {
$_SESSION[$count]->display();
$count++;
}
 
else {
$continue = false;
}
 
}
echo '</div>';
 
}
 
 
// Save the current equation in the session
if($save == true) {
 
$_SESSION['set'] = true;
 
// Find out how many equations are saved 
$continue = true;
$count = 0;
while($continue == true) {
 
if(isset($_SESSION[$count])) {
$count++;
}
 
else {
$continue = false;
$count -= 1;
}
 
}
 
while($count >= 0) {
 
$tempnum = $count + 1;
 
$_SESSION[$tempnum] = $_SESSION[$count];
 
$count--;
 
}
 
$_SESSION[0] = new oldAns;
$_SESSION[0]->oldquation = $equation;
$_SESSION[0]->oldans = $ans;
 
}
 
?>
</body>
</html>
The stylesheet:

Code: Select all

body {
    text-align: center;
    background: gray;
}
a {
    color: white;
}
#header {
    color: white;
    background: black;
    width: 500px;
    margin-top: 5px;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 5px;
    padding: 5px;
}
#form {
    color: white;
    background: black;
    width: 500px;
    margin-top: 5px;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 5px;
    padding: 5px;
}
#ans {
    color: white;
    background: black;
    width: 500px;
    margin-top: 5px;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 5px;
    padding: 5px;
}
#oldcontainer {
    color: black;
    background: white;
    width: 500px;
    margin-top: 5px;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 5px;
    padding: 5px;
}
.oldans {
    color: white;
    background: black;
    width: 450px;
    margin-top: 5px;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 5px;
    padding: 5px;
}
.close {
    float: left;
    text-decoration: underline overline;
}

Re: Trouble with Sessions

Posted: Sat Mar 14, 2009 5:52 pm
by php_east
i am not sure about this part, i doubt very much if sessions can remember class function references, and even if it could, the refences would have changed each time the script is loaded, which would still render your ->display void.
so you may wish to double check on this.

Code: Select all

$_SESSION[$count]->display();