Multibrowser refresh/reload page - php

I was using this code to reload the page, however I'd only tested it on Chrome and when I tried it on Firefox I realized it didn't work. How do I make it work in other browsers?
echo 'Reloading. <META HTTP-EQUIV="refresh" CONTENT="0">';
Thanks

Instead of pushing out invalid HTML, instead, send the actual header using PHP's header function.
header("Location: http://where.you/want/to/redirect.to");
If the page has already loaded (even partially), then this approach will not work -- in that case, either using JavaScript or the <meta> tag will do the trick -- just remember that the <meta> tag must be added to the <head> portion of your HTML file.

<script type="text/javascript">window.location.reload(true);</script>
What you are doing is telling the browser to refresh. However, the <meta> tag you are using is meant to be in the <head> part of the document. That is probably why Firefox isn't properly executing it.
If you instead use the blurb above, which is javascript, you can put that code virtually anywhere in the document and it will cause the page to refresh.

My guess is that it's not working because it's not in the <head> tag. Generate a properly formatted HTML document and it should work. But why in the world would you want to refresh the page after zero seconds?

Related

Send IE 7 (and below) to old/sub directory of site. <!-- [if IE 7]><"LINK TO OLD SITE"/><![endif]-->

I have just launched a site (using Joomla and a custom template), which doesn't display that well in IE7 (and I guess below too). I have looked around and have found out that you can link to different style sheets from my index.php, however, instead of linking to a different style sheet, I want it to link to the older site which is still live (under www.mydomain.com/old).
Is that at all possible?
As stated in the title, I have looked around and found out that you could use an if statement like this -
<!-- [if lte IE 7]><"LINK TO OLD SITE"/><![endif]-->
is what I'm trying even possible? I haven't got anywhere with it so far, trying the usual html tags of href="http://www.mydomain.com/old"
Any help would be great on this. I'm just getting stuck at the moment!
Conditional comments are used in the client-side part of your page, and so are not useful for PHP. You can use a conditional comment with JavaScript like this:
<!— [if lte IE 7]>
<script type="text/javascript">
top.location.href = "http://www.mydomain.com/old";
</script>
<![endif]-->
The disadvantage of this is that you are performing this task on the client machine, which is slower than if you performed the redirect on the server and sent to user to a different page instead. You can do this using PHP by checking the browser version and redirecting with header:
$browser = get_browser();
if($browser->browser == 'IE' && $browser->majorver <= 7) {
header('Location: http://www.mydomain.com/old');
}
Bear in mind that for this to work you must call header before any data is sent to the client.
Well, much reasonable would be to catch IE7 users before they started to render the page.
So it could be done with server-side script either with some mod_rewrite. Would be easier and faster.
I used Google to find this.
The objective of this technique is to enable redirects on the client side without confusing the user.
...
In HTML and XHTML, one can use the meta element with the value of the http-equiv attribute set to "Refresh" and the value of the content attribute set to "0" (meaning zero seconds), followed by the URI that the browser should request.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Tudors</title>
<meta http-equiv="refresh" content="0;URL='http://thetudors.example.com/'" />
</head>
<body>
<p>This page has moved to a <a href="http://thetudors.example.com/">
theTudors.example.com</a>.</p>
</body>
</html>

How can I redirect using a PHP script called from an SSI?

On a WAMP server, I have a server-side include in a file, a.shtml, composed of the following code:
<!--#include virtual="./req.php"-->
The contents of req.php are:
<?php
Header("Location:index.php");
echo "still here";
?>
When I open a.shtml, I see the text still here, but the page has made no attempt to redirect itself. Why is this? And is there any way to make it work?
Thanks for the help
EDIT: The reason I want to do this is because I have some session variables that I want to influence the way the PHP script acts. If the session variables are not set, I need it to redirect to a login page. I know I can just write the entire thing in PHP, but I'd like to do it this way if possible. If it's not possible to change header information from an included PHP file from SSI, then I'll just do it entirely in PHP.
it's impossible
you don't need that.
just address tour script that set session variables directly, not through ssi
MAYBE (with capital letters Lol), you can pull this off if you call that script in an IFRAME and that IFRAME outputs some JScript like window.parent.location = <some_url_here> forcing its parent to change its location... Its just fast-thinking from my part, I might be wrong with IFRAMEs' parent-child relation to the original document, as I haven't tested the "idea" myself :)
If your req.php returns the following html code, the redirect will happen:
<html><head>
<title>HTTP 301 This page has been moved</title>
<meta http-equiv="Refresh" content="0;URL=https://www.example.com/index.php">
</head>
<body></body></html>
But: "Google Warning: Using The Meta Refresh Tag Is Bad Practice"

PHP Change Page?

I have found out the bad news about header(), so I am no longer using it, because now my website doesn't work..
Is there any other simple way to change pages automatically?
you mean like redirect?
you can also use this:
<meta http-equiv="Refresh" content="5;
URL=http://www.yahoo.com">
Try javascript redirection
<script type='text/javascript'>
window.location = "new_page.php";
</script>
or you can use meta-refresh (many examples in google).
If you can't use header in your script, just add ob_start(); at the beginning and then you can use it AFTER html.
Ultimately you'd do well to re-work your code so you do a redirection before sending a bunch of HTML that's never going to be seen. And if you do that - you can use header(), which causes less delay for your end users, reduces processing all round, and is search-engine friendly! Win-win-win.

Meta page refresh not working for php page?

i am trying to redirect users without javascript enabled to our help page (help.php), specifically the part that talks about enabling javascript (help.php#nojavascript).
however, my meta refresh is not working! it keeps on refreshing the same page! i tried redirecting to a different page with a .html extension instead, and that works, so why doesn't this work?
...
</script>
<!-- if user doesn't have JS enabled, take them to help page -->
<noscript>
<meta http-equiv="Refresh" content="3;url=help.php" />
</noscript>
</head>
...
Try to use an absolute path:
<META HTTP-EQUIV="Refresh" CONTENT="3;URL=http://www.some.org/some.html">
For one thing, try taking it out of the NOSCRIPT element. Browsers with scripting enabled are supposed to ignore everything inside NOSCRIPT.
i was including a file that redirected the user to home.php =( sorry!

<noscript> redirection

I want to redirect the user to a certain page if he/she has javascript disabled. I tried this code:
<noscript><?php url::redirect('controller/method'); ?></noscript>
// url::redirect is much like the location header
to no avail...
How do I do this?
Since the headers have already been sent, you'll need to use standard HTML markup:
<noscript>
<meta http-equiv="refresh" content="0;url=noscript.html">
</noscript>
Trying this on both Firefox and IE seems to work well... With JavaScript enabled, the <meta> tag is ignored. When it is disabled, the browser redirects to noscript.html.
There is no way to do a redirect based on if javascript is disabled. Why not do the opposite - redirect if javascript is enabled?
<script>
window.location = "...";
</script>
You can't use meta refresh tag then remove it using Javascript because the browser is set to redirect upon reading the meta refresh tag and it's too late for JS to manipulate it.
The only way is to either do what Daniel suggested, or to show up a link when there's no Javascript:
<noscript>
Click here to continue
</noscript>
Or you can try to fail gracefully: Do you plan for javascript being off?
Add this as the first element in your body, style it to suit, and perhaps offer a link inside of it to a noscript page:
<div onload="return false;">
<!-- PAGE CONTENTS -->
</div><noscript>JAVASCRIPT IS REQUIRED TO VIEW THIS PAGE</noscript>
I use a more verbose method of this (that basically makes it appear as a modal dialog and all pretty) on pages where a client does not allow me to use more compatable means. I've tested in IE6+ and all other major browsers with great success.

Categories