PHP 5.3, Win 7, XAMP 1.7.3 local server, + a website with PHP 5.4 served.
3 forms: form.php, formcheck.php and thanks.html. Code is below.
I guess my methodology might be best to start witn in case it's all wrong. 😕
It's always seemed logical to get the security issues with access taken care of first, rather than on a 2nd or 3rd page. To that end, I'd like to put the initial security issues right up front onf the form.php just above the actual form input data.
On form.php:
First I create a random number using mt_rand and have the user type it into an input box, type=text, etc. which I assign to $d and follow it with the remaining inputs needed. No problem, all $_POST info echoes fine in formcheck.php.
Problem: Validating the random code.
In formcheck.php I have what the user entered as the code. BUT I cannot get at the 'code', OR the $d I assigned it to.
In other words, I have what the user eneterd, but I cannot get at the original random number to compare it to for validation. So I can't check if the 'code' the user entered and th eoriginal random number are the same!
Since I canot get at the original code from a POST, I decided to try using SESSION()s.
Here are the two pages' codes; minimized for my own clarity and TSing.
So I end up with 2 questions:
1. Is SESSION a reasonable way to get the info to other pages? Or is there an easier way?
All the sample code I've been able to find brings this question up:
2. In formcheck.php, WHERE can the $_SESSION[... be located in a script?
All the examples show it coming immediately after the session_start() but that feels like asking for something that hasn't existed yet. But a session, which any page could access, sounds like the answer but of course it won't work for me!🙁 I get an undefined index error in formcheck.php.
Here are the codes:
form.php
<?php
session_start();
$_session['key']='code';
?>
<!DOCTYPE html>
<head>
<title> Contact Form</title>
</head>
<body>
<html>
<?php
echo "<H1> Contact Form </H1>";
$a= mt_rand(nnn,nnn);
$b= mt_rand(nnn,nnn);
$c= mt_rand(nnnnn);
$d=($a.$b.$c);
echo "Your Temporary Code is : " . $d."<br />" ;
?>
<form action="formcheck.php" method="post">
<p> Enter your temporary code here : </b> <input type="text" name="code" size="14 " maxlength="12"> ,including dashes. <br />
<p> Enter Your Name : <input type="text" name="name" length="20" maxlength="25" value="Tom Rivet" <br /> </p>
<p> Your E-mail Address : <input type="text" name="email" size="30" maxlength="40" value="nobody@spamcop.net" <br />
<p> <input type="submit" value="Send "> </p>
</form>
</body>
</html>
and
formecheck.php:
<?php
session_start();
/* Set e-mail recipient */
$myemail = "nobody@kspamcop.net";
echo "<br />"."My E-mail: ".$myemail."<br />";
echo "======================="."<br />";
$code=($_POST['code']);
echo "Your temporary code was: ".$code."<br />";
echo "======================="."<br />";
// Verify the code is correct:
// use session? Can't get sessions to work
$name=($_POST['name']);
echo "Your Name is: ".$name."<br />";
if (count(explode(' ', $name)) >2) {
echo "You used too many names-use 1 or 2 names only."."<br />"."Go back and try again"."<br />";
}
echo (strlen($name)-1)." letters<br />";
echo "======================="."<br />";
echo "Server IP is: " . $_SERVER['REMOTE_ADDR'];
echo "<br />"."======================="."<br />";
?>
An earlier question just occurred to me:
3. Is there a way to get the random number $d into a $_POST to use?
TIA,
Rivet`