Sending a script from PHP file - php

Does anyone know if it is possible to send a command line script from a php page to another server from which the php file is not on? So Php file is on server A and the command line script needs to be run on server B. Both are hosted locally but on seperate machines.
I dont think it is possible but just want to see if anyone else has tried it or knows it doesnt work either.
Thanks,
Mike

It is possible to make a PHP script log on to another server using SSH, but it's very complicated.
It's probably much, much easier to have a publicly accessible PHP file on the remote server, call that through the web, and have it perform whatever actions you need.

What you describe can be done as a a simple "REST service" which Pekka described.
You should also research "RPC" (Remote Procedure Call) and "SOAP" as well.
Both of those come with some baggage, and both are probably overkill, but they are solutions to the problem.

Related

PHP - Multiple scripts at once (AJAX)

After asking this question, someone pointed on the right direction of not being able to execute a second script at all if one was already running.
I usually make apps which rely on the execution of AJAX calls to PHP pages, and today I found that trying to write on a file with fwrite() on a PHP script and trying to read that same file with fread() (to get progress feedback) on another AJAX call ended up in the second script only being executed when the first one had already finished.
Even trying to echo a simple "hello" (echo "hello"; exit;) would not show nothing on the page until the first script was finished.
So, I'm asking: is this a normal configuration? Is this the same on every installation of PHP by default? Is some configuration on php.ini that I can change?
Or it has to do with the server (in my case, Microsoft IIS 10)? Can someone shed some light on how to be able to execute multiple PHP scripts on different AJAX calls at once (or before the others finish)?
I know I'm not giving much information about the settings of my context, but I don't know neither where to look into.
Thank you everyone for your time and help!
As Luis said it could be a write-lock on the file that you're trying to modify. However another possibility if you're using sessions that use files (rather than a database), or a framework that uses file-based-sessions - then this behavior could also be a result of session-locking. My money would be on Luis' answer though - you should probably be using a database rather than a file unless you have a solid reason not to.

Using batch code in php

Is it possible to make a php code on a webpage send out a code that the visitor will run? Like:
<?php
batch("echo text");
?>
Then it will open a cmd window that says text. If it does not work in php does it work in another launge?
PHP runs on a server and has no magical control over what happens on someone else's computer. Once the page has loaded it will be send to the requester's browser.
From there you have access to some local storage/cookies/etc., but only what the browser provides to you. If the browser would give a website access to the cmd, it would mean that any website could simply read pretty much everything on anyone's computer, or do harm in many other ways. You can imagine that this is not really secure.
The only way you can execute batch commands on someone's computer is if you or your program have access to that computer. In other words, the user must download your batch/.exe file and run it manually.

PHP Automatic Self Trigger

Not sure if I understood the use/purpose of PHP entirely, but what seems to me that a .php file only executes when it is being called/executed by something before it, could be a html or a php file.
A thought, is it possible that a php file written, and it would just be activated by its own, example over a duration span of time, every week or so it would do something?
Thanks for looking...
You are looking for a cron job. This allows you to save a line of code on your remote server that will execute based on the criteria you set. You can make it execute a variety of files but PHP files are definitely one of the files you can execute in this manner.
As mentioned by nathan, you will be looking for a cron job. This is a server side setting in the server that will call a url at a set interval.
You seem to not really understand how PHP works. PHP scripts are called server-side before sending data to the client. They are run once when the client is accessing the script.

Creating a PHP web server... executing PHP files

I'm making a simple web server using PHP and sockets. Everything is working fine right now (static content only). I'm interested in supporting the execution of PHP files.
How would I go about doing this? I don't really want to use eval($fileContents) since that does not seem very secure. Is there some way that I can use FastCGI sockets or something?
What about PHP-CGI?
I've decided on using FastCGI, so
here's a more specific question:
How do I pass files into PHP-CGI and get the output as a string?
php-cgi "phpinfo.php" outputs HTML content like I want.
I understand that I can use sockets but I can't seem to find out what to send into that socket to get the output.
Thanks
what's so insecure in eval($fileContents)? or, more familiar but actually equal include $fileName?
(with proper filename sanitizing of course)
apparently it's no more insane than PHP web server itself.
You could exec() the PHP executable from within your web server. Though you'd also have to finagle the passing of GPC data to the script in question.

Dynamic Website on static html

I am trying to create a script that will display the contents of a folder, onto a newsticker, and I was wondering if anyone had a script that could run this. I was thinking probably php, but it has no been working for me.
Thanks for the help
The software I am using is dreamweaver cs4
I'm guessing that you have written some PHP, but are trying to run it locally without a server - for PHP to work you need a server. XAMPP is a good bet to do this locally, or you'll need to upload your file to some hosting that supports PHP.
I'm thinking why you're having trouble with it is because you just copy and pasted the code in a .html file and opening it on your local environment.
With that said, in order for it to run locally, you have to install php and a http server. The code is done on server side, not client side. So either get a hosting service that supports php or download and install your own server and php.
Also, if you already have the above, the code has to be surrounded by a < ?php and ? > tags(without spaces). If you're running it on cli, then you need to make sure you give it execution permission with the path to php, OR execute php < name of script >.
Last, the code you presented provides major security flaws. The first of which is where will the "$dir_path" variable be set? Will that be user given, or will you specify the variable?
Whenever you allow users to view your file system, always make sure you give limitations to it. For example, let's say you did this:
www.example.com/newsticker.php?path=/www/files/newsticker
looks innocent enough, but a clever hacker could say let me try....
www.example.com/newsticker.php?path=/
And so fort.
So be careful and don't allow users to specify directories or execute code.

Categories