1

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.

Antonios Tsimourtos
  • 1,676
  • 1
  • 14
  • 37

5 Answers5

1

From looking at your code, I don't see any reason why 1.php should be executed again. Though, that said you haven't posted the code from 2.php so the problem might lie there.

However, your problem is just a symptom of a bigger issue you have. One of systematic proportions. Namely how you've structured your code, without a clear and concise separation of concerns. The output buffer stuff is the clearest indicator for this, and you will find that your code will be much easier to fix once you've restructured it to avoid these issues.
There is a previous answer on here, on how to fix header errors, which will get you a long way into the right path.

As a side note, die()/exit() usually comes after the header() call. Not around it. Also, anything after exit() will not be executed. So that ob_end_flush() is unnecessary.

Community
  • 1
  • 1
ChristianF
  • 2,068
  • 9
  • 14
0
  • change queryToCreateUser() with file_put_contents("temp.txt",date('Y-m-d H:i:s')."\n",FILE_APPEND); to see if the date is writen once. if not the problem may be from queryToCreateUser() function

  • change exit(header("Location: 2.php")); with die('<meta http-equiv="refresh" content="0; url=2.php" />'); to see any changes

CatalinB
  • 571
  • 4
  • 11
0

Have you tried writing out the full url?

exit(header("Location http://example.com/2.php"));

That could eliminate some errors. Also, can you post 2.php's code?

0

This is silly, after many hours of searching i found what occurs this situation and let me explain.

When i was copying-pasting the url to hit 1.php, even before pressing "enter" 1.php was executing.

So first user was created by the time i was pasting the url, and the second user was created when "enter" was pressed.

I don't know why this is happening though, it must be something with google chrome, or even google. If for example i try to go to google.com without pressing enter it won't go. But in my case as i said it was already executed one time.

Antonios Tsimourtos
  • 1,676
  • 1
  • 14
  • 37
-1

try this ,should work

echo "location.replace('someurl.php')"; instead of header

Suhas S
  • 24
  • 2