Found the answer, look my answer below.
I have two files.
1) 1.php
2) 2.php
1.php executes, and after calling a function which creates a user(sql statement - insert), it redirects to 2.php
This is the flow of the 1.php file
<?php
ob_start(); // Output buffuring enabled, to be able to output before the call of header()
session_start(); // I store the name of created user in a variable.
create_random_user(); // just inserting a fake user into an sql database
?>
Note : header() is called inside create_random_user();
The function create_random_user() is :
1) Executing an sql query.
2) Uses header() to redirect after query is finished.
function create_random_user(){
queryToCreateUser() // A mysqli prepare statement, inserting into table. Inside that query when it finishes i store a variable in session.
exit(header("Location: 2.php")); // I tried without the exit, i tried with die(), it is the same
ob_end_flush();
}
What is my problem :
If i don't use header() the insertion of user works just fine. It creates one user without any problems and that's it.
If i use header() i have seen through my database table that it may create two users. Not always but there is a big chance.
What I have found but I don't know why it is happening :
I have searched this problem and I found what really happens using error logs.
When 1.php gets executed it is doing it's job pretty well, there are no errors in the query, but when header() is called, before redirecting to 2.php, 1.php gets executed again. So, that's why 2 users are created and not only one.
Is this a problem occurring by using ob_start()? or session_start()?
Has anyone encountered this problem before?
Some other information :
I am using ob_start() because i have some echoes before the call of header();
I am ussing session_start() to store the info of the user and use them in 2.php file.