-3

This is my first post here on the site so let me know if Im doing something wrong. Anyways onto my question.

I have been working on a login/Sign Up script for an application I am working on. When their Email is given to the script the Sign up process is suppose to be triggered. But I seem to be running into a issue that I don't understand.

    $Username = $_REQUEST['Username'];
    $EnteredPassword = $_REQUEST['Password'];
    $Email = $_REQUEST['Email'];

   if (!empty($Email))//Checks for email necessary for sign up
   {
    //Preform Sign Up Stuff
   }
   else 
   {
    //Preform Login Stuff
   }

For some reason the if statement evaluates as false, even though I POST the value of Email. Why does this happen? Is there a better way of checking if the value is provided?

scrowler
  • 24,273
  • 9
  • 60
  • 92
PeasB
  • 3
  • 4
  • How about `if ( isset($Email) )` . This will check if the value is set, basically to check if you have some value in it. Please show the form as well. – UserProg Apr 08 '15 at 01:52
  • 2
    Please show us your html form – Rizier123 Apr 08 '15 at 01:52
  • On top of what @Rizier123 said, you shouldn't be using `$_REQUEST`. You should be using what the form method was, i.e. `$_POST` or `$_GET` – Darren Apr 08 '15 at 01:56
  • *Is there a better way of checking if the value is provided?* Sure! `echo` them out! And you should also paste your form for us to investigate. – AkiEru Apr 08 '15 at 02:00
  • Didn't have a html form, b/c Its posting from an iOS application I'm working on. The `$_REQUEST` was just me testing things out. Im aware about the risks of using `$_GET` and my intend was to use $_POST all along. Anyways @Fred -ii- gave me a solution. Thanks for everyones help – PeasB Apr 08 '15 at 19:53

1 Answers1

2

Since you haven't provided your HTML form to go with your PHP, am submitting the following.

If your HTML form does not contain the following or similar, including form elements with name attributes for each and no typos, then there should not be any reason for failure.

Nota: name="Username" and name="username" are two different animals altogether.

<form action="handler.php" method="post">
Username:
<input type="text" name="Username"><br>
Password:
<input type="password" name="Password"><br>
Email:
<input type="text" name="Email"><br>
<input type="submit" name="submit" value="Submit">
</form>

Plus, as stated in comments, you're best using the form's actual method. Forms default to GET if omitted, therefore when using a POST method, it should be explicitly included in it.

Important sidenote:

Using GET to send information over the Internet, isn't safe; especially for passwords.


To debug, use error reporting.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.


References:


"Is there a better way of checking if the value is provided?"

  • Using a conditional empty() is fine, which is what you're doing right now.

  • You can tack on a conditional isset() also, but not required; see the link below about it.



$Username = $_POST['Username'];
$EnteredPassword = $_POST['Password'];
$Email = $_POST['Email'];

if (!empty($Email)) //Checks for email necessary for sign up
{
// Perform Sign Up Stuff
}
else 
{
// Perform Login Stuff
}

or:

if (!empty($_POST['Email']))
{
// Perform Sign Up Stuff
}
else 
{
// Perform Login Stuff
}
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • For some reason I didn't check the capitalization on the names. I feel stupid now. But thanks anyways I didn't know the `error_reporting()` function was a thing. Thanks – PeasB Apr 08 '15 at 19:44
  • @PeasB Ah, so that's what it was. I'm glad to know that I was able to provide a solution for you, *cheers* – Funk Forty Niner Apr 08 '15 at 19:45