Does php have the ability to do this:
Let's say you have an observatory. To start a photographing run, you would load the page and click a button. This would make the server start another script, which would run in the background. The page would then update every second (or whatever) with the current information about the observatory.
What I am wondering is if PHP can run in the background like that, and still provide updated status information on page refresh.
What you need is 'AJAX' (Asynchronous Javascript and XML) .. And this is not PHP's ability. The client side script will send asynchronous requests to the server side script, after the page is loaded. It (client side script) will receive responses, and update the page accordingly.
Have a look at these:
http://en.wikipedia.org/wiki/Ajax_(programming)
http://www.w3schools.com/Ajax/Default.Asp
Also have a look at this:
http://www.xajax-project.org/
I guess you are looking for a daemon written in PHP. There's lot info on the net under that term, e.g. re-cycledair.com/php-dark-arts-daemonizing-a-process
Related
I'm trying to force to reload a (second) page if a criteria is met in php
But if the criteria is met, i want the page to force reload everywhere, even if 10 people have it open at once for example.
for simplicty lets say the code is like this:
in /filelocation/script.php:
if {$data == "ok"}{
reload/refresh "reload.php" if it's open somewhere;
}
I came across a software that basicly does this, and i want to understand how this is done.
(it works cross device somehow, so i asume its done through php somehow)
Well, in your PHP code, you cannot simply reload/refresh something for all the users connected. This is simply because the PHP code is only executed when your browser requests a page on the server so it's only executed to build the HTML response and then it stops executing. Once the browser has the HTML response it will render the page and then it waits for an action from the user to do something else (such as clicking on a link or posting a form).
I imagine that you would like that when a specific user does something, like posting a comment or buying a product, you would like all the other visitors to be notified that a new comment has been posted or that the number of products available has been reduced.
To do that, you need to implement some JavaScript which is executed in the browser of each visitor. The idea is to keep a connection with the server with the help of web sockets. This way, you can inform the browser that something has changed.
You could google to find some examples of PHP apps using web sockets. The first example I found:
https://www.twilio.com/blog/create-php-websocket-server-build-real-time-even-driven-application
Another solution could be to have some JavaScript doing some pooling, meaning that every N seconds, it executes an Ajax request to the server to ask if something has changed. This can be done with the help of setTimeout(yourFunction, 10000) to call a JavaScript function every 10 seconds. This function will do the Ajax request and then update the part of your page that needs to change. Just be carefull that if you get a lot of users on your site then you'll produce quite a lot of load on your server. So this wouldn't be a good solution, but it could be an alternative to the web sockets.
Ok, here is a brief description of problem I'm trying to find a solution for.
Let's say we have an online service that can automate different online tasks (clicking on a link, navigate to the page, choose an option from a combo box and finally copy the address from address bar). The site that I want to perform these actions on is completely in JavaScript and I want everything to be at the PHP endpoint I mean a php script get parameters from users, pass it to JS Server and retrieve the result and show it back to the user.
How could such a scenario be done? Any idea?
I know that it can be done by analyzing DOM elements in details but its very very time consuming and volatile. Just want to know is there any way so I can record that action like what we can do with Selenium? What about that php <-> Headless browser interaction?
For the manual recording part of the question, The CasperJS + resurectio Chrome extension combo is really convenient.
In your scenario, where the web context that you are trying to crawl includes JavaScript, CasperJS is an excellent option. You can use it to trigger events, add process steps, include functions to wait and validate after each ajax call before to process any next step.
Here an example about how use casperjs using Javascript and jQuery.
CasperJs and Jquery with chained Selects
Here an example about how you can set casperjs as service in order to pass data to PHP.
CasperJS passing data back to PHP
I m trying to make messenger kind of service. In which I am facing a problem. I am supposed to reload the page manually. But the problem in running it automatically is that it gets reloaded for every second. I had used a javascript for reloading the page for every 1 sec. Inside which i m calling a php script. But the problem with that code is that complete page gets refreshed. Is there a way to reload only a particular part of the whole page and also i should be able run the php script in the background.
Use Ajax. Basic w3schools tutorial
Ajax is a group of interrelated web development techniques used on the client-side to create asynchronous web applications. With Ajax, web applications can send data to, and retrieve data from, a server asynchronously (in the background) without interfering with the display and behavior of the existing page. Data can be retrieved using the XMLHttpRequest object. Despite the name, the use of XML is not required (JSON is often used instead), and the requests do not need to be asynchronous.
[Wikipedia]
I would like to find a way to have my user not having to wait for the output of a php script and being redirected to a page while the script is running on the server.
Basically the user submits a form which takes quite long to process and I would like to redirect the user to a page notifying him that the form is being processed and that its output will be later available (I thought about opening a tab when the output is ready).
Basically I would like something like this, which I tried without success, the
if ($form_valid) {
process_form(); // this would need not to be running on the current page so that the user don't have to wait for it to be ready (timeout problems)
header('Location: http://example.com/form_submitted_output_coming_soon.html');
}
I hope that it is not too vague.
Thank you in advance for any help / advice on how I could do that.
It really depends on the time the script takes to execute if it's seconds, under 10 I would do an ajax request and have a modal progress message
If they take extended amounts of time my approach would be to create or use an existing task scheduler/ report generator
a single system scheduled task entry calling a central management script ( probably not optimal )
You mark a task/report for execution
Concurrency. Count, limit the number currently executing ( so you don't over load the server)
users pool via ajax for their tasks / reports or push to the clients with web sockets
Example on how to fork php to background
Update
I think you would get better performance out of a bot continuously check a database or file for work to do and submitting results back to the database. Alerting users via ajax, web sockets and or email when the work that they need is done / updated.
Here is a good introduction on how to build a web crawler in php
The best approach for solving this kind of problem is to use AJAX to make the request to the server in the background and then update the user once it has finished processing.
You may submit the form with an asynchronous request (ajax) and handle the page forward also with javascript. This way your form is handled asynchronously - you may even wait for the response to tell the user once you have an answer. This asynchronous request will not block the UI.
Just for completeness if you really really want to use php only for this:
Run PHP Task Asynchronously
Here is the scenario:
I have a page that is logging data to MYSQL. I have another page that reads that data and allows it to be viewed. When a new piece of data is logged I would like to have the first script check and see if the viewing page is open in the browser, and if so append the newest data to the end of the view. Also - could anyone point to some info giving an overview of how PHP and the browser interact? I think I have the concept of the DOM down for javascript...but as far as PHP it just appears that once the page is sent, that's it...
You're correct in that once the PHP is sent, that's it.
There is no way to send data to a PHP page once the page is loaded. There is another slightly nastier method, but the easiest way of doing this is going to be polling the page via Ajax.
So, have a script that every 20 seconds, sends a message to another PHP script that contains the timestamp of the last MySQL log you received, then get the script to return all the data that has been set by that time.
I'm unsure how new you are to JavaScript, but the easiest way of doing that is probably using JQuery's $.ajax and encoding the new MySQL records as JSON.
No this isn't possible as you describe. The viewing page will have to poll the server for changes, either by periodically reloading itself, or by javascript / AJAX.
You are right that once the page is sent by PHP it can have no further influence. In fact the PHP execution thread on the server is killed as soon as output is complete, so the thing that generated the page no longer even exists.
To expand on Dolondro's suggestion, rather than periodically polling the server for updates, you could use Server-Sent-Events (newly supported in modern browsers).
With these, you basically just send 1 ajax request to the server, and the connection is held open. Then, the server can send updates whenever it wants. When the browser receives an event, it can add the data to the screen. Even still, the connection is held open, and the server can send additional events/updates as they occur.
W3C page:
http://dev.w3.org/html5/eventsource/
Wikipedia:
http://en.wikipedia.org/wiki/Server-sent_events
More Info:
https://www.google.com/search?ix=hcb&sourceid=chrome&ie=UTF-8&q=server+sent+events