Recursion Advice

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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 - 01:57 PM

View PostDormilich, on 12 November 2012 - 01:04 PM, said:

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
}

Don't you mean:
// if username is NULL
if (!isset($username)
{
// no username entered
$smarty->assign("error", "no username entered.");

// display error template
$smarty->display("error.tpl");
}

// if password is NULL
if (!isset($password)
{
// no password entered
$smarty->assign("error", "no password entered.");

// display error template
$smarty->display("error.tpl");
}

// if password verify is NULL
if (!isset($passwordverify)
{
// no password verification entered
$smarty->assign("error", "no password verification entered.");

// display error template
$smarty->display("error.tpl");
}

// if email isn't valid
if (false === $email)
{
// invalid email given
$smarty->assign("error", "email entered is invalid.");

// display error template
$smarty->display("error.tpl");

}


Would this work? I prefer to let the user know which field wasn't filled out. Any assistance would be greatly appreciated.

Sincerely,

Aleeious
Was This Post Helpful? 0
  • +
  • -

#17 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Recursion Advice

Posted 12 November 2012 - 02:10 PM

no.

filter_input() returns NULL if the parameter is not set at all (i.e. a call from a different form or any other incomplete form submit or HTTP Request).

if the field is not filled in the form, but the form itself is successfully submitted, the parameters exist and hence an empty string is given (which should not cause a filter error, except in the email validation). these cases have to be checked separately.
Was This Post Helpful? 1
  • +
  • -

#18 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 - 02:55 PM

View PostDormilich, on 12 November 2012 - 02:10 PM, said:

no.

filter_input() returns NULL if the parameter is not set at all (i.e. a call from a different form or any other incomplete form submit or HTTP Request).

if the field is not filled in the form, but the form itself is successfully submitted, the parameters exist and hence an empty string is given (which should not cause a filter error, except in the email validation). these cases have to be checked separately.

Take um 6?
// if username is NULL
if (NULL == $username)
{
// no username entered
$smarty->assign("error", "no username entered.");

// display error template
$smarty->display("error.tpl");
}

// if password is NULL
if (NULL == $password)
{
// no password entered
$smarty->assign("error", "no password entered.");

// display error template
$smarty->display("error.tpl");
}

// if password verify is NULL
if (NULL == $passwordverify)
{
// no password verification entered
$smarty->assign("error", "no password verification entered.");

// display error template
$smarty->display("error.tpl");
}

// if email isn't valid
if (false === $email)
{
// invalid email given
$smarty->assign("error", "email entered is invalid.");

// display error template
$smarty->display("error.tpl");

}


Better or worse? Any assistance would be greatly appreciated.

Sincerely,

Aleeious

Take 7
// if username is empty
if (isEmpty($username))
{
// no username entered
$smarty->assign("error", "no username entered.");

// display error template
$smarty->display("error.tpl");
}

// if password is empty
if (isEmpty($password))
{
// no password entered
$smarty->assign("error", "no password entered.");

// display error template
$smarty->display("error.tpl");
}

// if password verify is empty
if (isEmpty($passwordverify))
{
// no password verification entered
$smarty->assign("error", "no password verification entered.");

// display error template
$smarty->display("error.tpl");
}

// if email isn't valid
if (false === $email)
{
// invalid email given
$smarty->assign("error", "email entered is invalid.");

// display error template
$smarty->display("error.tpl");

}


Sorry didn't realize I was using the wrong type of check(NULL instead of isEmpty() ).

Sincerely,

Aleeious
Was This Post Helpful? 0
  • +
  • -

#19 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Recursion Advice

Posted 12 November 2012 - 04:04 PM

definitely not better.

using == will use automatic type conversion (i.e. false would also match == NULL).

and as I have already said, if no password is entered, filter_input() will return "", not NULL. NULL is only returned if e.g. the field "password" does not exists within $_POST.

there is also no need for a custom isEmpty() function, use the built-in empty().

This post has been edited by Dormilich: 12 November 2012 - 04:05 PM

Was This Post Helpful? 1
  • +
  • -

#20 Aleeious  Icon User is offline

  • New D.I.C Head

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

Re: Recursion Advice

Posted 13 November 2012 - 12:08 PM

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

definitely not better.

using == will use automatic type conversion (i.e. false would also match == NULL).

and as I have already said, if no password is entered, filter_input() will return "", not NULL. NULL is only returned if e.g. the field "password" does not exists within $_POST.

there is also no need for a custom isEmpty() function, use the built-in empty().

Sorry, i confused the VB6 version with the PHP version. :whistling:
// if the form was submitted
if($username || $password || $passwordverify || $email)
{
// if username is empty
if (empty($username))
{
// no username entered
$smarty->assign("error", "no username entered.");

// display error template
$smarty->display("error.tpl");
}

// if password is empty
if (empty($password))
{
// no password entered
$smarty->assign("error", "no password entered.");

// display error template
$smarty->display("error.tpl");
}

// if password verify is empty
if (empty($passwordverify))
{
// no password verification entered
$smarty->assign("error", "no password verification entered.");

// display error template
$smarty->display("error.tpl");
}

// if email isn't valid
if (false === $email)
{
// invalid email given
$smarty->assign("error", "email entered is invalid.");

// display error template
$smarty->display("error.tpl");

}
}


How about now? Any assistance would be greatly appreciated.

Sincerely,

Aleeious

This post has been edited by Aleeious: 13 November 2012 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#21 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Recursion Advice

Posted 13 November 2012 - 01:22 PM

it’s getting better but there is still too much repetitive code. you can reduce it to write $smarty->assign() and $smarty->display() just once.

after that we learn how to reduce the nesting.

This post has been edited by Dormilich: 13 November 2012 - 01:22 PM
Reason for edit:: grammar

Was This Post Helpful? 1
  • +
  • -

#22 Aleeious  Icon User is offline

  • New D.I.C Head

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

Re: Recursion Advice

Posted 13 November 2012 - 01:57 PM

View PostDormilich, on 13 November 2012 - 01:22 PM, said:

itís getting better but there is still too much repetitive code. you can reduce it to write $smarty->assign() and $smarty->display() just once.

after that we learn how to reduce the nesting.

Problem is different errors will provide different error messages, which is why there is a $smarty->display("error" bla) for each type of error. Also display will be different depending on wither an error occurs or not.
error occurs: error.tpl
no error occurs: register.tpl


Any assistance would be greatly appreciated.

Sincerely,

Aleeious
Was This Post Helpful? 0
  • +
  • -

#23 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Recursion Advice

Posted 13 November 2012 - 02:22 PM

OK, you have spotted what distinguishes each $smarty->assign() call. It is the error message! It is not the call itself, it is only the message passed to it that differs.

Now, if you read through the first couple of posts, is there anywhere shown where to get a specific error message?
Was This Post Helpful? 1
  • +
  • -

#24 Aleeious  Icon User is offline

  • New D.I.C Head

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

Re: Recursion Advice

Posted 14 November 2012 - 08:07 PM

View PostDormilich, on 13 November 2012 - 02:22 PM, said:

OK, you have spotted what distinguishes each $smarty->assign() call. It is the error message! It is not the call itself, it is only the message passed to it that differs.

Now, if you read through the first couple of posts, is there anywhere shown where to get a specific error message?

Um the calls to empty for each field determines if an empty field is present and display an error. Here is the current version of my code:
<?php

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

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

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

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

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

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

	// if the form was submitted
	if($username || $password || $passwordverify || $email)
	{
		// if username is empty
		if (empty($username))
		{
			// no username entered
			$smarty->assign("error", "no username entered.");

			// display error template
			$smarty->display("error.tpl");

		// if password is empty
		}elseif(empty($password))
		{
			// no password entered
			$smarty->assign("error", "no password entered.");

			// display error template
			$smarty->display("error.tpl");
		
		// if password verify is empty
		} elseif(empty($verifypassword))
		{
			// no password verification entered
			$smarty->assign("error", "no password verification entered.");

			// display error template
			$smarty->display("error.tpl");
		
		// if email isn't valid
		} elseif (false === $email)
		{
			// invalid email given
			$smarty->assign("error", "email entered is invalid.");

			// display error template
			$smarty->display("error.tpl");

		}
	} else {
		
		// display it
		$smarty->display('register.tpl');
	}

?>


Any assistance would be greatly appreciated.

Sincerely,

Aleeious
Was This Post Helpful? 0
  • +
  • -

#25 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Recursion Advice

Posted 15 November 2012 - 12:49 AM

but that didnít answer the question

Quote

Now, if you read through the first couple of posts, is there anywhere shown where to get a specific error message?


you still do the repetitive coding.
Was This Post Helpful? 1
  • +
  • -

#26 Aleeious  Icon User is offline

  • New D.I.C Head

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

Re: Recursion Advice

Posted 15 November 2012 - 09:07 AM

Yes, you talked about exceptions briefly earlier. Would it be possible to check for an empty string exception? Any assistance would be greatly appreciated.

Sincerely,

Aleeious

This post has been edited by Dormilich: 15 November 2012 - 02:09 PM

Was This Post Helpful? 0
  • +
  • -

#27 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Recursion Advice

Posted 15 November 2012 - 02:13 PM

Quote

Would it be possible to check for an empty string exception?

sure, you can throw an exception if the string is empty. InvalidArgumentException and LengthException are a sensible choice.

This post has been edited by Dormilich: 15 November 2012 - 02:13 PM

Was This Post Helpful? 1
  • +
  • -

#28 Aleeious  Icon User is offline

  • New D.I.C Head

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

Re: Recursion Advice

Posted 15 November 2012 - 07:56 PM

Ok, I think i have it now: *Prays*
<?php

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

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

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

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

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

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

	// if the form was submitted
	if($username || $password || $passwordverify || $email)
	{
		try
		{

		// if username is empty
		if (empty($username))
		{
			throw new Exception("Username missing.");

		// if username is less then 4 characters or greater then 16 characters
		}elseif(strlen($username) < 4 || strlen($username) > 16)
		{
			throw new Exception("Username must be 4 to 16 characters.");
			
		// if password is empty
		}elseif(empty($password))
		{
			// no password entered
			throw new Exception("Password missing.");

		// if password is less then 6 characters
		}elseif(strlen($password) < 6)
		{
			throw new Exception("Password must be at least 6 characters.");

		// if password verify is empty
		} elseif(empty($verifypassword))
		{
			// no password verification entered
			throw new Exception("Password Verification missing.");

		// if password verification is less then 6 characters
		}elseif(strlen($verifypassword) < 6)
		{
			throw new Exception("Password Verification must be 6 characters.");

		// if password and password verification are different
		}elseif($password != $verifypassword)
		{
			throw new Exception("Both Passwords don't match.");

		// if email isn't valid
		} elseif (false === $email)
		{
			throw new Exception("Email missing.");

		}
		catch (Exception $e)
		{
			$smarty->assign("error", $e->getMessage());

			// desplay it
			$smarty->display("error.tpl");
		}
	} else {
		
		// display it
		$smarty->display('register.tpl');
	}

?>



Please let me know what you think.

Sincerely,

Aleeious
Was This Post Helpful? 0
  • +
  • -

#29 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Recursion Advice

Posted 16 November 2012 - 12:59 AM

well, how do you like it?

technically it doesnít matter whether you use elseif() or if(). once an exception is thrown, all subsequent code is skipped.
Was This Post Helpful? 0
  • +
  • -

#30 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Recursion Advice

Posted 16 November 2012 - 01:18 AM

regarding the very first if(), Iíd reverse that, too.
if (!isset($username, $password, $passwordverify, $email))
{
    $smarty->display('register.tpl');
    exit;
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2