Recursion Advice

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 2640 Views - Last Post: 16 November 2012 - 01:18 AM Rate Topic: -----

#1 Aleeious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 26-October 12

Recursion Advice

Posted 11 November 2012 - 12:27 PM

I am writing a simple mmo script and am working on getting registration working. On my form i have 4 fields:

Quote

Username
Password
Retype Password
Email

I am currently using code as follows:
if(isset($_POST("username"))){

$username = trim(strip_tags($_POST("username")));

if(isset($_POST("password"))){

$password = trim(strip_tags($_POST("password")));

if(isset($_POST("passwordverify"))){

$passwordverify = trim(strip_tags($_POST("passwordverify")));

if(isset($_POST("email"))){

$email = trim(strip_tags($_POST("email")));

if($_POST("password" == $_POST("verifypassword"))) {

// if everthing is correct continue processing

} else {

echo "No username entered";

} else {
echo "no password entered";

} else {

echo "You forgot to retype your password";

} else {

echo "no email entered";

} else {

echo "both passwords entered don't match";

}
}
}
}
}


Is there a better way of doing this short of writing a bunch of if statements? Any assistance would be greatly appreciated.

Sincerely,

Aleeious

Is This A Good Question/Topic? 0
  • +

Replies To: Recursion Advice

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: Recursion Advice

Posted 11 November 2012 - 01:22 PM

Exception can help it make shorter.
// I’ll show it for 3 parameters. there should be something
// left for you to excercise
try 
{
    $username = filter_input(INPUT_POST, "username", FILTER_SANITIZE_STRING);
    $password = filter_input(INPUT_POST, "password", FILTER_SANITIZE_STRING);
    $email    = filter_input(INPUT_POST, "email",    FILTER_VALIDATE_EMAIL);
    // well, failing to sanitise a string would be hard, so this is triggered
    // if the parameters were not sent. otherwise you could differentiate between
    // false (failed validation) and null (parameter missing) (see next)
    if (!$username OR !$password) 
    {
        throw new Exception("Username or Password missing.");
    }

    if (NULL === $email) 
    {
        throw new Exception("Email missing.");
    }
    elseif (false === $email) 
    {
        throw new Exception("Email invalid.");
    }

    // other condition in the same manner

    // do processing
}
catch (Exception $e)
{
    echo $e->getMessage();
}

This post has been edited by Dormilich: 11 November 2012 - 01:25 PM

Was This Post Helpful? 1
  • +
  • -

#3 Aleeious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 26-October 12

Re: Recursion Advice

Posted 11 November 2012 - 01:38 PM

Here's a bit more of my code, would the above work with Smarty?
<?php

if(isset($_POST("username"))){
	
	$username = trim(strip_tags($_POST("username")));
	
	if(isset($_POST("password"))){
		
		$password = trim(strip_tags($_POST("password")));
		
		if(isset($_POST("passwordverify"))){
			
			$passwordverify = trim(strip_tags($_POST("passwordverify")));
			
			if(isset($_POST("email"))){
				
				$email = trim(strip_tags($_POST("email")));
				
				if($_POST("password" == $_POST("verifypassword"))) {
					
					// if everthing is correct continue processing
					
					} else {
					
					$smarty->assign("error", "no username entered");
					
					$smarty->display("error.tpl");
					
					} else {
					
					$smarty->assign("error", "no password entered");
					
					$smarty->display("error.tpl");
					
					} else {
					
					$smarty->assign("error", "forgot to retype password");
					
					$smarty->display("error.tpl");
					
					} else {
					
					$smarty->assign("error", "no email entered");
					
					$smarty->display("error.tpl");
					
					} else {
					
					$smarty->assign("error", "the passwords don't match");
					
					$smarty->display("error.tpl");
					
				}
			}
		}
	}
}

?>


Any assistance would be greatly appreciated.

Sincerely,

Aleeious
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: Recursion Advice

Posted 11 November 2012 - 01:39 PM

View PostAleeious, on 11 November 2012 - 09:38 PM, said:

Here's a bit more of my code, would the above work with Smarty?

I don’t see a reason why it shouldn’t work.
Was This Post Helpful? 1
  • +
  • -

#5 Aleeious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 26-October 12

Re: Recursion Advice

Posted 11 November 2012 - 01:48 PM

View PostDormilich, on 11 November 2012 - 01:39 PM, said:

View PostAleeious, on 11 November 2012 - 09:38 PM, said:

Here's a bit more of my code, would the above work with Smarty?

I don’t see a reason why it shouldn’t work.

Ok, how would i combine the exception with the Smarty templates? Can i put an exception inside a smarty template? Any assistance would be greatly appreciated.

Sincerely,

Aleeious
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: Recursion Advice

Posted 11 November 2012 - 01:51 PM

Quote

Can i put an exception inside a smarty template?

you would have to ask the smarty people that. but in our case it is not needed.


Quote

how would i combine the exception with the Smarty templates?

not at all. the smarty templates are (as the name suggests) for output. the exceptions fall into the controller category.


Quote

Any assistance would be greatly appreciated.

lets start with the easy one: what information do you feed to the smarty template?
Was This Post Helpful? 1
  • +
  • -

#7 Aleeious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 26-October 12

Re: Recursion Advice

Posted 11 November 2012 - 03:09 PM

View PostDormilich, on 11 November 2012 - 01:51 PM, said:

Quote

Can i put an exception inside a smarty template?

you would have to ask the smarty people that. but in our case it is not needed.


Quote

how would i combine the exception with the Smarty templates?

not at all. the smarty templates are (as the name suggests) for output. the exceptions fall into the controller category.


Quote

Any assistance would be greatly appreciated.

lets start with the easy one: what information do you feed to the smarty template?

Here we go:

Form Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Application Form for Enployment in Aleeious Dealership!</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="templates/css/style.css" />
</head>
<body>
<h1>Congratulations!</h1>
<p>You have taken the first step in becoming one of the elite. Just fill out the application form below and we'll get you setup with your own repair bay.</p>
<form action="register.php" method="post">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" title="Your mechanic's name" accesskey="1" />
<label for="password">Password:</label><input type="password" name="password" id="password" title="A Secret Password, don't tell anyone not even your best friend!" accesskey="2" />
<label for="retypepassword">Retype Password:</label><input type="password" id="retypepassword" name="retypepassword" title="Retype your secret password to make sure you didn't make any mistakes" accesskey="3" />
<label for="email">Email:</label><input type="text" name="email" id="email" title="Email address in case you forget your password" accesskey="4" />
<input type="submit" value="enlist" title="Click here after double-checking the info above to signup" accesskey="5" />
</fieldset>
</form>
</body>
</html>



PHP Code:
<?php

	// include smarty library
	include('libs/Smarty.class.php');

	// create new instance of smarty object
	$smarty = new Smarty;

	// if the form was submitted
	if(isset($_POST['submit']))
	{
		// filter username
		$username = filter_input(INPUT_POST, "username", FILTER_SANITIZE_STRING);

		// filter password
		$password = filter_input(INPUT_POST, "password", FILTER_SANITIZE_STRING);

		// filter password verification
		$passwordverify = filter_input(INPUT_POST, "passwordverify", FILTER_SANITIZE_STRING);

		// filter email
		$email = filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL);

		// if the username was entered
		if(!isset($username))
		{
			// set error message
			$smarty->assign("error", "You did not enter a username");

			// display the form
			$smarty->display('register.tpl');
		}

	} else {

	// display the form
	$smarty->display('register.tpl');
}

?>


However when i submit the form with no username, it just repeatedly shows the form instead of displaying the error template. Any assistance would be greatly appreciated.

Sincerely,

Aleeious
Was This Post Helpful? 0
  • +
  • -

#8 Aleeious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 26-October 12

Re: Recursion Advice

Posted 11 November 2012 - 04:03 PM

Sorry for some reason I can't seem to edit my posts. I've started debugging my code and managed to comment all the lines except those reverent to my problem and managed to come up with this:
<?php

	// include smarty library
	include('libs/Smarty.class.php');

	// create new instance of smarty object
	$smarty = new Smarty;

	// if the form was submitted
	if(isset($_POST['submit']))
	{
		// filter username
		$username = filter_input(INPUT_POST, "username", FILTER_SANITIZE_STRING);

		// filter password
		//$password = filter_input(INPUT_POST, "password", FILTER_SANITIZE_STRING);

		// filter password verification
		//$passwordverify = filter_input(INPUT_POST, "passwordverify", FILTER_SANITIZE_STRING);

		// filter email
		//$email = filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL);

		// if the username was entered
		//if(!isset($username))
		//{
			// set error message
			$smarty->assign("error", "Registrations are currently disabled!");

			// display the form
			$smarty->display('error.tpl');
		//}

	} else {

	// display the form
	$smarty->display('register.tpl');
}

?>


However the problem still persists. Any assistance would be greatly appreciated.

Sincerely,

Aleeious
Was This Post Helpful? 0
  • +
  • -

#9 Aleeious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 26-October 12

Re: Recursion Advice

Posted 11 November 2012 - 05:09 PM

Still can't seem to edit my own post so here goes attempt 3:
<?php

	// include smarty library
	include('libs/Smarty.class.php');

	// create new instance of smarty object
	$smarty = new Smarty;

	// if the form was submitted
	if(isset($_POST["submit"]))
	{
		// set error message
		$smarty->assign("error", "Registrations are currently disabled!");

		// display the form
		$smarty->display('error.tpl');
	} else {

	// display the form
	$smarty->display('register.tpl');
}

?>


Still no luck. Any assistance would be greatly appreciated.

Sincerely,

Aleeious
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: Recursion Advice

Posted 11 November 2012 - 11:36 PM

View PostAleeious, on 12 November 2012 - 01:09 AM, said:

Still can't seem to edit my own post

Post editing requires a certain post count. this is to prevent students from posting their homework and then deleting it after they got the answer.

post #8: the isset() is not necessary. filter_input() checks itself, whether the form was submitted. and besides that it is always a bad idea to assume that a form element named "submit" guarantees that the form was posted. (some earlier version of IE did not send the submit button along, so that code would fail in that case).
Was This Post Helpful? 1
  • +
  • -

#11 Aleeious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 26-October 12

Re: Recursion Advice

Posted 12 November 2012 - 12:05 PM

That stinks, that i can't edit my posts because of some bad apples. :saddam: Hope I get edit privileges soon, so i no longer have to spam threads with updates. :tup: Anyways if filter input checks itself, how would i know if the form was submitted? Would this work:
<?php

	// include smarty library
	include('libs/Smarty.class.php');

	// create new instance of smarty object
	$smarty = new Smarty;

        // sanitize input
        $username = filter_input(INPUT_POST, "username", FILTER_SANITIZE_STRING);

	// if the form was submitted(checking for not empty?)
	if($username || $password || $passwordverify || email)
	{
		// set error message
		$smarty->assign("error", "Registrations are currently disabled!");

		// display the form
		$smarty->display('error.tpl');
	} else {

	// display the form
	$smarty->display('register.tpl');
}

?>


Any assistance would be greatly appreciated.

Sincerely,

Aleeious

Sorry Code Fix:
<?php

	// include smarty library
	include('libs/Smarty.class.php');

    // create new instance of smarty object
    $smarty = new Smarty;

    // sanitize input
    $username = filter_input(INPUT_POST, "username", FILTER_SANITIZE_STRING);

	// if the form was submitted(checking for not empty?)
	if($username || $password || $passwordverify || $email)
	{
		// set error message
		$smarty->assign("error", "Registrations are currently disabled!");

		// display the form
		$smarty->display('error.tpl');
	} else {

	// display the form
	$smarty->display('register.tpl');
}

?>




Sincerely,

Aleeious
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: Recursion Advice

Posted 12 November 2012 - 12:06 PM

Quote

Anyways if filter input checks itself, how would i know if the form was submitted?

if filter_input() returns NULL, the form was not submitted.


your code: the if() will always return true since email is interpreted as string (which is a truthy value). assuming this is just a typo, the if() will return true on the first truthy value.
Was This Post Helpful? 1
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6037
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: Recursion Advice

Posted 12 November 2012 - 12:08 PM

Quote

Hope I get edit privileges soon, so i no longer have to spam threads with updates.


NO! We would greatly prefer you NOT go back and edit your previous posts when you've made changes in the code. That only CONFUSES the issue, and renders posts regarding the previous code pointless.

When you make updates to your code, create a new post with the new code.
Was This Post Helpful? 1
  • +
  • -

#14 Aleeious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 26-October 12

Re: Recursion Advice

Posted 12 November 2012 - 12:13 PM

View PostDormilich, on 12 November 2012 - 12:06 PM, said:

Quote

Anyways if filter input checks itself, how would i know if the form was submitted?

if filter_input() returns NULL, the form was not submitted.


your code: the if() will always return true since email is interpreted as string (which is a truthy value). assuming this is just a typo, the if() will return true on the first truthy value.

So which would be better?
// sanitize input
if($username = filter_input(INPUT_POST, "username", FILTER_SANITIZE_STRING));


-or-
// if the form was submitted(checking for not empty?)
if($username || $password || $passwordverify || $email)



View PostJackOfAllTrades, on 12 November 2012 - 12:08 PM, said:

Quote

Hope I get edit privileges soon, so i no longer have to spam threads with updates.


NO! We would greatly prefer you NOT go back and edit your previous posts when you've made changes in the code. That only CONFUSES the issue, and renders posts regarding the previous code pointless.

When you make updates to your code, create a new post with the new code.

Ok, will do!

Sincerely,

Aleeious
Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: Recursion Advice

Posted 12 November 2012 - 01:04 PM

Quote

So which would be better?

neither.
// if neither is NULL, all values were passed
if (!isset($username, $password, $passwordverify, $email))
{
    // required value missing
}
// if email is valid
if (false === $email)
{
    // invalid email given
}

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2