/    Sign up×
Community /Pin to ProfileBookmark

Hello I have an issue with error handlers in php, im using a login system and only 2 errors work out of 6.

this is the code:

[code]
<?php
if (isset($_POST[‘signup-submit’])) {

require ‘dbh.inc.php’;

$username = $_POST[‘uid’];
$email = $_POST[‘mail’];
$pwd = $_POST[‘pwd’];
$passwordRepeat = $_POST[‘pwdrepeat’];


if (empty($username) || empty($email) || empty($pwd) || empty($passwordRepeat)) {
header(“Location: ../signup.php?error=emptyfields&uid”.$username.”&mail=”.$email);
exit();
}
else if (!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match(“/^[a-zA-Z0-9]*$/”, $username)) {
header(“Location: ../signup.php?error=invalidmail&uid”);
exit();
}

else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header(“Location: ../signup.php?error=invalidmail”.$username);
exit();
}

else if (!preg_match(“/^[a-zA-Z0-9]*$/”, $username)) {
header(“Location: ../signup.php?error=invaliduid”.$email);
exit();
}
else if ($pwd !==$passwordRepeat) {
header(“Location: ../signup.php?error=passwordcheck”.$email.”username=”.$email);
exit();
}

else {
$sql = “SELECT uidUsers FROM users WHERE uidUsers=?”;
$stmt= mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header(“Location: ../signup.php?error=sqlerror”);
exit();
}
else {
mysqli_stmt_bind_param($stmt, “s”, $username);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$resultCheck = mysqli_stmt_num_rows($stmt);
if ($resultCheck > 0) {
header(“Location: ../signup.php?error=usertaken”);
exit();
}
else {
$sql = “INSERT INTO users (uidUsers, emailUsers, pwdUsers) VALUES(?, ?,?)”;
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)) {
header(“Location: ../signup.php?error=sqlerror”);
exit();
}

else {
$hashedPwd= password_hash($password, PASSWORD_DEFAULT);

mysqli_stmt_bind_param($stmt, “sss”, $username, $email, $hashedPwd);
mysqli_stmt_execute($stmt);
header(“Location: ../signup.php?error=success”);
exit();
}

}

}
}

mysqli_stmt_close($stmt);
mysqli_close($conn);

}
else {
header(“Location: ../signup.php”);
exit();
}

————————————————————————————————————————————

and this are the signup.php handlers, first and last work but the others dont

<h3>Signup</h3>
<?php
if (isset($_GET[‘error’])){
if ($_GET[‘error’] == “emptyfields”) {
echo ‘<p class=”signuperror”>Fill in all fields!</p>’;

}
else if ($_GET[‘error’] == “invalidmail”) {
echo ‘<p class=”signuperror”>Fill in all fields!</p>’;

}
else if ($_GET[‘error’] == “invaliduid”) {
echo ‘<p class=”signuperror”>Invalid Username!</p>’;
}
else if ($_GET[‘error’] == “passwordcheck”) {
echo ‘<p class=”signuperror”>Passwords dont match!</p>’;
}
else if ($_GET[‘error’] == “usertaken”) {
echo ‘<p class=”signuperror”>Username is already taken!</p>’;
}
else if ($_GET[‘error’] == “success”) {
echo ‘<p class=”s”>Signup successful!</p>’;
}
}

?>
[/code]

to post a comment
PHP

17 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmApr 06.2020 — Do you have a question? Awful lot of code that doesn't make a lot of sense. Seems like you could do this all on one script but you're not. How about just sending back the original page with the error messages displayed on IT instead of all the header calls with GET parms?

But - what is your question?
Copy linkTweet thisAlerts:
@NogDogApr 06.2020 — Added ... tags in the original post. Please be sure to use them in future posts, please.
Copy linkTweet thisAlerts:
@young_developerauthorApr 06.2020 — yes my question is why do only 2 error handlers show up and others don't

they show up like this:

https://gyazo.com/afd89a2562be2cef0104724860f60fe3
Copy linkTweet thisAlerts:
@ginerjmApr 06.2020 — Is that a php question?
Copy linkTweet thisAlerts:
@young_developerauthorApr 06.2020 — yes this is the code that handles the errors
<i>
</i>&lt;?php
if (isset($_GET['error'])){
if ($_GET['error'] == "emptyfields") {
echo '&lt;p class="signuperror"&gt;Fill in all fields!&lt;/p&gt;';

<i> </i> }
<i> </i> else if ($_GET['error'] == "invalidmail") {
<i> </i> echo '&lt;p class="signuperror"&gt;Fill in all fields!&lt;/p&gt;';
<i> </i>
<i> </i> }
<i> </i> else if ($_GET['error'] == "invaliduid") {
<i> </i> echo '&lt;p class="signuperror"&gt;Invalid Username!&lt;/p&gt;';
}
else if ($_GET['error'] == "passwordcheck") {
echo '&lt;p class="signuperror"&gt;Passwords dont match!&lt;/p&gt;';
}
else if ($_GET['error'] == "usertaken") {
echo '&lt;p class="signuperror"&gt;Username is already taken!&lt;/p&gt;';
}
else if ($_GET['error'] == "success") {
echo '&lt;p class="s"&gt;Signup successful!&lt;/p&gt;';
}
}

?&gt;
Copy linkTweet thisAlerts:
@ginerjmApr 06.2020 — And again - what is the question?
Copy linkTweet thisAlerts:
@young_developerauthorApr 06.2020 — only the
<i>
</i> if ($_GET['error'] == "emptyfields") {
echo '&lt;p class="signuperror"&gt;Fill in all fields!&lt;/p&gt;';


and

<i>
</i>else if ($_GET['error'] == "success") {
echo '&lt;p class="s"&gt;Signup successful!&lt;/p&gt;';



work, the rest dont
Copy linkTweet thisAlerts:
@ginerjmApr 06.2020 — Looking at this code - that can't be true. Only one of them would show up since you are only checking one specific field value and it can't be both things.

Wouldn't a switch work better for this testing?
Copy linkTweet thisAlerts:
@ginerjmApr 06.2020 — Try echoing out the GET value before the testing logic and see what you have.
Copy linkTweet thisAlerts:
@young_developerauthorApr 06.2020 — how do i do that, can u give me an example please?
Copy linkTweet thisAlerts:
@ginerjmApr 06.2020 — In front of that block you last posted with all the ifs - add an echo of the GET value. Then let the ifs run through and see which one gets used.


I really don't understand why you have this logic. You apparently already set an error since you are checking to see what it is here. So why? Why did you not just do the echo of the message when you set that error value??? Or why do set these values? Simply put the error message into the GET value instead of the one word code.
Copy linkTweet thisAlerts:
@NachfolgerApr 06.2020 — @young_developer#1617093

You're appending their email/username to the error string. This is why it isn't working.

Example:
``<i>
</i>else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../signup.php?error=invalidmail".$username);
exit();
}<i>
</i>
`</CODE>

Should be:
<CODE>
`<i>
</i>else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../signup.php?error=invalidmail");
exit();
}<i>
</i>
``
Copy linkTweet thisAlerts:
@NachfolgerApr 06.2020 — @ginerjm#1617094

I'm not trying to throw out unnecessary "shade" here, but you seem to be just posting for fun. There's no reason this thread should have even reached 13 replies before the error in the logic was found. You never add anything meaningful to the discussion, and only provide useless advice.

Again, this isn't some petty attack, I'm just leveraging my opinion here.
Copy linkTweet thisAlerts:
@ginerjmApr 06.2020 — I didnot even read his mile-long post as I said earlier. Most of the time I was asking him to ask a question if you read some of mine. And my last post to him would have answered the question just before you did if he had understood what I was telling him to do.
Copy linkTweet thisAlerts:
@NachfolgerApr 06.2020 — @ginerjm#1617097

> I didnot even read his mile-long post as I said earlier.

Perhaps we have just solved the issue. This is a coding forum, and you can't be bothered to even read the code when someone says there is an issue with it? It isn't that complicated to skim code, and it took me under a minute to resolve the issue. Again, no reason this thread should've reached 14 replies before resolving it.

I question your knowledge surrounding PHP, taking into account posts like this.
>Is that a php question?

The answer to which would have been quite apparent had you given OP a minute of your time to actually attempt to solve the issue.

> @ginerjm#1617097 And my last post to him would have answered the question just before you did if he had understood what I was telling him to do.

Yes this is partially true, and I certainly commend you for teaching people how to debug. I find myself amazed by the amount of developers unable to debug code. However, had you given OP the time he deserved, you would have found the issue with the code before he even needed to go through the process of debugging on his own.

I want to reiterate, I'm not trying to come at you with some highschool drama here; I just find it appauling how negligent you are in every single post I've seen from you.

I'm no perfect individual either, but people come to this forum for **our** help, and it's **our** job to help them. Why did it take 17 hours for this poor man to get a answer he can work with? I'm dumbfounded.
Copy linkTweet thisAlerts:
@ginerjmApr 06.2020 — Sorry I displease you so much.
Copy linkTweet thisAlerts:
@young_developerauthorApr 07.2020 — @Nachfolger#1617095

Thank you, your awesome, that saved the problem :)
×

Success!

Help @young_developer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...