PHP - file_get_contents follow header refresh - php

I have a script that use the
header('Refresh: 5; url=http..');
die();
And i call this script with another php file that use the function "file_get_contents". Unfortunately it does not work.
With header location there aren't problems.
Any suggestions?
-- UPDATES --
I have followed the advice of Oscargeek.
I have updated the code with a print of HTML that contains meta-refresh.
The script that call this url, is a "system" of cron, and make this call in a foreach. So i think it can't work.
I have changed this call with a cron and wget, but the result is the same.
Other suggestion ?

When you are doing a file_get_contents, you get the HTML but not the headers of the first page.
file_get_contents only return a string without headers, header location it's working because are doing the redirection before return this string.
Try to do the redirect from the HTML, in your first page write this content:
<html>
<head>
<meta http-equiv="refresh" content="5; url=http://google.com" />
</head>
</html>
In the PHP that you are calling, you should only print this content without other data and the refresh will do.

Okay, first of all, I'm wondering why you use file_get_contents to include a PHP-File. I'd use include or require.
For your problem some additional information:
The problem is: None of those headers ever was running in your other script. So this means IF they are send, they got send by the file you were trying to read - but: Since the script didn't got send via HTTP-Protocol, it doesn't send.
If you want to use it like this, I'd advice you to use HTML-Refresh like Oscargeek stated, or use Include / Require, if you want to keep PHP-Code.

Related

Show a loading message while php is processing followed by a redirect to another page

I am trying to write a page in php which shows a loading message while it does some processing and then auto redirects to another page
<?php
//show a loading message - this is the bit I need help with
// do some processing - don't need help with this bit
header("Location: http://www.mysite.com/mynextpage.php");
exit;
?>
I can't use echo or javascript otherwise I get a "Cannot modify header information - headers already sent" error when the page executes.
Any clues?
First of all, you mustn't use any header change after outputing some data, that is why you get the error above.
Another way, use header redirections by refreshing page on next page:
<?php
header('Refresh: 5; url=http://www.mysite.com/mynextpage.php' );
echo 'Wait 5 sec then redirected';
Note:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Best way is to use ajax requests. Via javascript you should show the loading element, perform the request, on success redirect on target page
Using java script to redirect after the processing has finished seems to be the way to go.
I'm using
<script type="text/javascript">
window.location="http://www.mysite.com/mynextpage.php";
</script>
at the end of the page and it's working

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

Passing parameters from one PHP to WEB SITE

I am setting a parameters in my PHP page and need to pass it to another WEB SITE, to execute operation on the WEB SITE (i don't need to get re result). I use header ("location : https://localhost/test=123") but its not executing it and display:
Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/script1.php:33) in /Applications/XAMPP/xamppfiles/htdocs/script1.php on line 106
How can i simply just invoke it one time?
if there is solution from bash file,also can help...
The header function can only be used before ANY output is displayed on the screen.
For example:
<?php
echo( 'test' );
header('location: www.google.com');
?>
Would fail with that error you reported.
You already output content to use header().
I would recommend:
$sRemotePage = file_get_contents('https://localhost/test=123');
More here: http://php.net/manual/en/function.file-get-contents.php
You can use the HTTP meta element to set where the browser redirects the user. It needs to look like this:
<meta HTTP-EQUIV="Refresh" content="0; url=http://localhost/test=123">
Where 0 is the number of seconds the browser will wait before redirecting, and url is the redirected URL.
The other option would be using Output Buffering, but I am not entirely sure what you want to do and whether it can include ob_ or not.

How to go to new Html page from PHP script?

After the php script is over, how do I go to another html page?
Why would you want to do that? When the page is over (which I understand as the script ended execution), it is usually sent to the client and then it can be viewed by the user. So, basically, what you are trying to do is to redirect the user after the script stopped executing.
If so, you have two solutions available:
Do not output anything, and after your script stopped executing, use the header() PHP function:
header('Location: http://example.com/some/url');
where you should replace the example URL with your own.
If you are outputting the HTML page and sending it gradually to the user, you can just put JavaScript redirection script at the end of the page (so after everything has been sent):
<script>
window.location = 'http://example.com/some/url';
</script>
Does any of these solutions work for you?
header('Location: /page.html');
Make sure you don't output anything else, then simply
header('Location: http://www.example.com/');

PHP redirect AFTER sending headers?

I have a function which needs to redirect the page if a variable is set etc...
The problem is, this function is at the bottom of the php page.
This means, I have outputted alot of information, so I get a headers warning.
"Warning - Headers already sent by ..."
Is there any way to redirect after headers are sent?
Thanks
What you should do is put ob_start() at the very beginning of your page, and ob_flush() at the very end. This way you don't run into headers already sent errors.
See those functions for further reference.
There are ways, but they're basically workarounds:
The simplest one is the meta http-equiv:
<meta http-equiv="Refresh" content="0;http://www.example.com/newlocation">
some browsers will not like this when it's outside of the <head> element, and drop into quirks mode
Or, you can try the JavaScript redirect:
<script>
window.location = 'http://www.example.com/newlocation';
</script>
which obviously won't work without JavaScript.

Categories