John Davidson

if statement - PHP loop seems to go wrong


After typing in username and password I redirect the user to a page where a text message is send that he has to fill in (for some not a good 2FA but thats not the discussion :)

What should happen is this:
webpage gets loaded with a form and a text message is send to the user at the same time.
User types in the code he received in the text message.
Codes are compared and user gets redirected.

The part of the text sending works but after that something goes wrong. I logged everything and see that the comparison itself works but different values are compared as another (second) text is send. I do not know why this is? Why is it sending a second text after the post is submitted?

// Initialize the session
// Check if the user is already logged in, if yes then redirect him to welcome page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: index.php");

ini_set('error_reporting', E_ALL);
ini_set('error_log', 'error.log');

// SMS script
$otp = rand(100000,999999);
$mobiel = $_SESSION["mobielnummer"] ;
$tekst = "Je+beveiligingscode+is+:+".$otp."";
$api_key = '****';

$verzoek = "https://*****************" ;

$xml = file_get_contents($verzoek);

// Processing form data when form is submitted

// Check if bevcode is empty
$bevcode = trim($_POST["bevcode"]);

// compare vars
if ($bevcode === $otp) {
$_SESSION["smsoke"] = true;
header("location: home.php");
else {
$login_err = "Dit is een onjuiste code.";



My Answer:
It seems like there may be an issue with your if statement or loop logic in your PHP code. Without seeing the actual code, it's hard to pinpoint the exact issue. However, here are a few things you can check and troubleshoot:

1. Make sure that you are not accidentally sending a second text message in your code after the comparison is made. Check for any additional code that may be triggering the second text message.

2. Double check your if statement condition to ensure that it is correctly comparing the received code with the expected code. Make sure that you are using the correct comparison operator (e.g., == for equality).

3. Check if there are any loops or conditions in your code that may be causing the second text message to be sent. Make sure that the code for sending the text message is only executed once.

4. Verify that the code for sending the text message is not being triggered multiple times, such as in a loop or a callback function.

5. Consider adding debug statements or logging to track the flow of your code and see where the issue may be occurring.

If you are still unable to identify the issue, consider sharing your PHP code for further assistance and debugging.

Rate this post

3 of 5 based on 2321 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us