Current location: Hot Scripts Forums » Programming Languages » PHP » How to run a php script residing on remote server?

How to run a php script residing on remote server?

Reply
  #1 (permalink)  
Old
Newbie Coder
 
Join Date: Mar 2008
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
How to run a php script residing on remote server?

I have server 'A' and server 'B'

I am trying to find a way to have a php script on server'A', run a small php script that is on server 'B'.

I have looked at the php manual, looked in a couple of books, as well as a search on Google but I can't find anything that I can get working.

I guess one problem is that I don't understand the syntax of the functions such as exec(), system(), or passthru().

Most of the examples that I have found seem to be running a script on the same server as the calling script.

For example the php manual shows the system() function syntax as:

Code:
string system(string $command [, int &_var_return])
then for an example it shows:

Code:
<?php
$last_line = system('ls', $retval);
echo "$last_line";
?>
I believe that this is supposed to display a directory listing.

I'm not sure what the $command should be for running a script on a remote server. I tried using the following:

Code:
<?php
$command = "http://www.mysiteB.com/test/move_file.php";
$myresult = system($command, $retval);
?>
The script, 'move_file.php', is just a small script to move a file from one folder to another on server'B' for testing purposes.

The above attempt does not work and using a similar $command does not work with exec() either. I guessing my syntax for calling the script on server 'B' is way out of whack.

So, question (1) is: Which is the best function to achieve my goal

and

question (2) is: What is the proper syntax to use? (example code?)

Thanks
Reply With Quote

Featured Marketplace Scripts (view more)

  #2 (permalink)  
Old
End User's Avatar
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,029
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by douffle View Post
I have server 'A' and server 'B'

I am trying to find a way to have a php script on server'A', run a small php script that is on server 'B'.
One way would be to make the script on server 'B' accessible to the web (if it isn't already) and use the PHP cURL functions to call the page.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | Call Of Duty
Reply With Quote
  #3 (permalink)  
Old
job0107's Avatar
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 141 Times in 138 Posts
I believe you can do this with Ajax.

PHP Code:

<html>
<head>
<script>
/* AJAX functions. */
function createRequest()
{
 var xmlhttp = false;
 try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}catch(E){xmlhttp = false;}}
 if(!xmlhttp && typeof XMLHttpRequest!='undefined'){try{xmlhttp = new XMLHttpRequest();}catch(e){xmlhttp=false;}}
 if(!xmlhttp && window.createRequest){try{xmlhttp = window.createRequest();}catch(e){xmlhttp=false;}}
 return xmlhttp;
 }
function move_file(url)
{
 var con=createRequest();
 con.open("GET",url,true);
 con.onreadystatechange=function()
 {
  if(con.readyState==4 && con.status==200)
  {
   alert("File successfully moved.");
   }
  }
 con.send(null);
 }
</script>
</head>
<body>
<?php
echo "<script>
 move_file('http://www.mysiteB.com/test/move_file.php');
</script>"
;
?>
</body>
</html>
__________________
Jerry Broughton

Last edited by job0107; 05-08-08 at 07:49 PM.
Reply With Quote
  #4 (permalink)  
Old
mab's Avatar
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,681
Thanks: 0
Thanked 4 Times in 4 Posts
I am pretty sure javascript/ajax cannot make a request to a different domain due to the same origin security policy - http://en.wikipedia.org/wiki/Same_origin_policy

Almost any php statement that causes a http request to the second URL will cause that script to be executed on the remote server. The question becomes what or how do you want any results to be returned.

The following requires the allow_url_fopen setting to be on.

PHP Code:

$url "http://www.mysiteB.com/test/move_file.php";

$myresult file_get_contents($url); // this will return any content that is output by the remote script as it executes 
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #5 (permalink)  
Old
End User's Avatar
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,029
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by mab View Post
I am pretty sure javascript/ajax cannot make a request to a different domain due to the same origin security policy - http://en.wikipedia.org/wiki/Same_origin_policy
I believe this is correct- Javascript is domain-limited for security reasons. Basically, you can't download AJAX data from another domain. However you can get around this to some degree:

"You bypass it by dynamically inserting a script tag on your page. Script tags get run when inserted and can be downloaded from anywhere and any domain. The simple solution to the restrictions of XmlHttpRequest is to have a receiving function on the page with the JSON data as a parameter."

This requires access to the remote page. If you don't have that, I think AJAX is a no-go.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | Call Of Duty
Reply With Quote
  #6 (permalink)  
Old
job0107's Avatar
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 141 Times in 138 Posts
Quote:
Originally Posted by End User View Post
I believe this is correct- Javascript is domain-limited for security reasons. Basically, you can't download AJAX data from another domain. However you can get around this to some degree:

"You bypass it by dynamically inserting a script tag on your page. Script tags get run when inserted and can be downloaded from anywhere and any domain. The simple solution to the restrictions of XmlHttpRequest is to have a receiving function on the page with the JSON data as a parameter."

This requires access to the remote page. If you don't have that, I think AJAX is a no-go.
I believe you and mab are right. I can get the script to function in IE after
answering the question:
Quote:
This page is accessing information that is not under it's control. This poses a security risk. Do you want to continue? YES/NO
But it will not work at all in FireFox.
__________________
Jerry Broughton
Reply With Quote
  #7 (permalink)  
Old
wirehopper's Avatar
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 110 Times in 107 Posts
One alternative - include

http://www.php.net/manual/en/function.include.php

Quote:
If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see List of Supported Protocols/Wrappers for a list of protocols) instead of a local pathname.
This does require that the remote server be accessible through the web.

You can refuse access to all other servers except your publicly accessible server with an Apache configuration.

http://httpd.apache.org/docs/2.0/mod/mod_access.html

On the remote server, in .htaccess

Code:
<Directory *>
Order Deny,Allow
Deny from all
Allow from public_server.com 20.30.20.29
</Directory>

Last edited by wirehopper; 05-08-08 at 10:07 PM. Reason: Clarification
Reply With Quote
  #8 (permalink)  
Old
mab's Avatar
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,681
Thanks: 0
Thanked 4 Times in 4 Posts
Using an include() with a URL is dangerous, because if someone either gets you to include raw php code from their URL or gets their raw php code onto the site you are including, that raw php code will be executed on your site and they can take over your site.

Edit: Staring in php5.2, allow_url_include (off by default) was added to prevent include() using a URL.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 05-08-08 at 10:14 PM. Reason: More info.
Reply With Quote
  #9 (permalink)  
Old
Newbie Coder
 
Join Date: Mar 2008
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Wow! A lot of suggestions. As a newbie to php, I know nothing about cURL or AJAX but I will certainly look into that. I will try the suggestions offered and see where I end up.

A big thanks to everyone for your suggestions.
Reply With Quote
  #10 (permalink)  
Old
job0107's Avatar
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 141 Times in 138 Posts
Quote:
Originally Posted by mab View Post
Using an include() with a URL is dangerous, because if someone either gets you to include raw php code from their URL or gets their raw php code onto the site you are including, that raw php code will be executed on your site and they can take over your site.

Edit: Staring in php5.2, allow_url_include (off by default) was added to prevent include() using a URL.
Also as mab roundabout pointed out, when you use include or require, to include a remote file, the PHP and Javascript in that file will try to run on your server and not the remote. So if you are trying to make something happen on the remote, it won't.

I believe include and require work similar to file_get_contents. It does get the contents of the file and includes it in your code. But when the code is run, it is run local only unless the code uses complete paths and then you run into the permissions issue again.
__________________
Jerry Broughton

Last edited by job0107; 05-08-08 at 10:24 PM.
Reply With Quote
Reply

Bookmarks


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

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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] it works on localhost but not on hosting account? myslowquietlife PHP 42 12-19-08 09:31 AM
script to test php mail on server - HELP bloodl PHP 5 07-25-07 07:29 PM
how can call .exe(executable file) via or using php script on web server mohit Script Requests 0 08-03-06 01:59 AM
asp script run autonomously on server shermski ASP 4 10-03-05 07:09 AM


All times are GMT -5. The time now is 01:56 PM.
vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.