Noskiw Posted August 5, 2014 Share Posted August 5, 2014 Currently, I'm trying to write a contact form, and perhaps I'm overcomplicating it, but I have a public variable called $EquationAnswer that stores the answer to the Captcha method (randomised maths question) and whenever the page reloads, it creates a new instance of the Contact class, which then erases the value that has been stored in that variable. What I'm trying to achieve is keeping the value stored even after a new instantiation of the class. I remember from a class at University that static was a way to do this, but this was in C#. <?php class Contact { public $EquationAnswer; public function CaptchaCreation() { $SignArray = array('+', '×', '-'); $NumberOne = rand(1, 9); $NumberTwo = rand(1, 4); if($NumberOne < $NumberTwo) $SignIndex = rand(0, 1); else $SignIndex = rand(0, 2); $Sign = $SignArray[$SignIndex]; $EquationString = $NumberOne." ".$Sign." ".$NumberTwo; switch($Sign) { case "+": $Answer = $NumberOne + $NumberTwo; break; case "-": $Answer = $NumberOne - $NumberTwo; break; case "×": $Answer = $NumberOne * $NumberTwo; break; } $this->EquationAnswer = $Answer; return $EquationString; } public function EchoContactForm() { $CaptchaMessage = $this->CaptchaCreation(); echo "<form method='POST' action=''>\n\n"; echo "\t\t\t\t\t<table>\n\n"; echo "\t\t\t\t\t\t<tr><td>Name: </td><td><input type='text' name='name' /></td></tr>\n"; echo "\t\t\t\t\t\t<tr><td>Email: </td><td><input type='text' name='email' /></td></tr>\n"; echo "\t\t\t\t\t\t<tr><td>Subject: </td><td><input type='text' name='subject' /></td></tr>\n"; echo "\t\t\t\t\t\t<tr><td>Message: </td><td><textarea rows=5 name='message'></textarea></td></tr>\n"; echo "\t\t\t\t\t\t<tr><td>What is: ".$CaptchaMessage."?</td><td><input type='text' name='captcha' /></td><td></td></tr>\n"; echo "\t\t\t\t\t\t<tr><td colspan=2 style='text-align: right'><input type='submit' name='submit' value='Send' /></td></tr>\n\n"; echo "\t\t\t\t\t</table>\n\n"; echo "\t\t\t\t</form>\n"; } public function ContactFormValidation($Name, $Email, $Subject, $Message, $CaptchaInput, $CaptchaAnswer) { echo $CaptchaAnswer; $ErrorsArray = array(); if(strlen($Name) == 0) array_push($ErrorsArray, "You must provide a name."); if(strlen($Email) == 0) array_push($ErrorsArray, "You must provide an email."); if(strlen($Message) == 0) array_push($ErrorsArray, "You must provide a message."); if(strlen($CaptchaInput) == 0) array_push($ErrorsArray, "You must provide an answer to the maths question."); if($CaptchaInput != $CaptchaAnswer) array_push($ErrorsArray, "Incorrect captcha answer."); if(count($ErrorsArray) > 0) { echo "There are ".count($ErrorsArray)." Errors: <br />"; foreach($ErrorsArray as $E) echo $E."<br />"; } else { return true; } } } ?> ^ That's the Contact class, and the main function that I want to focus on is the last one, "ContactFormValidation()". From the contact form page (which is within a template), I want it to send the EquationAnswer variable to that function so that it can compare what the user has input. <?php //Instantiate the contact class include './inc/PHP/classes/ContactClass.php'; $ContactClass = new Contact; if(!isset($_POST['submit'])) { $ContactClass->EchoContactForm(); } else { $NameInput = $_POST['name']; $EmailInput = $_POST['email']; $SubjectInput = $_POST['subject']; $MessageInput = $_POST['message']; $CaptchaInput = $_POST['captcha']; $CaptchaAnswer = $ContactClass->EquationAnswer; //Tried echoing out what is in the variable but it produces nothing because the contact class is a new instance. //echo $CaptchaAnswer; if($ContactClass->ContactFormValidation($NameInput, $EmailInput, $SubjectInput, $MessageInput, $CaptchaInput, $CaptchaAnswer) == true) { //Mail function } } ?> Any help would be appreciated. Cheers. Link to comment https://forums.phpfreaks.com/topic/290288-keeping-a-value-stored-in-a-variable-after-reloading-the-class/ Share on other sites More sharing options...
requinix Posted August 5, 2014 Share Posted August 5, 2014 C#'s static works in the application pool, which PHP does not have. PHP does have sessions, however, so you can put the value in there instead. Link to comment https://forums.phpfreaks.com/topic/290288-keeping-a-value-stored-in-a-variable-after-reloading-the-class/#findComment-1486916 Share on other sites More sharing options...
Noskiw Posted August 5, 2014 Author Share Posted August 5, 2014 C#'s static works in the application pool, which PHP does not have. PHP does have sessions, however, so you can put the value in there instead. I have considered using sessions, but was trying to avoid them for now. Is there perhaps any other way (not cookies)? Link to comment https://forums.phpfreaks.com/topic/290288-keeping-a-value-stored-in-a-variable-after-reloading-the-class/#findComment-1486919 Share on other sites More sharing options...
KevinM1 Posted August 5, 2014 Share Posted August 5, 2014 I have considered using sessions, but was trying to avoid them for now. Is there perhaps any other way (not cookies)? PHP is essentially a fire-and-forget environment. Once a particular script has executed, it's done. So, there's no shared memory or whatever for you to store values in. Because of that, you're left with the following options: Sessions Cookies Flat file Database Link to comment https://forums.phpfreaks.com/topic/290288-keeping-a-value-stored-in-a-variable-after-reloading-the-class/#findComment-1486921 Share on other sites More sharing options...
Noskiw Posted August 5, 2014 Author Share Posted August 5, 2014 Well I guess, since using a flat file is just ridiculous, and a database is too much processing for such a simple task, I'll have to use sessions. Thanks for your insights guys. Link to comment https://forums.phpfreaks.com/topic/290288-keeping-a-value-stored-in-a-variable-after-reloading-the-class/#findComment-1486922 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.