Page Redirection

My current code is as follows:

header("Location: http://localhost/Inventory/HomePage.php");

However that does not redirect and only gives me an error saying header cannot be modified

Well, instead of using localhost, use

header("Location: ./Inventory/HomePage.php");
1 Like

you seeing this cuz you already declared the header somewhere else in your code.
try to use set this in your code
ob_start(); This turns on PHP’s output buffering feature. In PHP when you output something (do an echo or print) if has to send the HTTP headers at that time.
put it as the very first thing in your script if you only need it in this one script. If you need it in all your scripts add it as the very first thing in your header.php file.

ob_start();
header("Location: http://localhost/Inventory/HomePage.php");

Thank you, I tried both and still get:
Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd5/170/5390170/public_html/login.php:1) in /storage/ssd5/170/5390170/public_html/login.php on line 75

Now Here is the code:

CS & IT Department Inventory
    <!-- Bootstrap Core CSS file -->
    <link rel="stylesheet" href="assets/css/bootstrap.css">

    <!-- Override CSS file - add your own CSS rules -->
    <link rel="stylesheet" href="assets/css/styles.css">

</head>
<body>

    <!-- Include Nav Bar HTML -->
    <?php
    include 'navBarBeforeLogin.html';
    ?>

    <!-- Page Content -->
    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-12">

                <!-- Page breadcrumb -->
                <ol class="breadcrumb">
                    <li><a href="index.php">Home</a></li>
                </ol>

                <!-- Page Heading -->
                <h1>CS & IT Department Inventory - Login</h1>
                <hr>

                <!-- Create Account Form -->
                <h3>Log Into Inventory</h3>
                <div class="well">
                    <form name="login" id="login" method="POST" action="<?php $_SERVER['PHP_SELF'] ?>">
                        <div class="form-group">
                            <label for="userName">User Name</label>
                            <input name="userName" type="text" class="form-control" id="userName" placeholder="Enter your user name" required="" maxlength="100">
                        </div>
                        <div class="form-group">
                            <label for="password">Password</label>
                            <input name="password" type="password" class="form-control" id="password" placeholder="Enter a password" required="" maxlength="100">
                        </div>
                        <button name="submit" type="submit" class="btn btn-primary">Submit</button>
                    </form>
                    <?php
                    //check to see if 
                    if ((isset($_POST['submit']))){
                        
                        
                        //include database handler file so that we can
                        //call it's functions
                        include 'dbHandler.php';
                    
                        //getting data input into the form and assigning
                        // it to variables
                        $userName = $_POST['userName'];
                        $password = $_POST['password'];
                        
                        //connecting to MySQL and selecting shopper database
                        $link = connect();
                        
                        $status = select_user($link, $userName, $password);
                        
                        if ($status == 0) {
                            echo '<p class="help-block">You\"re not a valid user of Inventory!</p>';
                        } else {
                            $_SESSION['userid'] = $status;
                            
                            close($link);
                            ob_start();
                            header("Location: ./Inventory/HomePage.php");
                            
                            exit();
                        }
                        
                        
                        
                        //close connection to MySQL
                        close($link);
                    }
                    ?>
                </div>
                <hr>

            </div>
        </div>
        <!-- /.row -->
    </div>

So I’m not sure why it says that line one changes the header

use this code to redirect

else{
$_SESSION['userid'] = $status;close($link);
?>


   <script type="text/javascript">
    window.location.href = 'http://localhost/Inventory/HomePage.php';
    </script>


<?php
exit();
}
//close connection to MySQL
                        close($link);
} 

i hope this one works for you use javascript to redirect insted of header function