1

hosting-file-format

I control form elements using php.If they are not valid i print an error message below them.My problem is when every element is true in the form ,the page is not redirected the destination page i want,it shows an empty logintest.php(inside php folder) and not redirecting it. Here my index.php code(it is login page actually)

<form action="../php/logintest.php" method="post">
                    <div class="form-group">
                        <input type="email" name="email" class="form-control" id="name" placeholder="Username-mail">
                            <?php if(isset($nameError)) { ?>                
                                <?php echo "<span style='color:red;'>$nameError</span>"; ?>                     
                            <?php } ?>
                    </div>   
                    <div class="form-group">                            
                        <input type="password" name="password" class="form-control" id="parola" placeholder="Password">
                            <?php if(isset($parolaError)) {?>
                                <?php echo "<span style='color:red;'>$parolaError</span>"; ?>   
                            <?php } ?>
                    </div>
                    <div class="form-check">
                        <input type="checkbox" class="form-check-input" id="accept" name="checker">
                            <p class=ctrlButton>You have to accept the terms.</p>
                            <?php if(isset($checkBoxError)) {?>
                                <?php echo "<span style='color:red;'>$checkBoxError</span>"; ?> 
                            <?php } ?>
                      </div>
                    <div class="form-group">
                        <input type="submit" name="submit" value="Enter" id="submit" class="btn btn-outline-danger  login_btn">                     
                    </div>    
                  
                </form>

And my logintest.php code is..

<?php     
       $nameError="";
       $parolaError="";
       $checkBoxError="";
       if(isset($_POST['checker'])){
            if(isset($_POST['email']) && isset($_POST['password']) ){
                if($_POST['password']=="123" && $_POST['email']=="123@gmail.com"){
                    //true input
                    
                   // window.open('https://github.com/', '_blank');
                    header("location:https://www.google.com");

                }
                if($_POST['password']!="b211210381"){
                            $parolaError='Parola is wrong';
                         //   header('Location: index.php'); 
                }
                if($_POST['email']!="abc@gmail.com" ){
                        $nameError='Wrong email try again';
                       // header('Location: index.php');
                }
            }
            else{
                if($_POST['password']==""){
                    $parolaError='Enter a parola';
                }
                if($_POST['email']==""){
                    $nameError='Enter a userName';
                
                }
            }
        }
        else {           
              $checkBoxError='Please accept the terms';              
           }
        include('../index.php');

        ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL ^ E_NOTICE);
      

?>
samaritan
  • 74
  • 9

1 Answers1

1

I believe you cannot set the header location once its already been set see accepted answer here : How to fix "Headers already sent" error in PHP

I am not sure how much hate I will get for offering this solution but this does the trick for me: ( 0 is time in seconds so you can add a delay)

echo '<meta http-equiv="refresh" content="0; URL=https://www.google.com">';
DAG
  • 98
  • 8
  • @samaritan True, you can't send headers once already sent, and I see this is the accepted answer, but __where in the code provided__ are headers, or anything whatsoever, already sent to the client? What am I missing? – bloodyKnuckles May 16 '22 at 09:03
  • @bloodyKnuckles the url is not loaded after submit because he is trying to redirect by changing header location (from inside the view while he already is in index.php ?) I believe there is a way to change the header only if you are executing from the actual page you are on ( in his case index.php) but not when using /views/something.php inside of the index, To be honesty this is how I fixed it long time ago and been using it ever since, my understanding of why it doesn't work with header location might be flawed and if you have a better explanation I welcome it. – DAG Jun 20 '22 at 17:25