Refresh PHP file on page when file changed - php

I have made a very successful PHP chat box that saves the chat to a text file. I'm almost done with it except for the fact that I have to have it for user #2 when user #1 sends a message. I have looked into AJAX but I'm not using any JavaScript so I think it would be hard to put it in my script. I don't want the page or iframe or anything to be refreshed until a new message (file updated) had appeared.

if you can afford to use HTML5, then HTML5 - WebSockets is the way forward for your project.

PHP is a server-side language. You would be required to use javascript or alike for interaction from the browser outside of the PHP script.
In your PHP script, if you echo the following to User2, it will cause their browser to refresh the current page:
<script>window.location.reload();</script>
Or you can do the following to refresh the page every, say, 60 seconds:
<script>setTimeout( window.location.reload()", 60*1000 );</script>

Related

Can I know on PHP (without javascript) if a user has right-clicked oppening a link?

I was coding from some ideas collected over the web a "solution" to redirect all pdf, for example, downloads on a server to a reCAPTCHA form and, after passed, download the requested file. It is cms agnostic and works but I have one situation that downloads an incorrect file.
The code is at: https://github.com/Siot/docafi (request.php file)
I explain the process:
All .pdf requests where redirected to request.php through .htaccess:
RewriteRule ^(.+.pdf)$ request.php?file=$1 [NC,L]
If it's the first time ($_SESSION var) request.php will show you a reCAPTCHA form.
If reCAPTCHA passed -> download starts.
If error -> error message.
If reCAPTCHA was passed previously on the same session, download starts automatically.
The special case is when is the first time in the session but starts download using right-click over the link.
It downloads an incorrect file content because I can't show the reCAPTCHA form. Can I know if a user had right-clicked over the link without javascript? I want a transparent solution over the different cms/html files on the server.
Can I solve this on another way?
No, selecting 'Save as...' in the popup menu works exactly as it should - it saves whatever is fetched by the link to local disk instead of trying to show it in the browser. It's thus completely correct that it's downloading the HTML file with the reCAPTCHA instead, and there's no way to detect it, not even with Javascript - to the server it's all the same, since the browser issues the same request, it just processes the result differently. You could set a flag on onContextMenu event, but it'd just have the wrong setting anyway if you then opened the link by leftclicking it or pasting the address in the address bar by hand.
It should be possible with javascript however PHP i'm fairly confident there will be no way this can be done considering it is server side.
Look through some previous stack overflow questions like:
how to get right click event javascript

Does php's include option do a partial page refresh?

I've started using PHP locally and notice that if I use include files within a main index.php file, the whole page does not refresh when I click a button/link that will include a new/updated include file. My question is, is php's include option like ajax and only does a partial page refresh? If so, am I better off using just plain php or should I stick with ajax?
PHP code is interpreted server-side so it doesn't use AJAX like client-side JavaScript. Clicking a link will request a new page, and PHP will interpret the requested files and return the output. You're likely not experiencing refresh times because you're working locally. If you take a look at the Network tab of Chrome Developer tools you'll see the refresh take place.
No, only AJAX do partial page refresh. Because you're programming in localhost and you're including the same file in multiple pages, you won't notice the loading time and it looks like nothing has changed (on the browser).
Include function just includes the content of a php file in the one where it was called, all PHP code is done on the server-side, so the content is included in server side and sent all of it to the client (no ajax).
So, stick with ajax because it's the only way to do partial refreshing.

Delay for Simple HTML DOM Class

I'm using the simple html dom class and have gotten it to work on basic pages and can view the information I want. However, when I attempt to use it on a page that reloads a div with ajax, I can't seem to get it to "wait" before reading the page.
I basically want it to load the page, then wait 2 seconds before reading the page content (so that the new div has time to load). Is this possible or am I trying to use the class incorrectly? I'm manually inputting the URL, so it's not a link issue.
Example Page:
- You can see the load issue when you navigate through the pages.
Someone suggested curl and I tried that with the same results.
Thanks in advance.
PHP runs on the server. JavaScript (e.g. AJAX) runs in the browser, after the PHP code on the server has finished producing the page. You can't make a PHP program, running on the server, wait for an event that happens later in the browser.
You'll need to either load the content for that div using PHP code, or replace the PHP DOM-parsing code with JavaScript code that does the work on the client.
You can use the Sleep method ( http://php.net/manual/en/function.sleep.php ) if you simply want to delay the program execution for some set amount of time.

Trying to display a Loading screen in PHP, but doesn't display until after script is executed?

I am trying to display a loading message with an loading bar animation so people know the script is working away.
However even though the HTML is at the top of my PHP script , it only gets displayed once the script is fully loaded.
The script takes about 40 seconds to complete because it Generates a PDF, Inserts Data into a DB then sends a MIME email to an external MX (takes the longest part).
Here is the HTML that displays the loading message:
<div class="grid_16">
<fieldset>
<h2>
Processing <img src="img/loading.gif" alt="loading animation">
</h2>
</fieldset>
</div>
<?php
//rest of php page
Any idea how I display this out to the user so they don't get impatient and start hitting F5 etc.
Thanks
Merry Christmas!
PHP loads on the server and is then passed to the browser. This means that, until the page has completely finished loading on the server it will not be passed to the browser for display.
What you will need to do is use javascript and ajax. Firstly, remove the slow PHP from the HTML and show the page. Then once that has shown load the PHP script via ajax and once it has completed write it to the document.
My friend, PHP only sends output when it finishes script interpretation(1). So, your option is to put the gif in the preceding page fired (using some JavaScript) with the event that moves the user from oldpage.php to slownewpage.php. So, the gif will stay in front of user until PHP sends the new HTML result and the browser has something to work with.
(1) Unless you order him to do otherwise with something like ob_flush
run flush(); after the part you want to directly output (within php), because you are probably buffered especially if you are gzipping your code. You can load the page like others said and pull the result with javascript as well (actually that's probably the better option).
.. can I ask why you are sending a long message during a page load? Can't you have a cron job do it (check every few seconds to see if something needs to be sent)? I think they have cron programs for windows as well.

Stop or interrupt php script for particular time

Can I Stop or interrupt PHP script for particular time?
I would like to implement logic like below:
on mydomain.com/index.php there will be flash appear showing some intro of a product for suppose 20 sec. Once it complete, on same index.php page the home page of site will appear.
I am not aware about flash (action script).
Is it possible in PHP or Javascript ?
Usually "splash pages", as the're called, are made up of a seperate page.
In flash you can use the following code (Actionscript 3). Put it int the last frame, or use an event listener to redirecrect when the file is finished. The actual redirect looks like this:
getURL("http://www.woursecondpagehere.com", "_self")
Where you place it is up to you.
EDIT: I think that this is a reliable solution because this guarantees (if implemented correctly) that the page won't move until Flash is done. CSS and Javascript will work fine too.
There isn't a need to interrupt PHP in the scenario given. Though I think what you want is to load the rest of the HTML after a certain event occurs.
If thats the case then you can use AJAX to load the additional HTML from the server. Or you can use CSS to hide that content and show it after a certain point.
The META Refresh tag is probably not what you want since it will redirect the user after 20 seconds, regardless of how long it took to load your Flash file, then play it. Since the speed of the user's connection cannot be reliably predicted, you will probably end up with a poor user experience.
What you want to do is definitely possible but it will involve some interaction between the Flash object and the rest of your page. If you could do as Moshe suggested and simply have the Flash object redirect the user's browser to your actual home page with content on it, that would be easier.
If you insist on keeping everything on the same page, one way to do it is to call a Javascript function from the Flash object once it's finished playing. The function you call should be written to hide the Flash object and/or it's container and display the container () with all of your content that you're ready to show.
See this Codefidelity blog post for a quick tutorial on how to call JS functions from Flash.
Also, to clarify, you won't be interrupting or changing when your PHP script runs. That runs on the server before the page is created and sent back to the user's browser. All you need to do is structure the HTML/CSS of your page to have two DIVs: one with the Flash object in it and the other with all your normal page content. However, set the CSS to only show the DIV with the Flash object, then finally use Javascript to hide that DIV and show the one with the content in it.
Try this,
write the your flash (splash screen) <embede> code in index.html and simply use javascript redirect function with javascript timer functions to redirect to index.php where you actual content is there.
something like...
window.location = "http://your.domain.name/index.php"
or
location.href = "http://your.domain.name/index.php"
use setTimeout() to call redirect after specified time.

Categories