How do I frequently refresh a specific web page using PHP? - php

I need a PHP script running on my server to frequently request a web page.
I thought of using the PHP header function combined with a meta refresh tag, but that won't work because the header will redirect to the URL immediately, and the meta refresh will never execute.
<?php
header('Location: http://www.example.com/');
?>
<html>
<META HTTP-EQUIV=Refresh CONTENT="60">
</html>
Does anyone have any suggestions for how to do this please?

If you want to do this using PHP alone, you'll need to change your solution a bit. Instead of sending an HTTP Location: header, redirecting the user away from the page, you'll want to load the remote contents into a variable yourself using file_get_contents. Then you can rewrite all the URLs and inject your refresh tag into the HTML, and output that.
A far easier solution would be to make an iframe and set that up to be refreshed using JavaScript. If you like I could provide a code sample, just ask.

If I understand your question correctly, you want to automatically refresh a page each time after a certain timeout.
The header generated by the php code is intended to directly redirect to some URL, so take it out because you will not be able to set a time out that way.
It is the client (that is, the webbrowser that views the page) that must reload the page after some timeout, not your webserver. Server side scripting (such as PHP) will not help you therefore.
The client can be instructed to reload the page using that <META ...> tag, or with some javascript:
<script type='text/javascript'>
setTimeout(function () {
window.location.reload(true);
}, 60000); // reload after 60 seconds
</script>

Related

Refresh Page with Status Code after Content was loaded

I am building a file manager with php. Every request is handled by a php script, i.e. also direct download paths will be pre-checked so I can prohibit certain downloads and also display other content.
My problem now is, any here comes stackoverflow into play, that I do not know any solution how I can prepare the download aka display the content first, then refresh the page once so I can send the file ... but without side effects.
Refreshing is not a problem of course, but no refreshing solution works so far. Refreshing by header() keeps my page from displaying any content, refreshing with or javascript works regarding the display of the content, however, downloads with e.g. wget do not work anymore.
The problem in other words is:
Due to the prior content load a valid status code will be sent, thus programs like wget or curl will not follow the refresh, so it will not download the file, only the html.
To get the actual file with curl or wget you need to send http headers, however, the 3XX redirection status codes will need a value for Location. If header('Location: ..') is sent the website will not be displayed before the refresh.
A solution is required that sending appropriate status codes is possible and the content will still be loaded prior the file distribution.
One partially solution has been found with status code 206, which does not require a location value and still works with wget. curl actually does not. so maybe a better solution is still available?
Thanks for your help!
What about the <meta> tag?
<meta http-equiv="refresh" content="5" URL="http://your-url.com/redirect/">
Write this statement after printing content and it will wait 5 seconds before redirecting
<meta http-equiv="refresh" content="5;url=http://your-url.com/redirect/" />

Block access for non-javascript users

I have a webpage written in PHP, HTML and, first of all, JavaScript. I want to block (deny access page and show a message) access my webpage for person who doesn't have enabled JavaScript in browser settings or doesn't support it, because then my page isn't correcly displayed and there are a lot of bugs (also security bugs !). I know that there is possible to write <noscript> statement in HTML, but then something other than this text is dispalyed and it also is removable (e.g. by Inspector function is browsers), I said that without JS my page contains bugs. So, my question : is there any way to block access* for users which doesn't support JS to my page using PHP ? Any other suggestions are welcome :)
*Block access means - deny access main page and show a message
This is not possible to do in a truly secure way.
Yes, you could just serve a page that is blank, and then use JS to actually load the content (e.g. via AJAX), but the problem is that JS must load that code from somewhere, and an attacker could do that too. But here's the real problem:
Users have control over their browsers. JS is client side code. An attacker may choose to run, not run, or change and then run your JS. An attacker may even run their own JS to call or replace your functions. Any security that relies on your JS is broken by default.
So while you could (and people do) show a warning message over your page that is then hidden by JS code, or use JS to load your content, it won't ever be secure.
If you really really really need this. I said three really because I think most of the time you can choose alternative to this.
Set a cookie using JavaScript and pass it along with your request to server and validate the cookie on your server with the passed request. If you are able to verify the cookie Your client has JS else not.
Better way would be to redirect js and non js users from single point. say in your index.html file you have have javascript code that will redirect your clients or visitors to different url. That way you know those users have js enabled else they would not be redirected.
You can make a "sub" page that loads its entire content via AJAX. This will not stop people from hitting your URLS directly though. Don't trust the client.
Other answers and comments already include a lot of details on why you really can't technically block access using Javascript, however, a simple workaround to do something only when JS is enabled, is to call a JS function after DOM loads:
<script type="text/javascript">
window.onload = do_something();
</script>
</body>
</html>
do_something() then could include simple things like switching block element visibility, e.g., hide the non-JS message, plus launch AJAX loader or do something else from the stuff that has been already suggested above.

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.

PHP: how to determine if the browser supports javascript in PHP?

I am building an AJAX deep-linked site.
I want PHP to load all the HTML code of the page if the user is trying to access the site with a Javascript non-supported browser or if it is a search crawler. Basically PHP will return the whole page.
On the contrary, when the user is trying to access the site with Javascript supported browser, I want PHP to return only the template code, and let Javascript (AJAX) take care of the rest. Basically PHP will only load design elements and let Javascript populate them with content.
I looked into PHP's get_browser() function, however it seems it is not such a reliable tool. What is the industry's practice see if the browser supports Javascript or it is a search crawler using PHP?
Background:
Why I want the site to have this behavior.
Since I want the home page to load just by loading the address: example.com, which does not send any query to PHP, PHP returns the HTML code of the home page. This however causes issues when the user tries to load the following page: example.com#foo. So, for this example, PHP will return the home page and once the home page is loaded, Javascript (AJAX) will change the content around so that it shows proper content for #foo. This will make the user to see the home page, therefore load time will be slower and user-experience will not be so nice. However if my PHP script can figure out that if the use with Javascript supported browser is trying to load the page, it will only return the template of the web site, which has no content) and the javascript will populate that template with content whatever is supposed to be displayed for #foo. On the other hand, if the Javascript non-separated browser or a crawler will try to access the page example.com#foo, home page will be returned.
I am using SWFaddress (http://www.asual.com/swfaddress/) library for the deep-linking.
Edit
Thank you guys. I did not think of using <noscript></noscript> before.
Here is what I decided to do. PHP by default will load pages such as example.com or example.com#foo (which is essentially the same as example.com from PHP's point of view since fragments by definition are not sent to the server) blank (just visual template) with <noscript> tag inside for the content of the home page. This way users with javascript will not see the home page and AJAX will populate the content of the page according to the #foo fragment. On the other hand, search crawlers and users without javascript will see a home page.
Thank you again. I think this is pretty simple and elegant solution. If you have any further suggestions, please post a comment or another answer.
You can't do this using PHP. What you can do though is use a noscript tag to redirect to another php page if they don't have javascript:
<noscript>
<meta http-equiv="refresh" content="0; URL=nojavascript.php">
</noscript>
It's not possible to accomplish this in the way you're trying to do it.
It's rare that someone has JS turned off and doesn't know it.
PHP doesn't get passed anything after #, only javascript can do anything with that. So even if PHP could determine if the browser has javascript turned on then it still couldn't read # anyways.
You could include a link inside some <NOSCRIPT> tags that point the user to something like example.com#foo?javascript=disabled.
Unfortunately, browsers do not report whether JS is enabled or not, so there's no way to know from a simple HTTP GET whether or not you should send JS reliant pages.
You should just build an AJAX query that sets a session variable for javascript enabled.
Run this AJAX query before any other information on the site is loaded and then do a simple redirect to the actual site.
You could do something like this pseudo code:
Index.php:
ajax(check_js.php);
redirect(main_page.php);
check_js.php
$_SESSION['js_enable'] = true;
main_page.php
if($_SESSION['js_enable'] == true) {
//execute page
} else {
header("Location: no_js_error.php");
}
Instead of the server trying to sniff our the user's settings, how about using unobtrusive javascript in the first place? This way, the page will degrade gracefully (to the desired state) if JS is not available.

Will static page content wait to load until an AJAX script gets its content?

I'm trying to get off site content and display it on my web site.
I have site 1 and I want to display some content from site 2 on site 1.
My plan is to use AJAX, on site 1, to load a PHP script, on site 1, that will load the content from site 2 and echo it. Then AJAX would take the echo from the PHP script and display it in a DIV on site 1.
My question is...
Will all the other static content of site 1 wait for the PHP script to fully load and echo it's content before it loads?
OR
Will all the other static content of site 1 load initially, then when the PHP script is finished getting the content from site 2 AJAX will display it in the DIV?
This will all happen on page load.
If you do it on Page Load, your DOM will render first before displaying the AJAX loaded content. Page_Load means that the function will wait until.. well... the page is loaded.
If you didn't want this to happen you could have the script running AS part of the Page Load, with inline Javascript.
Say your function was in your <head> section.
<head>
<script type="text/javascript>
function alertMe() {
alert("hello world!");
}
</script>
</head>
Then at the start of your <body> section, you could call that function.
<body>
<script type="text/javascript">
alertMe();
</script>
<!-- Other page content -->
</body>
This might not be good practice however, since the other website might not be available, and may cause your entire page to time out or annoy the user because your website is taking longer to load.
It's pretty common practise to load AJAX, AFTER your page is loaded, so I would go with option #1.
Normally it's the second case: page loads first, then you add ajax response to div.
You can write synchronous ajax request which would stop rendering until response received, but that doesn't seem like a good choice to me.
Perhaps you misunderstand the idea behind AJAX. If not, then I misunderstand your question.
If site 1 runs a PHP script that echoes content from site 2, that PHP script will wait until it gets site 2's content before continuing with the rest of the script. However, if site 1's PHP script sends some JavaScript to the client, then the PHP script on site 1 will run without sending any request to site 2 and that JavaScript will request site 2 for its content.
My apologies if I misunderstand your question.

Categories