-1

Hi I am trying to make an if statement, if the username and password equals to something from my database then it will go to one page else it will go to another page. The problem that I am having is that I don't know how to redirect the user to another page without having a href link. I could not find anything relating to this or anything that I could get to work.

This is my form

<form action="" method="POST" class="mbr-form form-with-styler" data-form-title="Mobirise Form"><input type="hidden" name="email" data-form-email="true" value="ebXrsCFkyEilOcwLVabTInb5bd9jF9L29ky83zR0+60VHf98DYd4rDL//d/Y3Ug73DE8L4nKvcAhQVdTN7aTTusZnU8vqZzg6UDpw3DDpXZvzblZJJXTRV01xBPiEitp">
                    <div class="row">
                        <div hidden="hidden" data-form-alert="" class="alert alert-success col-12">Thanks for filling out the form!</div>
                        <div hidden="hidden" data-form-alert-danger="" class="alert alert-danger col-12">
                        </div>
                    </div>
                    <div class="dragArea row">
                        <div class="col-md-4  form-group" data-for="username">
                            <label for="username-form1-2" class="form-control-label mbr-fonts-style display-7">Username</label>
                            <input type="text" name="username" data-form-field="username" required="required" class="form-control display-7" id="username-form1-2" placeholder="Joe blog">
                        </div>
                        <div class="col-md-4  form-group" data-for="password">
                            <label for="password-form1-2" class="form-control-label mbr-fonts-style display-7">Password</label>
                            <input type="password" name="password" data-form-field="password" required="required" class="form-control display-7" id="password-form1-2" placeholder="qwerty">
                        </div>
                        <div data-for="organisation" class="col-md-4  form-group">
                            <label for="organisation-form1-2" class="form-control-label mbr-fonts-style display-7">Organisation</label>
                            <input type="tel" name="organisation" data-form-field="organisation" class="form-control display-7" id="organisation-form1-2">
                        </div>
                       
                        <div class="col-md-12 input-group-btn align-center">
                            <button type="submit" class="btn btn-primary btn-form display-4">Log in</button>
                        </div>
                        
                    </div>
                </form>

This is my php code

<?php
$username = $_POST['username'];
$password = $_POST['password'];
$organisation = $_POST['organisation'];

$con = mysqli_connect('localhost:3308', 'root', '', 'kitchen_organiser');
if (!$con) {echo "Connect error: " . mysqli_connect_error();}

$uname = mysqli_real_escape_string($con,$_POST['username']);
    $password = mysqli_real_escape_string($con,$_POST['password']);

    if ($uname != "" && $password != ""){

        $sql_query = "select count(*) as cntUser from login where username='".$username."' and password='".$password."'";
        $result = mysqli_query($con,$sql_query);
        $row = mysqli_fetch_array($result);

        $count = $row['cntUser'];

        if($count > 0){
            $_SESSION['username'] = $username;
            header("Location: localhost/11PHP/2after login.php");
        }else{
            echo 'Failed';
            
        }

    }
?>

This is the section of which I need some sort of code to redirect the user to one page or another, I had already tried to do a header but I could not get it to work. And I put the 'Failed' as a test to see if the header code could only be stated once.

if($count > 0){
            $_SESSION['username'] = $username;
            header("Location: localhost/2after login.php");
        }else{
            echo 'Failed';

I keep getting this error message Warning: Cannot modify header information - headers already sent by (output started at C:\wamp64\www\11PHP\2login.php:1) in C:\wamp64\www\11PHP\2login.php on line 13

1 Answers1

-1

if you can use a function try this to avoid repetition

function Redirect_to($New_Location){
  header("Location:".$New_Location);
  exit;
}

to call the function: after session, Redirect_to("index.php");

Albasi
  • 1