Okay first im going to give you some working code then im going to point a few things out. Someone else may also want to talk to you about your coding practices I imagine others could explain things better, but I have the code working, and it wasnt the if statement that was the problem.
here is the first page re-done
Code: Select all
<?php
session_start();
$getcode = rand();
$_SESSION['getcode'] = $getcode;
$gettingcode = $_SESSION['getcode'];
?>
<html>
<head></head>
<body>
<form action='codetestpage.php' method='post'>
Enter this code: <?php echo $getcode; ?>
<input name='inputcode' type='text' border='1' >
<input value='Send' type='submit' >
</form>
</body>
</html>
And here is the codetestpage.php redone it works I tested it.
Code: Select all
<?php
session_start();
$input = $_POST["inputcode"];
$getcode = $_SESSION["getcode"];
echo 'Your input was: '.$input.'<br/>';
echo 'The code was: '.$getcode . '<br/>';
if ($input == $getcode)
{
echo "Code matched.";
}
else
{
echo "Code didn't match.";
}
?>
The problem was your code in general lets look at the original.
Code: Select all
<?php session_start(); ?>
<?php
$getcode = rand() . "\n";
$_SESSION['getcode'] = $getcode;
$gettingcode = $_SESSION['getcode'];
echo "
<form method='post' action='codetestpage.php'>
Enter this code: ".$getcode." <input name='inputcode' type='text' border='1' />
<input value='Send' type='submit' />
</form>"
;
?>
first, there was no reason to use the ?> and then <?php after the start session. Also you should try to seperate your php, and html where possible, there was no need to echo all that html. Just echoing the variable in the one spot works fine. Im sure I have missed alot.
Code: Select all
<?php session_start(); ?>
<?php
$input = $_POST["inputcode"];
$getcode = $_SESSION["getcode"];
echo 'Your input was: '.$input.'<br/>';
echo 'The code was: '.$getcode;
?>
<hr/>
<?php
if ($input == $getcode)
{
echo "Code matched.";
}
else
{
echo "Code didn't match.";
}
?>
Again here there was no reason to keep opening, and closing with <?php tags. That was the only error I was getting.
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\test\codetestpage.php:3) in C:\xampp\htdocs\test\codetestpage.php on line 3
However the code still did everything you wanted it to do with that error, on my enviroment anyway.