Jump to content

Startup Event And Threads


Xero

Recommended Posts

Greetings!I'm having some problems with PHP here. I tried :) but I didn't find a good solution to my problems.First, I need to call a php function when my apache service starts up. How can I do this? Then, like other programming languages (C++, C#, Java)I want to create 2 threads that are called every hour/day. Does php support this? Or does it have other ways to simulate a thread?Thanks for helping me out :)

Link to comment
Share on other sites

First, I need to call a php function when my apache service starts up. How can I do this?
I'm not aware of a way to do that. One workaround is to create a batch file or something that starts Apache and then runs the PHP code.
I want to create 2 threads that are called every hour/day.
Use the operating system's scheduler to run your PHP code whenever you want to. For Windows it's Scheduled Tasks, and for Linux it's cron.
Link to comment
Share on other sites

If you don't have control over the server you won't be able to say what happens when Apache starts. You'll also need a way to set up the scheduled tasks, so your host will need to let you do that through a control panel or something like that. cPanel has an interface to set up cron jobs, for example. The alternative is to set up a scheduled task on your own computer to open a web browser and point it to your PHP script, which will require that your computer is running and is able to access the other web server.Just out of curiosity, what are you trying to run when Apache starts?

Link to comment
Share on other sites

As a note, to my knowledge you don't have access to threads via PHP, but you can do multiprocessing via the stream_select() or forking functions which give you multiple processes rather than multiple threads. A search for "php concurrency" brings up the IBM article on stream select and a blog article by me as it happens about the older process control functions for POSIX forking.Oh, and Zymic doesn't include scheduled tasks, you also won't have control of the apache process as it's shared hosting. You would need to schedule a page visit from your own machine as suggested above.

Link to comment
Share on other sites

Free web hosting isn't going to let you do much, that's why it's free.I'm still not clear on what you're trying to do. You want "your threads" to start when Apache starts, what threads are those? PHP has an execution limit, it will stop executing after a certain time limit. You don't have PHP running in the background waiting for Apache to get requests, Apache just starts PHP whenever a request comes in. This isn't like ASP.NET where you can set up event handlers for various server events, PHP doesn't work that way. What code are you trying to run when Apache starts, what is the code supposed to do?

Link to comment
Share on other sites

I'm making a site that show information of the players of a game. I save the players online on a database and show them all later. I want to run a php function from time to time to catch the new online players.

Link to comment
Share on other sites

It sounds like you just need to have code that runs whenever someone comes on the site to figure out if they're a new player, and add them to the database if so. You can just add that code on whatever page you want that check to happen.

Link to comment
Share on other sites

It sounds like you just need to have code that runs whenever someone comes on the site to figure out if they're a new player, and add them to the database if so. You can just add that code on whatever page you want that check to happen.
Yes, I think I will have to do this. For example, I have on my php code a file() function that opens a website page. Is it my server that will load the page right? I mean, the page will be loaded by the server internet connection. Is there any way in php to execute a php script and do not wait it to finish?
Link to comment
Share on other sites

I will need this because my script retrieves information from web pages. So, for each player, it takes ~3 seconds to get this information. I don't want to wait that long :)Edit: Do you know if it is possible to fork a process in a free web hosting site? (Zymic)Another problem I'm having is:Call to undefined function pcntl_fork()It is not recognizing this function. Why? I'm using Windows (for now) and EasyPHP 5.3.0 (Apache 2.2.13).Thanks again :)

Link to comment
Share on other sites

Do you know if it is possible to fork a process in a free web hosting site? (Zymic)
a) try itb) ask them
Another problem I'm having is:Call to undefined function pcntl_fork()It is not recognizing this function. Why? I'm using Windows (for now) and EasyPHP 5.3.0 (Apache 2.2.13).
InstallationProcess Control support in PHP is not enabled by default. You have to compile the CGI or CLI version of PHP with --enable-pcntl configuration option when compiling PHP to enable Process Control support. Note: Currently, this module will not function on non-Unix platforms (Windows).
Link to comment
Share on other sites

Zymic's version of PHP is not compiled with --enable-pcntl and if it were allowed I'm doubtful that it would be well received on most any shared hosting platform, since a POSIX fork duplicates the memory footprint of the currently running script and is rarely required to build a website. Besides, you probably should be using stream_select() for streamlining downloads anyway, it's somewhat cleaner.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...