-1

I want to redirect to a specific url after saving data.

I used:

header('Location: https://linkbook.co/index.php/site/saveUrl');

and it did not worked.

Why?

The entire code:

public function actionSaveUrl(){
            $criteria = new CDbCriteria();
            $criteria->limit = 1;
            $criteria->order = 'id asc';
            $model_url = Url::model()->FindByAttributes(array('saved_to_file'=>0,'status'=>1),$criteria);
            if($model_url){
                echo $model_url->link;
                CurlTool::downloadFile($model_url->link,'url_' . $model_url->id . '.html');
                $model_url->saved_to_file = 1;
                if( $model_url->validate() && $model_url->save() ){
                    echo 1;
                    header('Location: https://linkbook.co/index.php/site/saveUrl');
                    exit();
                }
                else{
                    echo 2;
                }
            }
        }
Ionut Flavius Pogacian
  • 4,750
  • 14
  • 58
  • 100

4 Answers4

8

Here's your problem:

echo 1;

You can't send headers after you've already sent output to the browser (unless you are buffering the output). You should have gotten a warning message from PHP when you did this.

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
3

There cannot be any output before the header, output can be:

  • Unintentional:
    • Whitespace before
    • UTF-8 Byte Order Mark
    • Previous error messages or notices
  • Intentional:
    • print, echo and other functions producing output (like var_dump)
    • Raw areas before

For more info take a look at this answer!

Also in the future it could be helpful if you enable errors in php, that way you will get an error telling you where other output was generated. You can enable the errors by putting this on top of your php page:

<?php 
ini_set('display_errors', 'On'); 
error_reporting(E_ALL); 
?>
Community
  • 1
  • 1
Daanvn
  • 1,254
  • 6
  • 27
  • 42
2

The manual explicitly says:

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.

You do:

 // <-- THIS LINE OUTPUTS SOMETHING
 echo $model_url->link;
 ...
 echo 1; // <-- THIS LINE OUTPUTS SOMETHING AS WELL
 // SO THIS WON'T WORK
 header('Location: https://linkbook.co/index.php/site/saveUrl');

So: scan your program for any program line that may output something, and move your header() call before that line.

fvu
  • 32,488
  • 6
  • 61
  • 79
-1

First off, you shouldn't have any output before redirection (what is echo 1; for?)

Second, in Yii, you can redirect like this:

$this->redirect('https://linkbook.co/index.php/site/saveUrl');
casraf
  • 21,085
  • 9
  • 56
  • 91
  • 1
    There is no mentioning of yii anywhere in the question - where did you get that from? – Aleks G Jun 25 '13 at 13:33
  • 1
    I can see the structure of his code, I work with Yii myself and I recognized it. Is this really a reason to downvote? Like all others I also mentioned the fact he's showing output before changing headers :/ – casraf Jun 25 '13 at 13:34
  • @ChenAsraf no one here listens especially if they have high rep. – DevZer0 Jun 25 '13 at 13:38
  • :( I got downvoted for nothing – casraf Jun 25 '13 at 13:43