Quick beginner PHP question.
June 20, 2004 5:40 PM   Subscribe

Quick beginner PHP question. I'm messing around with loops. If I run an infinite loop, will it terminate when my session ends (when I close the browser)? Or will the server continue to process it forever, becoming a zombie process and overload the memory or something? I really don't want to mess anything up.
posted by banished to Computers & Internet (7 answers total)
It will continue to run. You may be able to terminate the process or you may have to reboot. It's not a huge deal. You will create an infinite loop at some point. You will run it. You will feel dumb. Look at it this way: if you're just tinkering at home, no one from the IT department is going to give you a dirty look.

If you do this at work, the only defense is to take pride in it. I discovered a way to crash a Windows server with a malformed SQL connection (trying to do SHAPE queries) that is unto an infinite loop like a howitizer to a pop gun.
posted by yerfatma at 6:01 PM on June 20, 2004

php.ini contains a max_execution_time = seconds variable you can set. Check phpinfo() to see what yours is set at.
posted by holloway at 6:20 PM on June 20, 2004

Considering you mentioned "zombie" processes, let's assume you're using unix (it wouldn't actually be a zombie process mind you, a unix zombie process is one which has finished but the parent processes hasn't yet called wait on it to check its return code). The process will indeed keep running, but should be easy enough to kill. Log on to the server, type ps -aux*, get the process ID of your script and kill it with kill process-ID.

*This works for linux and the BSDs, syntax might be slightly different for other unices
posted by fvw at 6:21 PM on June 20, 2004

holloway is right. By default, after 30 seconds PHP will stop running the script. This is controlled by a config variable you can tweak at runtime. You really want to make sure your PHP pages are executing in less than a second if you want your site to handle any kind of serious request load.
posted by Voivod at 8:01 PM on June 20, 2004

...will it terminate when my session ends (when I close the browser)

Since others have addressed your core question, I'll just add this sidenote: HTTP is a stateless protocol. What this means in practical terms is that the server doesn't get notified when you close a window. A lot of beginners eventually get tripped up on mistaken assumptions about the client-server relationship in HTTP (for instance, when trying to exchange variables between PHP and JavaScript), so it helps to hear this one early. Developing a working understanding of basic HTTP is tremendously helpful when starting out in web programming. Here's a nice tutorial/reference.

posted by nakedcodemonkey at 8:24 PM on June 20, 2004

That's not necessarily true, nakedcodemonkey. When you close the browser window, that will close any open sockets. PHP will by default stop the execution of a script at the next output statement if the connection has been closed. There is an option to turn off this behavior and have PHP always run scripts to completion.
posted by Khalad at 11:07 PM on June 20, 2004

Darn, I just knew someone would bring up the exceptions anyway... Yes, there are plenty of ways that an HTTP connection can be kept open if necessary or a workaround (cookies, the javascript refresh method cited in the link, etc.) can preserve state information between requests. But newbies don't usually start with socket programming, after all. Banished is conflating the expiration of a client-side session cookie with termination of its server-side session. Classic rookie mistake. From a beginner's standpoint, it's important to understand that HTTP was not designed to maintain state and that this type of cause-effect relationship cannot be assumed.
posted by nakedcodemonkey at 11:53 PM on June 20, 2004

« Older Free internet telephony   |   VHS -> DVD Newer »
This thread is closed to new comments.