I'm writing a code for Login Password Recovery and therefore, I needed a $_GET value called 'recoverykey'. Whenever I visit the WebPage without defining recoverykey, it tells me the Undefined Index Error. Of course, the error will occur, but I don't need to express recoverykey's value, so how can I remove that error?
The error looked like this when I'm on the url: localhost/websites/index.php
The error vanishes when I'm on the url: localhost/websites/index.php?recoverykey=39238579857
The code goes below:
<?php
$change_code = $_GET['recoverykey'];
$sqlQuery = "SELECT * FROM RegisteredMembers WHERE RecoveryCode='$change_code'";
$result = mysqli_query($conn,$sqlQuery);
$fetch_result = mysqli_fetch_array($result);
$row_count = mysqli_num_rows($result);
if($row_count==1 && !$change_code==0){
$error = false;
$passError = null;
$output = null;
$errorMsg = null;
if(isset($_POST['btn-changepass-submit'])){
$password = $_POST['change-pass'];
// Password Validation
if(empty($password)){
$error = true;
$passError = "Please write your new password.";
} else if(strlen($password)<8){
$error = true;
$passError = "Passwords should atleast be 8 chars long.";
}
// If no error, continue
if(!$error){
/*$fetch_result['userEmail'] = $email;*/
$dtbQuery = "UPDATE RegisteredMembers SET password='$password' WHERE RecoveryCode='$change_code'";
$new_result = mysqli_query($conn,$dtbQuery);
if($new_result){
$errorType = "success";
$errorMsg = "Successfully changed password. <a href=#login-login>Login here</a>.";
$dbQuery = "UPDATE RegisteredMembers SET RecoveryCode='0' WHERE RecoveryCode='$change_code'";
$new_output = mysqli_query($conn,$dbQuery);
} else{
$errorType = "danger";
$errorMsg = "There seems to be an error. Please try again!";
}
}
}
?>
<div id="login-passchange">
<h2>Change your Password</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?recoverykey=<?php echo $change_code ?>#login-passchange">
<span class="<?php echo $errorType; ?>-msg">
<?php
if($output){
echo $errorMsg;
} else{
echo $errorMsg;
}
?>
</span>
<label><span class="danger-msg"><?php echo $passError; ?></span><input type="password" placeholder="Write your New Password" maxlength="255" name="change-pass" required /></label>
<input type="submit" value="Set new Password" name="btn-changepass-submit" />
</form>
<div class="lower-part">
<a class="login-link" href="#login-login">Password Found? Login</a>
<br />
<a class="home-link" href="#home">Go back</a>
</div>
</div>
<?php
} else{
?>
<div id="login-passchange">
<h2>Error 404!</h2>
</div>
<?php
}
?>
The code goes successful in terms of Password changing but I don't want that Error message to appear. Morever, I tried finding this on StackOverflow where there was no specific answer to this question. So, its never a duplicate.
Thanks in advance, for the help!
</div>