auto refresh page with different URL - php

I am using following code for a refreshing page, it is not reloading on completion. The following code is not working sometime.
$url = array("www.yahoo.com","www.gmail.com");
$go=$url[0];
header("Refresh: 10; URL=$go");

"www.yahoo.com" may work in most browsers, but it's not a proper URL without the scheme.
Try http://www.yahoo.com/ etc. instead.
Additionally, redirects via header() are usually done with a Location: header, not a non-standard Refresh:. A refresh is best supported as a meta tag, not a header.

Related

window.location (JS) vs header() (PHP) for redirection

using JS : (in <head> tag)
<script>window.location="https://stackoverflow.com";</script>
using PHP : (in <head> tag)
header('Location: https://stackoverflow.com');
end();
Which one I should use ? or another ?
and what about using <meta>?
<meta http-equiv="refresh" content="0;url=https://stackoverflow.com"/>
Many good answers , I don't know which answer I will accept, Thanks so much
The result is same for all options. Redirect.
<meta> in HTML:
Show content of your site, and next redirect user after a few (or 0) seconds.
Don't need JavaScript enabled.
Don't need PHP.
window.location in JS:
Javascript enabled needed.
Don't need PHP.
Show content of your site, and next redirect user after a few (or 0) seconds.
Redirect can be dependent on any conditions if (1 === 1) { window.location.href = 'http://example.com'; }.
header('Location:') in PHP:
Don't need JavaScript enabled.
PHP needed.
Redirect will be executed first, user never see what is after. header() must be the first command in php script, before output any other. If you try output some before header, will receive an Warning: Cannot modify header information - headers already sent
A better way to set the location in JS is via:
window.location.href = 'https://stackoverflow.com';
Whether to use PHP or JS to manage the redirection depends on what your code is doing and how. But if you're in a position to use PHP; that is, if you're going to be using PHP to send some JS code back to the browser that simply tells the browser to go somewhere else, then logic suggests that you should cut out the middle man and tell the browser directly via PHP.
It depends on how and when you want to redirect the user to another page.
If you want to instantly redirect a user to another page without him seeing anything of a site in between, you should use the PHP header redirect method.
If you have a Javascript and some action of the user has to result in him entering another page, that is when you should use window.location.
The meta tag refresh is often used on download sites whenever you see these "Your download should start automatically" messages. You can let the user load a page, wait for a certain amount of time, then redirect him (e.g. to a to-be-downloaded file) without Javascript.
PHP redirects are better if you can as with the JavaScript one you're causing the client to load the page before the redirect, whereas with the PHP one it sends the proper header.
However the PHP shouldn't go in the <head>, it should go before any output is sent to the client, as to do otherwise will cause errors.
Using <meta> tags have the same issue as Javascript in causing the initial page to load before doing the redirect. Server-side redirects are almost always better, if you can use them.
The first case will fail when JS is off. It's also a little bit slower since JS must be parsed first (DOM must be loaded). However JS is safer since the destination doesn't know the referer and your redirect might be tracked (referers aren't reliable in general yet this is something).
You can also use meta refresh tag. It also requires DOM to be loaded.
window.location.href = 'url';
is beter than
header('location:url');
because the header command is mustly return an error "Warning: Cannot modify header information - headers already sent"
using js window.location.href = 'url';
this is beter

I can authorize users but cant redirect them to member page

I have designed a login page for my plugin it perfectly works and check the users credentials but when the user is authorized I cant move to the px_member234.php page.
the first page is px_myplugin234.php then it opens the px_login234.php file using include(px_login234.php) which is used for authentication, I suppose thats why the header does not work. is there any other option?
<form method="post" action="">
....
</form>
authorizer(){
........
if(user_is_authorized)
{
//go to member.php page
header ("Location:px_member234.php"); << does not work
}
}
I have also used the following but it does not work
echo '<script type="text/javascript">window.top.location="px_member234.php";</script>';
echo '<script type="text/javascript">window.location="px_member234.php";</script>';
This is because you're echoing HTML content before the header.
When PHP sees actual output, it has to flush all of it heads to apache (who sends them to the client). After PHP has sent the headers and started output, it is not possible to add more headers.
Your code needs to be refactored to call header('Location: ...') BEFORE outputting any content at all
You can use header redirection. To use this, you have to ensure, that nothing is outputed before the header command. If the header is already sent, this code wont work
header("Location:member.php");
In your example, the auth code have to be on the top of the page. If you can't prevent the output before your header function call, you could still add a meta redirect header to the html page or use JavaScript to set a new window.location
Besides calling the header() function before any output like others mentioned to the page you should also leave a space between Location: and the URL.
What you have:
header("Location:member.php");
What will work:
header("Location: member.php");
Update
Try and include the full URI instead of your relative one as per HTTP/1.1 Header Field Definition.
From php.net:
HTTP/1.1 requires an absolute URI as argument to ยป Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself.
To your form markup, you can add
<form method="post" action="?noheader=true">.
The ?noheader=true will stop Wordpress from outputting any headers. You can see details here: http://www.lessthanweb.com/blog/wordpress-and-wp_redirect-function-problem
Also, in wordpress development, wp_redirect is preferred over the php header function because it includes hooks that let it play nicely with other plugins.
Note that it takes the absolute url of where you want to redirect the user. Since you're making a plugin, you can use get_option('siteurl') to get the root url so:
<?php
wp_redirect(get_option('siteurl').'px_member234.php');
exit;
?>

php scriptable web browser

I want a url redirect tracer function in the php such as http://www.wheregoes.com/ .
I want to detect four kinds of redirects:
Http 301 redirect
Http 302 redirect
meta tag redirect
javascript redirect
If i use curl, i can easily detect 301, 302 redirect, but it is difficult to detect the other two redirections.
So i want a scriptable web browser, i will use a library as below:
$browser = Browser::createBrowser('chrome');
$delay = 10; // (This is a important parameter for detecting javascript or meta tag redirection).
$browser->load($url, $delay, function onLoadComplete($arr_track_url){
print_r($arr_track_url);
});
I searched and ran into some libraries such as http://www.simpletest.org/en/browser_documentation.html, but they don't support javascript and meta tag redirect.
Is there any php scriptable browser? Or can i detect javascript or meta tag redirection easily?
If I get that right you want to find out where some link finally leads to, if that final url differs from the url actually clicked in the first place?
If so I think the best approach is to let the browser do its work and loko afterwards where it came out. This way you get exactly the 'normal' behaviour of a browser, not that of some library.
Use a (hidden) iframe where you load the url into. Then wait for a window.load event or something and query the documents location afterwards. I didn't try that now, but sounds doable to me...

How do you keep a PHP link in the header while it loads another page?

If you go here: https://web.archive.org/web/20120903235224/http://getriotpointscodes.com/ and look at the PHP coded link on the right hand side (hxxp://getriotpointscodes.com/index.php?id=35863).
How do you mimic the behavior where you copy the link into the browser while it loads another page while keeping the original clicked link (hxxp://getriotpointscodes.com/index.php?id=35863) as the header?
This page is already exhibiting this behavior, but I wasn't sure how to exactly code it for my own use.
I had something like this, but it's probably wrong:
<?php
// referral.php
$id = (isset($_GET["id"])) ? strval($_GET["id"]) : "1";
// lookup
$url[1] = 'hxxp://getriotpointscodes.com/index.php?id=23121';
header('Content-Type: text/plain');
include("index.php");
?>
The only way to achieve this in all browsers is by using a single-frame frameset.
However, frames are deprecated and the behaviour you mention is a very bad thing - for example, it prevents the user from properly bookmarking a page
If both links are on the same domain you could also use the HTML5 history.pushState JavaScript API to modify the displayed URL.

more than one header in chrome not working - php

Im using PHP to track the clicks of all mailto links by rewriting the mailto: to my script and then setting the header of the referring page.
Initially I just had:
header("location: mailto:email#address.com");
...but this has an undesirable effect in IE8: it opens 2 email windows. So, in my attempt to resolve that issue I am now using:
header("Status: 200");
header("location: http://mypage.com");
header("Refresh: 0; url=mailto:email#address.com");
This works fine in IE but not chrome. I threw the "status" in there hoping to solve the mystery.
Other than detecting the browser and issuing different commands, what else could one do?
A location header should be accompanied by a 30X status code (like 302), not 200.
Check this out > http://php.net/manual/en/function.header.php
Especially those two parts:
The second special case is the
"Location:" header. Not only does it
send this header back to the browser,
but it also returns a REDIRECT (302)
status code to the browser unless the
201 or a 3xx status code has already
been set.
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
You send 200 and 302 at same time, and also you didn't follow the exit; rule.
You can play with my suggestions (especially the exit; part).
!!! Note !!! There was a bug in the past that makes the Chrome not to work if header("Status: 200"); wasn't set first, but not sure if it's fixed yet.
it would probably be better to use some AJAX to handle your problem here, and if your going to use AJAX use JQuery its just easier
firstly though mailto is not a preferred method on the web any more its too clunky and relies upon the default email client of the user being set up which in most cases you cannot rely upon.
So to address that have a link that is styled to look like your button.
once you have this use JQuery to send an ajax request to a PHP script that performs the counting and then if you wish upon receipt handle the success with a redirect (I only include that because I am unsure what your redirect achieves).
Its clean quick and the user will not notice a difference apart from your site will probably experience a speed increase :) hope this helps
Header location redirects the browser, so the other headers are ignored. You should send this header always as the last one. Also it's not good idea to execute any PHP code after you send the redirect.
You probably want to do this:
On first page:
header("Status: 200");
header("location: http://mypage.com");
exit();
On the http://mypage.com:
header("Refresh: 0; url=mailto:email#address.com");
The weird thing about chrome: it accepts the following header refresh.
<meta http-equiv="refresh" content="4; url=page.php" />
<button value="go further">
I place a button below this refresh for the browsers who does not support any type of header refresh.

Categories