PHP post to url - php

I have a python script that listen to specific port on a server and waits until it gets an http request from browser or program to run.
I want that script to run when the admin on my Joomla site clicks on the save button.
That means that I need a way to go to the link that the server listens to without showing it to the the user (a way to do it in the background).
Any ideas?
Thanks!

PHP is server side, so anything implemented there would not be visible to your user, unless it gets rendered as HTML output.
Does the request need to come from the client machine? Or can it come from your web server?
If it needs to come from the client, I would recommend using PHP headers.
Script 1
header("Location: exec_something_private.php");
exec_something_private.php
exec("do something");
header("Location: success.php");
The above implementation could call a php file, do what you need to do, and then redirect them immediately to a new page?
If the function can be called by the webserver, simply uses exec to run a curl request or something, no?

Related

Where is determined that the file has PHP code in it?

I am reading "Learning PHP, MySQL, JavaScript, and CSS: A Step-by-Step Guide to Creating Dynamic Websites" 4th edition. I came across request-response procedure. After fetch page, the data should be sent to web browser and the web browser should send the data to web server after it detects PHP in it. Am I wrong?
You are wrong.
The browser will never detect PHP in a page. PHP is executed by the server and the output of the PHP program is sent to the browser. The browser only sees HTML (or an image, or whatever else you want the PHP to output).
The web browser doesn't care what the server is running. It talks HTTP to the server, and the server talks HTTP back. The server's configuration is what tells the server that a URL should be routed to a particular PHP script.

Detecting Browser exit in PHP

I have looked at a few topics (here & google) regarding detecting browser exit in php and im not really any clearer on how to do so.
I tried the register_shutdown_function in PHP but that was executing even when i refreshed the browser page.
Can anyone help?
Thanks in advance
PHP is a server side language. Browser events are handled by DOM events. On "onunload" or "onbeforeunload" events, sending an AJAX call to a PHP file.
And in this other question there is a flavored explanation of what I'm saying.
Please explain what you want to do when the browser closes, to see if there perhaps is another way to do so.
A web server sends its response to the browser, and then (usually) closes the connection. You'd have to do something in Javascript, but that won't catch all conditions. You certainly can't detect it serverside.
It can be detected using the Javascript onbeforeunload or onunload functions, but that is absolutely not accurately, since it won't detect:
a browser crash
a browser exit
a computer shutdown
a link click on the page
when going Back in the browser
Also see this answer.
So for example when you want to log out users when they close the browser, you'd better use a "keepalive" mechanism instead of a "say goodbye" one. You can then log those users off on the server (e.g. using cron) whose sessions have not been active (i.e. who haven't sent a "keepalive") for more than X minutes.
I don't think there is any foolproof way to detect a browser close button in PHP or Javascript.
It is much safer and better to handle it via timer based polling OR just simple session timeouts on server side.
One solution that is at least fool resistant is to send a heartbeat to the server using Javascript and Ajax, then assuming that the browser window has been closed when the signal stops pulsing.
Another would be to use web sockets to maintain a constant connection until the browser window closes.
In any case it would take quite a bit of work from your part to set it up
Not just with PHP.
PHP runs server-side, and is far done processing your page by the time the user will have a chance to close their browser. You could technically detect if PHP was still processing the page and the user closes it, with a specific configuration. However, it is not ideal. See connection_aborted().
What you need to do is set up a long-polling connection with JavaScript, and monitor it server-side. You will then get an idea for when that window is closed. That connection could be made to your PHP script, allowing PHP to check connection_aborted(). Note that you will need to set up ignore_user_abort() for this to work, or configure PHP.ini accordingly.

Run php script remotely

I have a php script (qa.php) that my app points to on my current server. I just recently switched servers and want the qa.php script running from this new server.
Is there a way to have a php script redirect or automatically run the remote php script?
If you mean a redirect in the sense of a client visiting this page, you can use the header() function to change the location header and simply perform the redirect.
header('Location:http://www.your-new-domain.com/qa.php');
But if you mean (and this I'm assuming) running the script without direct client interaction, there are a few different approaches. One good way is to use the Curl library to send a request formed the way you'd like to the script. I've used this method in scheduling cronjobs before that had to fire signals to several controllers at once and record the output.
http://www.php.net/manual/en/intro.curl.php
It's also acceptable to use the file_get_contents() php function to simulate a client visit on the page if you don't need to manipulate any specific headers for the request going to the server-side script.

Call web URL with PHP script from a shell script on Mac OS-X

I have a server with a PHP script that pulls data from a source and populates a database. I need to call this PHP script repeatedly, each time with a different parameter.
I need to create a shell script on a Mac (which reads in a text file with the list of parameters - that part is not a problem) and for each parameter, runs the PHP script/URL in a web browser.
The PHP is on a remote server, so I need to load a web browser instance (safari or firefox) and instruct it to load the URL (ie. something like http:/myserver.com/scriptname.php?param1). Then I need to wait for it to complete and trigger the same URL with the next parameter.
I don't know the incantation to launch the web browser with a URL (I am a former Windows dev not a Mac OS-X pro, yet). I also don't think there is a way to detect when the script completes - but I don't want to end up with 100 instances of the browser running simultaneously.
Any help would be greatly appreciated!
If you just need it to hit a php page on a remote server, don't use a browser. Use curl, or some equivalent.
curl http:/myserver.com/scriptname.php?param1
open -a Safari http://stackoverflow.com

Handling web requests on server side

Situation: a user clicks on a link, the server gets the request and starts processing it. In the meanwhile, the user clicks on another link, the server gets the new request while processing the 1st one. What happens? On the client side we only see the webpage from the 2nd request, but is the process from the 1st request killed on the server when receiving the 2nd one? And is it managed by the server or the language (Apache or PHP)?
Depends. If the browser does not drop the connection to server, it'll have absolutely no idea that the client has navigated elsewhere. If it does drop the connection, it's up to the Web server to choose to detect it and abort the processing thread or not.
Either case, this is the nature of statelessness of HTTP. You shouldn't rely on anything in this regard.
Both requests get served (if the browser did send the second one).
You would only see the second page, but if you'll look into access_log you'll surely notice two requests.
That's how HTTP works.
You can use ignore_user_abort() to tell a script to continue (or not) after the connection has been terminated.

Categories