|
4th September 2008, 12:26 PM
|
#1 (permalink)
| | Senior Member
Join Date: May 2004 Location: Marden, Kent
Posts: 179
| Trigger an External PHP script I have an application that I want it to trigger an external php script to the current page once the current page has completed loading.
I do not want any results to be seen on the screen, just for the script to run.
E.g. It is going to add info to a table and then the external script is going to do some searches on the new submission and then check another table for corresponding matches and email out results in the backend. |
| |
4th September 2008, 01:08 PM
|
#2 (permalink)
| | Registered User
Join Date: Oct 2005 Location: old cottage
Posts: 945
| If you need to wait for page load, then JavaScript is the way to go - write a JS command to trigger the script once the page has loaded.
__________________ Underground, Overground, Wombling Free! |
| |
4th September 2008, 01:39 PM
|
#3 (permalink)
| | Senior Member
Join Date: May 2004 Location: Marden, Kent
Posts: 179
| Terra, can you give me an example of what you mean |
| |
4th September 2008, 02:08 PM
|
#4 (permalink)
| | Registered User
Join Date: Oct 2005 Location: old cottage
Posts: 945
| Assuming your page is PHP:
In the header you'd have:
window.onload = runScript; # This takes care of the window load wait
function runScript() {
<?php
# all your PHP stuff goes in here or just use an include command if the PHP is another page
?>
}
Bit simplistic but that's how I'd do it - maybe with some additional error checking (e.g. ensuring that script only runs if all necessary elements are there).
__________________ Underground, Overground, Wombling Free! |
| |
4th September 2008, 03:05 PM
|
#5 (permalink)
| | Senior Member
Join Date: May 2004 Location: Marden, Kent
Posts: 179
| Hmm not sure that will do what I want.
What I want to happen is that when a process runs, at the end of it, it triggers another script to run on the server, but the person that instigated doesn't know anything about it and can carry on surfing the site.
Bit like a cron job, but a process that occurs on the site triggers it. |
| |
4th September 2008, 03:10 PM
|
#6 (permalink)
| | Registered User
Join Date: Feb 2003 Location: Lost in confusion
Posts: 700
| Actually, that will not work because javascript is client side and php is server side. The php will be executed before the client receives the request, and the resulting javascript will be and empty function, i.e.
function runScript() {
}
Is there a reason this needs to be run separately from the original request? You can just put your php code in the original script, and not output anything.
If not, an option is to use AJAX (XMLHttpRequest) |
| |
4th September 2008, 03:16 PM
|
#7 (permalink)
| | Registered User
Join Date: Feb 2003 Location: Lost in confusion
Posts: 700
| Also, see this: PHP: pcntl_fork - Manual
I've never used it myself, but looks like it'll do what you need. We're doing a similar thing in Java. |
| |
4th September 2008, 03:30 PM
|
#8 (permalink)
| | Senior Member
Join Date: May 2004 Location: Marden, Kent
Posts: 179
| Might be better to explain what I am doing.
I am working on an Self Advertising Site using Joomla and Marketplace.
I have a part in my site that people can register if new adverts are added to the site that meet their requirements. E.g. Between £100 and £200.
Their details sit in a seperate table.
When another person submits an advert at the very end I would like a seperate script to run server side, to collect the details of the new add, search through the table of other people requirements and email them a link to the new advert if it matches.
The reason I do not want it to run at the end of the writead.php script is that that script is already doing enough and I do not want the advertisers waiting anylong that they already do. I just want the few variable needed to be passed to the mailing and searching script and they can carry on as if it is not happening. |
| |
5th September 2008, 12:49 AM
|
#9 (permalink)
| | Senile Member
Join Date: Mar 2005
Posts: 1,009
| well you could make an exec call to perl before you display the screen after validation of your input. Write a mini perl script to call a piece of PHP which does what you want. Providing you route the perl output to /dev/null and conform to a few other rules, then that code will run in the background and your calling php will not have to wait for it to finish. If you know perl, then even better.
See php manual and notes on the exec command for more about it.
__________________
An old dog learning new tricks
|
| |
8th September 2008, 10:13 AM
|
#11 (permalink)
| | Senior Member
Join Date: May 2004 Location: Marden, Kent
Posts: 179
| Quote:
Originally Posted by percepts well you could make an exec call to perl before you display the screen after validation of your input. Write a mini perl script to call a piece of PHP which does what you want. Providing you route the perl output to /dev/null and conform to a few other rules, then that code will run in the background and your calling php will not have to wait for it to finish. If you know perl, then even better.
See php manual and notes on the exec command for more about it. | Ok, I need a little help with this one.
Trying to use the EXEC statement, but I am unfamiliar with this.
I have the following, which is not working, can anyone help.
exec ("test_background.php >/dev/null &");
test_background.php is in the html root of my site. (var/www/html) |
| |
8th September 2008, 11:52 AM
|
#12 (permalink)
| | Senior Member
Join Date: May 2004 Location: Marden, Kent
Posts: 179
| this is the code I am tryin
Code:
-------------------------------------------------------------------------------
$command = "/usr/bin/php5 /var/www/html/test_background.php";
exec( "$command > /dev/null &", $arrOutput );
-------------------------------------------------------------------------------
I am getting this error in the error log
[Mon Sep 08 11:42:30 2008] [error] [client0000000] sh: /usr/bin/php5: is a directory |
| |
8th September 2008, 12:57 PM
|
#13 (permalink)
| | Senior Member
Join Date: Jun 2006 Location: Co. Durham
Posts: 107
| Quote:
Originally Posted by wmuckell this is the code I am tryin
Code:
-------------------------------------------------------------------------------
$command = "/usr/bin/php5 /var/www/html/test_background.php";
exec( "$command > /dev/null &", $arrOutput );
-------------------------------------------------------------------------------
I am getting this error in the error log
[Mon Sep 08 11:42:30 2008] [error] [client0000000] sh: /usr/bin/php5: is a directory | Shouldn't the command just be "/var/www/html/test_background.php". Your command variable should just contain the path to the file you want to run, or that's how I understand it. |
| |
8th September 2008, 02:08 PM
|
#14 (permalink)
| | Senior Member
Join Date: May 2004 Location: Marden, Kent
Posts: 179
| Solved, (For now)
$command = "/usr/bin/php /var/www/html/test_background.php";
exec( "$command test test > /dev/null &", $arrOutput );
And the strings test test can be picked up from the test_background.php file using the $argv[1] and $argv[2] and so on. |
| |
8th September 2008, 08:46 PM
|
#15 (permalink)
| | Senile Member
Join Date: Mar 2005
Posts: 1,009
| Great, that may come in handy in future. Let us know how it works out.
__________________
An old dog learning new tricks
Last edited by percepts : 8th September 2008 at 09:18 PM.
|
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Thread Tools | | | | Display Modes | Rate This Thread | Linear Mode | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |