0

Hi! I'm working on a website, but at the moment I'm stuck! When I press the Submit button, I will be redirected to

Object not found! The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.

Error 404 localhost Apache/2.4.29 (Win32) OpenSSL/1.1.0g PHP/7.2.0

I have tried to change my XAMPP ports, but it does not work either. I have also googled it, but without results.

I think it has something to do with my header variable.

But i am stil a beginner so can not see mistakes yet.

What should I do?

My code -->

//Database connect
    $host = "localhost"; //Database host --------------------
    $user = "root"; //User in the database --------------------
    $password = ""; //The password to the database --------------------
    $database = "hjaelpsom"; //The specific database --------------------

//Database connect validation --------------------
    $connect = mysqli_connect($host, $user, $password, $database);

    if(!$connect){
        echo "Kunne ikke tilslutte til vores database! Prøv igen senere!";
        header("Location: ../HTML-opret-ung.php");
    } else {

    //Inserting the input to variables
        $name = $_POST["name"];    
        $mail = $_POST["email"];   
        $mailConfirm = $_POST["emailConfirm"];    
        $age = $_POST["age"];
        $password = $_POST["password"];    
        $passwordConfirm = $_POST["passwordConfirm"];  
        $agree = $_POST["agree"];
        $submit = $_POST["submit"];

        //Validation of the user input --------------------
          //If the submit button is clicked --------------------
            if(isset($_POST['submit'])){

              //Check if all fields are filled --------------------
                if(empty($name) || empty($mail) || empty($mailConfirm) || empty($age) || empty($password) || empty($passwordConfirm) || empty($agree)){

                  echo "Du skal udfylde ALLE felterne!";
                  header("Location: HTML-opret-ung.php");
                  exit;

                } else {

                  //Name --------------------
                    if(!preg_match("/^[a-åA-Å ]*$/", $name)){
                      echo "Du skal indtaste et navn ikke $name";
                      header("Location: HTML-opret-ung.php");
                      exit;
                    } else {

                      //Mails --------------------
                        if(!filter_var($mail, FILTER_VALIDATE_EMAIL) || !filter_var($mailConfirm, FILTER_VALIDATE_EMAIL)){
                          echo "Du har indtastet en ugyldig email!";
                          header("Location: HTML-opret-ung.php");
                          exit;
                        } else {

                          $mailCheck = "SELECT * FROM unge WHERE email='$mail'";
                          $mailResult = mysqli_query($connect, $mailCheck);
                          $mailResultCheck = mysqli_num_rows($mailCheckResult);

                          if($mailResultCheck > 0){
                            echo "Din email var allerede i brug!";
                            exit;
                          } else {
                            if($mail === $mailConfirm){
                              echo "Dine emails var ikke ens!";
                              header("Location: HTML-opret-ung.php");
                              exit;
                            } else {

                              //Age --------------------
                                if($age < 12 || $age > 17){
                                  echo "Du er enten for ung eller for gammel!";
                                } else {

                                  //Passwords -------------------- 1:04:32
                                    if(preg_match("/^[a-zA-Z0-9]*$/")){
                                      if($password === $passwordConfirm){
                                        $passwordHash = password_hash($password, PASSWORD_DEFAULT);

                                        //Insert into DB --------------------
                                          $insertSQL = "INSERT INTO unge (Name, Email, Age, Password) VALUES ('$name', '$mail', '$age', '$passwordHash')";
                                          mysqli_query($connect, $insertSQL);
                                      } else {
                                        echo "Dine kodeord skal være ens!";
                                      }
                                    } else {


                           echo "Dit kodeord skal mindst indeholde: et stort bogstav, et småt bogstav og et tal";
                                  exit();
                                }
                            }
                        }
                      }
                    }
                }
            }
    } else {
      header("Location: ../HTML-opret-ung.php");
      exit();
    }

}

  • Note: If you have `exit;` inside a conditional, then you don't need to put the rest of the code in that statement's `else` block. This alleviates the endless indenting syndrome. – Alex Howansky Jan 18 '18 at 19:53
  • Also, your password `preg_match()` is missing a parameter. – Alex Howansky Jan 18 '18 at 19:54
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jan 18 '18 at 19:55

1 Answers1

1

The 404 error shows that the file is either missing or you are redirecting to the wrong directory. What does the $_POST["submit"] return? Also try to remove the headers completely and see if it still returns something. Try a var_dump() or echo after removing the headers.

I have been using xampp myself and mainly 404 has to do with the wrong filepath when I give a wrong input.

The ../ means a directory above the current directory.

GSquadron
  • 204
  • 3
  • 12