views:

37

answers:

3

I put redirection code on the top of a page which has bootstrapping code below. Does redirection spawn a different process making the redirecting page process as a background process or it kills the current process entirely?

Im using header() for redirection but surprisingly the remaining code below header() which required database connection also got executed. That got me curious.

A: 

As far as I understand, a single process attends both the original request as well as the redirect request. The redirect request is processed once the original request has been completed.

Since there are many URL redirection techniques, we cannot be sure unless you let us know the particular URL redirection technique that you are using.

Alan Haggai Alavi
A: 

So far from my understanding, if you are redirecting directly from apache htaccess file, then no new process will be created. If header() function is used, it sends response to the apache server, and apache redirects the page. In both cases, no new process was created; but the later one runs the php script before re-directing (more costly?).

tanjir
+2  A: 

First, you should never call header() just like that, unless you are very sure that the Drupal helpers are not appropriate (I have never come across such a situation in my 10+ years of Drupal development)

header()will not call the shutdown and other closing functions in Drupal, resulting in potentially broken sessions, wrong statistics and broken modules (which depend on closing being invoked). The fact that sockets and other low-level resources are not closed in such cases, might even crash your apache server (or other servers) at some point.

Rather call drupal_set_header() if you want to set a header. Nine out of ten times you want a redirection header, in wich case you best call drupal_goto(), wich does all the closing and even supports destination-paramters to be followed.

On drupal_goto, all processes are killed (see the exit() at the bottom), hence no running process will be kept. The module_invoke_all('exit') will make sure that all modules get a turn in closing their sockets, connections and what more.

berkes
I placed the the header code right on top of index.php. Can this also cause the breakdown ? Actually I used this redirect to take the site to a maintenance page as the MySQL server required to be stopped for sometime.
Loveleen Kaur
at the top of index.php Drupal is not yet loaded, so indeed, you would need to run header() there, followed by a call to exit(), /if/ you really want this. I doubt that. You can simply put an HTMLpage, called index.html in the Drupal root and then disable or rename .htaccess. If you redirect, Google, bookmarks and services will follow the redirect, and cause many unwanted side effects.
berkes