Welcome Guest, Not a member yet? Register   Sign In
Warning show up when using redirect($url, 'refresh');
#1

[eluser]sangprabo[/eluser]
I have modified the url_helper so MY_url_helper looks like this.
Code:
function redirect($uri = '', $method = 'location', $http_response_code = 302)
    {
        if ( ! preg_match('#^https?://#i', $uri))
        {
            $uri = site_url($uri);
        }

        switch($method)
        {
            case 'refresh'    : header("Refresh:5;url=".$uri);
                break;
            default            : header("Location: ".$uri, TRUE, $http_response_code);
                break;
        }
        exit;
    }

To use it

Code:
echo $this->parser->parse('static_page/success_message', $data, TRUE);
  redirect($redirect_url, 'refresh');

I'm using it on my local machine. Nothing is wrong. The message is displayed correctly. And the page will be redirected within 5 seconds.

The problem is, when I upload my application to the server, it shows warning and the page is not redirected.
Code:
Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /var/www/controllers/page.php:62)

Filename: helpers/MY_url_helper.php

Line Number: 29

My temporary solution is to create a meta tag
Code:
<meta http-equiv="Refresh" content="5; url={meta_redirect_url}" />

Do you guys have any better idea? Why can't I use the 'refresh' after echoing some lines of message? Thank you.
#2

[eluser]Vlad Balmos[/eluser]
You are not allowed to output anything to the browser before sending http headers. As to why it works on your local machine i don't really know. It shouldn't work Smile.
If you really need to refresh the page after outputing a message, you could try a redirect using javascript
Code:
[removed]
   var newUrl = '<?php echo $newUrl;?>';
   setTimeout(function() {
      [removed].href = newUrl;
   }, 5);
[removed]

Or something like that Smile
#3

[eluser]sangprabo[/eluser]
Thanks for your reply. I'm not using the redirect($url), but redirect($url, 'refresh'); Is it not allowed too?

It's like when you perform a log in action in this forum, the message will flash for 1 second. But the code is written in meta tag.

Sorry, I can't read your script code.
#4

[eluser]Vlad Balmos[/eluser]
It doesn't matter. You are making a header request. The following is a quote from the php manual:

"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."

The code i posted was escaped to prevent xss. It's plain javascript code. Search for [removed] and setTimeout function in javascript. You'll get the ideea.
#5

[eluser]Vlad Balmos[/eluser]
[removed] = window . l o c a t i o n
#6

[eluser]sangprabo[/eluser]
Okay, noted. But why is redirect($url, 'refresh') working flawlessly on my local machine?

Your script code is good for those who activate javascript on their browser, but I prefer the meta tag.
#7

[eluser]Vlad Balmos[/eluser]
Ok, so i did some testing on my machine and got the same results as you. It works on your machine because you must have output buffering enabled in you php configuration. Open php.ini on your local server and search for output_buffering. If it's != off then for sure output buffering is enabled. Thus, even thou you echo the message before sending the redirect header, the message gets buffered and it's actually sent to the browser after the redirect header
Try this on your local machine, i'll bet it will give the same error as the production server:

Code:
echo $this->parser->parse('static_page/success_message', $data, TRUE);
@flush();
@ob_flush();
redirect($redirect_url, 'refresh');

The solution is simple: echo the message after you have sent the refresh header, just reverse the order of those 2 statements:
Code:
redirect($redirect_url, 'refresh');
echo $this->parser->parse('static_page/success_message', $data, TRUE);

it should work.
#8

[eluser]sangprabo[/eluser]
Thanks for your fast reply. I will try it soon on my server.




Theme © iAndrew 2016 - Forum software by © MyBB