Home > Blockchain >  Php login redirect with mysqli
Php login redirect with mysqli

Time:01-31

Hello guys I'm making a signup/login site as a learning and practicing project, I am using a tutorial which : https://www.youtube.com/watch?v=LC9GaXkdxF8&t=5768s&ab_channel=DaniKrossing

everything works fine for now and I'm not getting any syntax error but when I logged in with a username/email and password it have to redirect me to ../index.php?login=success page but it doesnt and it stays on index.php page.I can't find if you can help me about it I will be happy to hear from you guys.

<?php

if (isset($_POST['login-submit'])) {

    require 'dbh.inc.php';

    $mailuid = $_POST['mailuid'];
    $password = $_POST['pwd'];


    if (empty($mailuid) || empty($password)) {
        header("Location: ../index.php?error=emptyfields");
        exit();
    }
    else {
        $sql ="SELECT * FROM users WHERE uidUsers=? OR emailUsers=?;";
        $stmt =mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
            header("Location: ../index.php?error=sqlerror");
            exit();    
        }
        else {
            mysqli_stmt_bind_param($stmt, "ss",$mailuid, $mailuid);
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);
            if ($row = mysqli_fetch_assoc($result)) {
                $pwdCheck = password_verify($password, $row['pwdUsers']);
                if ($pwdCheck == false) {
                    header("Location: ../index.php?error=wrongpwd");
                    exit();


                }
                else if ($pwdCheck = true) {
                    session_start();
                    $_SESSION['userId'] = $row['idUsers'];
                    $_SESSION['userUid'] = $row['uidUsers'];
                    header("Location: ../index.php?login=success");
                    exit();


                }
                else  {
                    header("Location: ../index.php?error=wrongpwd");
                    exit();

                }


            }
            else {
                header("Location: ../index.php?error=nouser");
                exit();


            }

        }

    }

}
else {
    header("Location: ../index.php");
    exit();
}

Thanks a lot from now.

CodePudding user response:

<?php
if (isset($_POST)){
    if (isset($_POST['login-submit'])) {

            require 'dbh.inc.php';

            $mailuid = $_POST['mailuid'];
            $password = $_POST['pwd'];


            if (empty($mailuid) || empty($password)) {
                    header("Location: ../index.php?error=emptyfields");
                    exit();
            }
            else {
                    $sql ="SELECT * FROM users WHERE uidUsers=? OR emailUsers=?;";
                    $stmt =mysqli_stmt_init($conn);
                    if (!mysqli_stmt_prepare($stmt, $sql)) {
                            header("Location: ../index.php?error=sqlerror");
                            exit();    
                    }
                    else {
                            mysqli_stmt_bind_param($stmt, "ss",$mailuid, $mailuid);
                            mysqli_stmt_execute($stmt);
                            $result = mysqli_stmt_get_result($stmt);
                            if ($row = mysqli_fetch_assoc($result)) {
                                    $pwdCheck = password_verify($password, $row['pwdUsers']);
                                    if ($pwdCheck == false) {
                                            header("Location: ../index.php?error=wrongpwd");
                                            exit();


                                    }
                                    else if ($pwdCheck = true) {
                                            session_start();
                                            $_SESSION['userId'] = $row['idUsers'];
                                            $_SESSION['userUid'] = $row['uidUsers'];
                                            header("Location: ../index.php?login=success");
                                            exit();


                                    }
                                    else  {
                                            header("Location: ../index.php?error=wrongpwd");
                                            exit();

                                    }


                            }
                            else {
                                    header("Location: ../index.php?error=nouser");
                                    exit();


                            }

                    }

            }

    }
    else {
    header("Location: ../index.php");
    exit();
    }
}

CodePudding user response:

If you want to use a header, type <?php ob_start();?> at the top of the page and <?php ob_end_flush();?> at the bottom of the page.

CodePudding user response:

Verify if the input type submit in your form has these attributes : name="login-submit" & value="submit"
Because the only way you can redirect to index.php is if :

if (isset($_POST['login-submit'])) // is FALSE
  •  Tags:  
  • Related