Jump to content

Not showing errors when should


dean7

Recommended Posts

Hey guys, I've recently been getting back into coding after a long break of it, I'm currently finishing coding my Registration Script for my Website, although I've came across a few annoying things I cannot work out a way to sort.

 

Below I have a few things I want to throw out an error if done incorrectly. If the Username field is empty I want it to Display the $Message variable, same as with the Password, CPassword and so on. Some reason in which I don't know why its not actually giving me these errors, its not letting the user Register as it shouldn't but its also not telling them why. I have try it a few other ways with the empty($Password) or !$Password etc but nothing changed.

if ($Username == ""){
$Message = "Username field was missed.";
$Succ = "no";
}else{
$Succ = "yes";
}
 
if ($Password == ""){
$Message = "Password field was missed.";
$Succ = "no";
}else{
$Succ = "yes";
}
 
if ($CPassword == ""){
$Message = "Confirm Password field was missed.";
$Succ = "no";
}else{
$Succ = "yes";
}
 
if ($Email == ""){
$Message = "Email Field was missed.";
$Succ = "no";
}else{
$Succ = "yes";
}

On a different note, I do have

if ($Password != $CPassword){
$Message = "Your Passwords don't match.";
$Succ = "no";
}elseif ....

Which works perfectly fine and shows the error.

 

After I've done all that I check that the $Succ variable is not equal to no like this:

if ($Succ != "no") {

I am displaying the $Message variable like this:

echo $Message;

Which nothing is wrong with that?

 

Anyone have an idea what I can do to make it show these errors? 

 

Thanks for any help in advance :)

Link to comment
Share on other sites

 

I am displaying the $Message variable like this:

echo $Message;

That should output the value stored in $Message.

 

Where is that line in your code?

 

Also when coding make sure you have error reporting enabled. Such as make sure you have these settings are in your php.ini

display_errors = On
error_reporting = E_ALL
Link to comment
Share on other sites

because your code is only using one set of variables, the last validation test will win.

 

a better approach would be to use an array to hold the validation messages, one array entry per message. this will let you perform all your validation tests and output a message for each one that failed.

 

you would also use the array as the flag that indicates a success or failure. if the array is empty() there are no validation errors. if the array is not empty, loop over and output the error messages stored in it.

Link to comment
Share on other sites

 

That should output the value stored in $Message.

 

Where is that line in your code?

 

Also when coding make sure you have error reporting enabled. Such as make sure you have these settings are in your php.ini

display_errors = On
error_reporting = E_ALL

 

When I echo out message its at the end of all the PHP. 

 

because your code is only using one set of variables, the last validation test will win.

 

a better approach would be to use an array to hold the validation messages, one array entry per message. this will let you perform all your validation tests and output a message for each one that failed.

 

you would also use the array as the flag that indicates a success or failure. if the array is empty() there are no validation errors. if the array is not empty, loop over and output the error messages stored in it.

How would I simply change this in my code?.

Link to comment
Share on other sites

Can we assume the statement:

if ($Username == ""){

has $Username holding the sanitized value of $_POST['Username']? And etc.?

For example like me doing this?

 

$Email = filter_var($_POST['Email'], FLITER_SANITIZE_EMAIL);
$CEmail = filter_var($_POST['CEmail'], FILTER_SANITIZE_EMAIL); 
 
But no I haven't got anything like that for the $Username.
 
Same thing when I do my Emails like that they never seem to match :S  Little typo :)
Edited by dean7
Link to comment
Share on other sites

Yes, something like that.

 

Since you are echoing stuff to the browser, let's try:

var_dump($Username); if ($Username == ""){

 

Let's let PHP tell us what it thinks of $Username.

I done that and nothing showed

 

I have realised when I made an account which was successful it told did tell me the username 

Edited by dean7
Link to comment
Share on other sites

var_dump() gives you nothing...?

 

If the unlikely result of var_dump() gives nothing if $Username is not set, please prepare the variable by doing this:

$Username = $_POST['Username'] . "";

 

This will force $Username to equal an empty string if the POST variable is not set.

Link to comment
Share on other sites

var_dump() gives you nothing...?

 

If the unlikely result of var_dump() gives nothing if $Username is not set, please prepare the variable by doing this:

$Username = $_POST['Username'] . "";

 

This will force $Username to equal an empty string if the POST variable is not set.

 

I've got it as

 

if ($_POST['Username']){

 

At the start then I've got

 

$Username = safe($_POST['Username']);

 

Safe is just my function for abit of security, if I remove that nothing different happens.

 

So my guessing thats wrong? Should I do it like:

 

$Username = safe($_POST['Username'] . ""; 

 

Is that how you mean?

Link to comment
Share on other sites

have you even confirmed that your form is submitting the fields with the names and values that you think it is -

echo '<pre>',print_r($_POST,true),'</pre>';

beyond that, the problem is in your code, either with mistyped names or code that is setting the value to an empty string or completely unsettling it.

 

in post #2 in this thread Ch0cu3r suggested setting php's error_reporting/display_errors to get php to help you. did you do that?

Link to comment
Share on other sites

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.