UnitedForums - UK Web Hosting Forum UnitedHosting Community Hosting Forums
Network and Server StatusCustomer SupportUK Web Hosting
UnitedHostingUnitedHosting Sitemap UK Hosting ForumUK Web HostingWeb Hosting ForumsUK Reseller HostingWeb Host CommunityUK Managed Dedicated ServersHosting Help and SupportUK Domain Name Registration

Go Back   UnitedForums.co.uk > UnitedHosting Community > Website Development & Scripting

Reply
 
Thread Tools Rate Thread Display Modes
Old 4th September 2008, 12:26 PM   #1 (permalink)
wmuckell
Senior Member
 
wmuckell's Avatar
 
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.
wmuckell is offline   Reply With Quote
Old 4th September 2008, 01:08 PM   #2 (permalink)
Terra
Registered User
 
Terra's Avatar
 
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!
Terra is offline   Reply With Quote
Old 4th September 2008, 01:39 PM   #3 (permalink)
wmuckell
Senior Member
 
wmuckell's Avatar
 
Join Date: May 2004
Location: Marden, Kent
Posts: 179
Terra, can you give me an example of what you mean
wmuckell is offline   Reply With Quote
Old 4th September 2008, 02:08 PM   #4 (permalink)
Terra
Registered User
 
Terra's Avatar
 
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!
Terra is offline   Reply With Quote
Old 4th September 2008, 03:05 PM   #5 (permalink)
wmuckell
Senior Member
 
wmuckell's Avatar
 
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.
wmuckell is offline   Reply With Quote
Old 4th September 2008, 03:10 PM   #6 (permalink)
Euge
Registered User
 
Euge's Avatar
 
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)
Euge is offline   Reply With Quote
Old 4th September 2008, 03:16 PM   #7 (permalink)
Euge
Registered User
 
Euge's Avatar
 
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.
Euge is offline   Reply With Quote
Old 4th September 2008, 03:30 PM   #8 (permalink)
wmuckell
Senior Member
 
wmuckell's Avatar
 
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.
wmuckell is offline   Reply With Quote
Old 5th September 2008, 12:49 AM   #9 (permalink)
percepts
Senile Member
 
percepts's Avatar
 
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
percepts is offline   Reply With Quote
Old 5th September 2008, 12:02 PM   #10 (permalink)
TygerTyger
Lumberjack and OK
 
Join Date: Aug 2004
Posts: 833
Two options:

Asynchronous Processing With PHP | Robert Accettura’s Fun With Wordage
Nick Yaitsky » Blog Archive » Asynchronous PHP calls

Or if it's possible, an invisible iframe that has the src of the script you want to run is a very simple way of doing things.
TygerTyger is online now   Reply With Quote
Old 8th September 2008, 10:13 AM   #11 (permalink)
wmuckell
Senior Member
 
wmuckell's Avatar
 
Join Date: May 2004
Location: Marden, Kent
Posts: 179
Quote:
Originally Posted by percepts View Post
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)
wmuckell is offline   Reply With Quote
Old 8th September 2008, 11:52 AM   #12 (permalink)
wmuckell
Senior Member
 
wmuckell's Avatar
 
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
wmuckell is offline   Reply With Quote
Old 8th September 2008, 12:57 PM   #13 (permalink)
knapper
Senior Member
 
Join Date: Jun 2006
Location: Co. Durham
Posts: 107
Quote:
Originally Posted by wmuckell View Post
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.
knapper is offline   Reply With Quote
Old 8th September 2008, 02:08 PM   #14 (permalink)
wmuckell
Senior Member
 
wmuckell's Avatar
 
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.
wmuckell is offline   Reply With Quote
Old 8th September 2008, 08:46 PM   #15 (permalink)
percepts
Senile Member
 
percepts's Avatar
 
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.
percepts is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 05:21 PM.

UK Web Hosting  |  UK Reseller Hosting  |  UK Dedicated Servers UnitedHosting  |  UnitedSupport  |  UnitedForums  |  SEO by vBSEO 3.0.0
Copyright © 1998-2008 United Communications Limited. All Rights Reserved. Registered in England and Wales 3651923 - VAT Reg No. 737662309