Jump to content

Keeping a value stored in a variable after reloading the class


Go to solution Solved by KevinM1,

Recommended Posts

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.

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)?

  • Solution

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.