Jump to content

A little help with the code for an idea.


lanceox

Recommended Posts

Hi guys, 

 

I have a little issue,  I have already got a quiz,  here is some of the code from the quiz. 

<?php 
session_start();
session_name("Question_Test");
require_once('questionsandanswers.php');
require_once('functions.php');

if (!isset($_SESSION['questions']))
    $_SESSION['questions'] = array();

$id = which_question($_POST['some_field']);

$score = 0;

if (!isset($_POST['submit']))
{
	$_SESSION['dclty'] = $_POST['dclty'];
	if ($_SESSION['dclty'] == "beg")
		{
	?>
		<script>
            window.open("extrahelp.php", height=300,width=300);
            </script>
	<?php
		}
	if ($_SESSION['dclty'] == "int")
		{
			?>
			<script>
			window.open("pointers.php", height=300,width=300);
			</script>
			<?php

		}
}

if (!isset($_POST['submit'])) 
{			
	$_SESSION['correct'] = array(); 
	$_SESSION['wrong'] = array();
	$_SESSION['finished'] = 'no';

	if (isset($_SESSION['error']))
	{
		unset($_SESSION['error']); 
		$_SESSION['$num'] = 0;
	} 
		else
	{
	$_SESSION['score'] = $score;
	$_SESSION['correct'] = array(); 
	$_SESSION['wrong'] = array(); 
	$_SESSION['finished'] = 'no';
	$_SESSION['$num'] = 0;
	} 
}
else
{
$_SESSION['$num'] = (int) $_POST['num'];
$postedanswers = str_replace("_"," ",$_POST['answers']);
	if ($postedanswers == $answers[$_SESSION['$num']]['0'])
	{
		$_SESSION['score'] ++;
		$_SESSION['correct'][] = $postedanswers; 
	} 
	else
	{
		$_SESSION['wrong'][] = $postedanswers;
	}	

if ($_SESSION['$num'] < count($questions)-1) 
{
	$_SESSION['$num']++;
} 
	else 
{
	$last = true;
	$_SESSION['finished'] = 'yes';
}
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Forensics E-learning Package</title>
        <?php 
echo $_SESSION['score'];
if (!isset($last)) {
echo "<script type=\"text/javascript\" src=\"form.js\"></script>";
}
?>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="wrapper">
<div id="header">
<div id="toplinks">

</div>
</div>
<div id="menu">
	<ul>
		<li><a class="selected" href="home.html">Home</a></li>
		<li><a href="initialquiz.php">Initial Quiz</a></li>
		<li><a href="about.php">About</a></li>

	</ul>
  </div>
<div id="content">
	<div id="main">
        <h1>Initial Quiz</h1><BR /><BR />
        <h1>Protection Section</h1><BR /><BR />
<?php if (!isset($last)){?>
<h2>Questions <?php echo $_SESSION['$num']+1; ?>:</h2>
<p><strong><?php echo $questions[$_SESSION['$num']]; ?></strong></p>
<form id="questionBox" method="post" action="initialquiz.php">

    <?php
$pattern = ' ';
$replace = '_';
$shuffledAnswers = shuffle_assoc($answers[$_SESSION['$num']]);
#var_dump($newanswers);
foreach ($shuffledAnswers as $answer) 
		{
			$answer2 = str_replace($pattern,$replace,$answer);
			echo "<li><input type=\"radio\" id=\"$answer2\" value=\"$answer2\" name=\"answers\" />\n";
			echo "<label for=\"$answer2\">$answer</label></li>\n";
		}	
?>

<input type="hidden" name="num" value="<?php echo $_SESSION['$num']; ?>" />
<input type="submit" id="submit" name="submit" value="Submit Answer" /></p>
</form><BR /><BR /><b>
<?php
} 
else
{ 
	 echo "<p id=\"compare\"><a href=\"detection.php\">Next Section! <img src=\"images/arrow.png\" /></a></p>";
}
?>
<?php

$_SESSION['$ProtectionPercent'] = number_format(($_SESSION['score'] * 100) / 5);


?>
</b>



          
        
        </div>
	<div id="right">
	<h2>Right Menu</h2>
	<div class="rightitem">
		<ul>
		<li><a class="selected" href="home.html">Home</a></li>
		<li><a href="initialquiz.php">Initial Quiz</a></li>
		<li><a href="about.php">About</a></li>
		</ul>
	</div>
  </div>
  </div>
<div class="clearbottom"></div>
<div id="footer">
	<p id="legal"> </p>
</div>
</div>
</div>
</body>
</html>

The problem is, is that everytime i refresh my page the score counter increments.

 

I have been chatting to a friend and they recommended some coding ideas that may work to stop this, however can any1 help me with a little more on implementing it to my systems?

 

here is the recommended code

session_start();

if (!isset($_SESSION['questions']))
    $_SESSION['questions'] = array();

# assuming which_question gives the proper id for the question
$id = which_question($_POST['some_field']);

# Only handle post data if questions hasn't allready been answered
if (!isset($_SESSION['questions'][$id]))
{
    # remove anything other than a-z, A-Z and regular whitespace from answer
    $answer = preg_replace('#[^a-zA-Z ]#', '', $_POST['answer']);
    $_SESSION['questions'][$id]['answer'] = $answer;
    $_SESSION['questions'][$id]['points'] = grade_answer($answer);
    
    # ...
} 

 

Any help would be hugely Appreciated.  Thanks

 

Labce

Link to comment
https://forums.phpfreaks.com/topic/232079-a-little-help-with-the-code-for-an-idea/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.