Jackymamouth
Jackymamouth

Reputation: 149

Finishing running php functions before header()

I have on my website a form to complete (including file import input and several other standard text inputs).
I use $_POST() to retrieve the input data, and then call PHP functions to save the data in MySQL database.

Once the form is completed and the submit button pressed (<form action="" method="post" enctype="multipart/form-data">), I want all the PHP functions to run and then and only then redirect to another page (using header() ).

The problem is we must put header() before calling our php function or else PHP returns an error. Therefore header() is placed before the PHP functions.

This is what it looks like:

if(isset($_POST)){
header("Location: other_page.php");

save_name($_post['name']);
//... more php functions to save data(including input file)
}

This isn't a problem most of the time, but sometimes it seems that the page is redirected before all of the PHP functions have been run correctly which obviously is a big problem ...

I think it is caused when the functions take too long to run (ie When the input file is too big ?), and the redirect takes effect before the functions have time to finish.

If i could get some help as to get around this problem, it would be great ! =)

Upvotes: -1

Views: 67

Answers (1)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

The problem is we must put header() before calling our php function or else php returns error.

No. You just need to enable PHP's output buffering feature, which will most likely solve your problem.

Upvotes: 1

Related Questions