Unable to echo in php each time a for loop iterates? - php

So I have a PHP script process.php that basically pings either a single host by name or IP and or a range of IPs. The IP(s) are passed via a form.php and I'm happy to report that it works well.
What I'm trying to achieve is the following:
1. Output to the form each IP as it is pinged, currently the script completes before it outputs. This isn't an issue for the single IP or hostname but for the IP range it leaves the user hanging.
My question is can I do this with JQuery/Ajax keeping the logic within PHP and process.php or do I need to move the logic to form.php within JQuery/Ajax?
I know I haven't included the actual code but I just need a check on the logic.
for ($ip=$formipstart;$ip<=formipend;$ip++) {
echo "$ip";
}

You can leave it as is, but I would move your logic into web page, specially if you are good with jQuery and AJAX. For me it is always nicer to present visitor that something happens in background, to show an overlay and spinner somewhere, no matter if that takes 2 seconds or 2 minutes. It will always look better and it may save your hosting resources. For example, you can limit ping-check to 1 per second, or you can stop pinging any time, which is not possible with PHP.

So my question was kind of general on where I should put the logic to achieve a ping response for each request as it happens. My first attempt was a simple form that posted to a process.php page. All the logic was contained within the process.php. The ping tool worked great but for one thing. I couldn't get ping to output as they happened. It would only output the results after the entire logic/page had been processed. The output was correct but it did leave the user hanging.
After creating this post my second attempt moved the logic to the form.php page and was rewritten in a mix of jquery and php. The form.php called the process.php which had been reduced to just the actual ping and result. With jquery and ajax I was able to show each ping response as it happened.
Annoyingly after read many more posts it would seem that if the logic was kept in process.php as pure php i would have to wait until the page has finished processing. I'm yet to see evidence showing me otherwise. It's also a pain that so much logic/code is now visible to the end user.

Related

force reload/refresh a second webpage through php

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.

show a block of code while process is running php

I have a page that allows for used input, when the user inputs his/her specifications and selects submit the algorithm will run. This can take a few minutes depending on input. The user will also be directed to a page which will show their results. I want to show a block of php code that allows for the user to input their email and an email containing the url for results will be sent to them automatically when the results are ready. However, if the results are ready I want the block of code (acting like loading page) to 'disappear' and the results visualisation code to run.i.e. show the results. is this possible? I'm fairly new to programming, so not sure the way to go about this. Thanks for the help.
The thing about PHP script is that, it will keep running until -
1) The server kills the script.
2) Script kills itself.
So whatever you are doing, it will keep running in the PHP script untill it is finished and t the end you could place the algorithm to mail the user. To have the results placed back when they are ready you could use Ajax. You could see the following tutorial for Ajax with jQuery: http://www.devirtuoso.com/2009/07/beginners-guide-to-using-ajax-with-jquery/

Is there a way to return ajax POST results in pieces / increments?

Is there a way to pass results generated within a PHP page (called into action by an ajax post request) back to the document in bits / intervals?
Here is an example...
I am making an ajax POST to a PHP document with keywords passed by the user which scans a few sites to determine if they have resources for the search. If they do the PHP file returns a link to the site, and continues to the next one, if not I will just continue on to the next one..
with an ajax (I use jQuery) I can make this request and wait for the page to load, and then show all the links together easilly, but am wondering if I can display the links one by one as they load from the PHP file so that I don't have to wait for every page to be checked.
Thank you for your input.
You can implement this by having the client send a request for the first X (5 or whatever) results, display those, and then immediately send the request for the next X records. Your client will simply continue making requests and displaying records until it gets an empty response, at which point retrieval is complete.
To make this work you either need to maintain state on the server so that you know "where" in the search to pick up searching, or the client needs to include sufficient information in each AJAX request for the server to know how to continue processing.
By the way, this seems more like a GET operation than a POST.

Is it possible to access an html document from a different PHP script than the one that generated it?

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

How to stop the 1st ajax request if it is called again before the first call gets a response?

There might be some cases that your request takes long time because
of some problems with your client internet connection or your server
connection. So since the client doesn't want to wait he clicks on the Ajax
link again which sends the request to the server again which messes up
the following:
Rendering of our website in the browser because we are giving extra
load to the browser.
What if the second request processed correctly and you showed user
the page and then comes along the error message from your first
request(saying request timed out) which loads above on the correct
content and mess up with the user reading the correct content.
I want to stop the 1st Ajax response if the Ajax function is called twice. How do I do this?
so i want to stop the 1st Ajax response if the Ajax function is called
twice
What you actually want is to prevent a second request when a first request is in progress.
For example, You may have to change the Save button to Saving..., disable it (and add a little progress wheel) to give live feedback to the user. (Facebook does this)
The key is love feedback to the user. If the user is clueless on what is going on, they are going to think nothing is happening.
You might want to check why the operation is taking long
If this is a complex/time consuming operation, like, say a report generation or a file upload, a progress bar should do
If this is because of the client's internet connection, say it up front, like Gmail: Your have a slow Internet connection and this site may be slow. Better still, provide a fallback option, with less/no Ajax.
You say cause we are giving extra load to the browser: this is kind of fishy. You will not be giving extra load to the browser unless you are giving it tons of HTML to render. Use Ajax only for small updates on the browser. You may want to reload the page if you expect a large change.
How bout seeing as your using some form of JavaScript to begin with you have that link either hidden or disabled in a manor of speaking til the pages request has been followed through with. You could for example have the requested ajax wait for a second variable that would enable that link so a user could click it. But until that variable is received from the original AJAX request its disabled to the click, then if it is clicked it disables again and waits for the same variable to come back again.
Think of how some developers disable a submit button on a form so a user can't double submit there form.. same concept of notion here.

Categories