George....
Well, I'm not going to debug your php (I have enough trouble finding my own bugs!), but one thing I notice:
header('Location:/welcome/index_main_site_entry.php');
seems suspect.
The php docs say:
header("Location: http://www.example.com/"); /* Redirect browser */
and further note:
Note: HTTP/1.1 requires an absolute URI as argument to Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:
Maybe it's only working some of the time, depending on the clients?
brew