2

Trying to create my server.php script, so everythning was fine, till now. I wanted to prevent form resubmission and added header('location: index.php'); to my script. And then I faced the problem:ERR_TOO_MANY_REDIRECTS. And as many of you already understand my database was full of a junk. So, here is my code:

<?php

$username = $email = $password = "";
$usernameErr = $emailErr = $passwordErr = "";
$servername = 'localhost';
$serveruser = 'root';
$serverpassword = 'root';
$db = 'example';

$conn = new mysqli($servername, $serveruser, $serverpassword, $db);

if($conn->connect_error){
    die("Connection failed: " . $conn->connect_error);
}

if(isset($_POST['register'])) {
    $username = mysqli_real_escape_string($conn,$_POST['username']);
    $email = mysqli_real_escape_string($conn,$_POST['email']);
    $password = mysqli_real_escape_string($conn,$_POST['password']);

    if(empty($username)) {
        $usernameErr = "Username is required";
    } else {
    $username = test_input($_POST["username"]);

    if(!preg_match("/^[a-zA-z ]*$/", $username)){
        $usernameErr = "Only letters and whitespaces allowed";
    }

    }

    if(empty($email)) {
        $emailErr = "Email is required";
    } else {
    $email = test_input($_POST["email"]);

    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $emailErr = "Wrong email format";
    }

    }

    if(empty($password)) {
        $passwordErr = "Password required";
    } else {
    $password = test_input($_POST["password"]);
        }
}

if ($usernameErr == "" && $emailErr == "" && $passwordErr == "") {
    $sql = "INSERT INTO users (username, email, password)
    VALUES('$username','$email','$password')";

    if($conn->query($sql) === TRUE) {
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    header("location: index.php"); 
}

function test_input($data) {
    $data = trim($data);
    $data = htmlspecialchars($data);
    $data = stripslashes($data);
    return $data;
}

?>
Shadow
  • 33,525
  • 10
  • 51
  • 64

2 Answers2

0

You can do couple of ways to stop this:

1) You can write either of these, unset($_POST['register']); or $_POST = array(); just before header('location:index.php');so it will not pass through if(isset($_POST['register'])) condition and so it will not go in infinite loop.

2) Or use full URL in header like this: header("location: mydomain.com/index.php"); It will stop infinite loop too.

Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
  • still no ideas m8? I'am surfing through interner for about 2 hours and 0 progress... – FeelsBadMan Oct 10 '17 at 05:53
  • @FeelsBadMan, I did not notice that you have written `header` code outside `if(isset($_POST['register']))`. That was the culprit. :D – Himanshu Upadhyay Oct 10 '17 at 07:10
  • Hey, could you tell me what this code does, if I remove it user's data won't be saved in DB. This code:` if($conn->query($sql) === TRUE) { } else { echo "Error: " . $sql . "
    " . $conn->error; }`
    – FeelsBadMan Oct 10 '17 at 07:20
  • The sentence you wrote in `if` condition: `$conn->query($sql)` will execute the query, and if it fails, it will return `FALSE` and so your `if` part won't get executed. – Himanshu Upadhyay Oct 10 '17 at 07:24
  • I know that, but what it does??? If I delete that 'if' my user's data wont be saved in DB – FeelsBadMan Oct 10 '17 at 07:31
  • I said: `$conn->query($sql)` will execute the query, so if you will delete that, the query you would have written in string format `(like $sql = "select * from table";)` will not be executed ever and so the data will not be stored in DB. Its simple logic man. – Himanshu Upadhyay Oct 10 '17 at 07:33
0

To prevent TOO MANY REDIRECT put this code

if ($usernameErr == "" && $emailErr == "" && $passwordErr == "") {
$sql = "INSERT INTO users (username, email, password)
VALUES('$username','$email','$password')";

if($conn->query($sql) === TRUE) {
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

header("location: index.php"); 

}

within

if(isset($_POST['register'])) { //.................}

this block after checking errors

And to prevent re-submission of form use accepted answer on this question

Preventing form resubmission

Rakesh Mishra
  • 358
  • 5
  • 17