CodeIgniter Forums
Simple tutorial problem: redirect problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Simple tutorial problem: redirect problem (/showthread.php?tid=23332)

Pages: 1 2


Simple tutorial problem: redirect problem - El Forum - 10-07-2009

[eluser]markowe[/eluser]
Just plodding through the "Weblog in 20 minutes" tutorial to try to get into CI - about time I used a framework instead of the mess I usually make.

However I have got well and truly stuck with the redirect Derek inserts into the "comment_insert" function, which is supposed to redirect the browser to the page for the particular comment after inserting the new comment POST data into the database.

Code:
function comment_insert()
   {
            $this->db->insert('comments', $_POST);
            redirect('blog/comments/' . $_POST['entry_id']);
   }




The insert works fine, but the redirect causes an error:

Quote:A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home1/itsgotta/public_html/citest/system/application/controllers/blog.php:1)

Filename: helpers/url_helper.php

Line Number: 541

...and I can't for the life of me figure out what the problem is. I was hoping to get a little further than this today Smile Any thoughts?


Simple tutorial problem: redirect problem - El Forum - 10-07-2009

[eluser]jedd[/eluser]
Hi markowe and welcome to the CI forums.

[quote author="markowe" date="1254939986"]
Message: Cannot modify header information - headers already sent by ...
[/quote]

About 99% of the time, headers already sent comes down to one of these things:

o Closing ?> tag with new lines after them. Solution - don't use closing ?> tags anywhere, ever,*
o Whitespace before a <?php opening tag at the start of one of your files,
o echo (print_r, var_dump, etc) in your non-view files.

* Note that this obviously doesn't apply to views, but rather helpers, libraries, models, controllers and so on.


Simple tutorial problem: redirect problem - El Forum - 10-07-2009

[eluser]markowe[/eluser]
Thanks for the quick reply. To be honest I had already eliminated the possibility of a mistake with closing tags/white space as I am only too painfully aware of that issue.

As for echoing something, well, I am just following the tutorial to the letter! The function I quoted above is executed as-is, just those two lines, there's nothing being echoed there before the redirect. So I am still stumped...


Simple tutorial problem: redirect problem - El Forum - 10-07-2009

[eluser]jedd[/eluser]
As you could imagine, most people assert that there's nothing wrong with their code, and they've checked for blank lines, extra spaces, etc .. and a few days later realise (and occasionally reveal) that they missed one.

So I hope you won't be mortally offended by any residual doubt at this end. If you could chuck the contents of your demo directory up to github, it'd be easy for someone to scan through it.


Simple tutorial problem: redirect problem - El Forum - 10-07-2009

[eluser]markowe[/eluser]
I know, I still remain open to the possibility, however slim, that I may have screwed up somewhere Smile

I will have another tinker and then upload if I can't fix it. To be honest, I wouldn't do this with a redirect normally anyway, I'd just say:

Code:
if ( $_POST ) ...

...i.e. have the same function handle the logic, whether there's been a POST or not, rather than redirect to another function if there's been a POST, not mad keen on redirects anyway. Bbut I was just trying to "think CI" - maybe there's no intrinsic need to use a redirect? (though I might need to do one at some point!)

Thanks.


Simple tutorial problem: redirect problem - El Forum - 10-07-2009

[eluser]markowe[/eluser]
Phew, fixed! I wouldn't have managed it without this thread (sorry, I did search the forum beforehand, but didn't hit on that thread) - seems it's an encoding issue. I use a free editor under Windows called RJ TextEd, which is great, but I had saved the file with a scheme just labelled UTF-8, rather than another one on offer labelled "UTF-8 (without signature)" (whatever that means). Presumably that fixed a line feed problem similar to what the user above experienced, though it's strange, I've been using RJ for PHP projects for ages and never had that problem... Anyway, hope that'll help someone else too.

Thanks for the help. Now I can move on to more advanced stuff! (How Code Igniter can Make Tea for you, or something Smile )


Simple tutorial problem: redirect problem - El Forum - 10-07-2009

[eluser]jedd[/eluser]
As an aside, you may want to use the $this->input->post() functions, rather than accessing $_POST directly. These are covered in the Input & Security Class page of the manual.


Simple tutorial problem: redirect problem - El Forum - 10-07-2009

[eluser]markowe[/eluser]
[quote author="jedd" date="1254955331"]As an aside, you may want to use the $this->input->post() functions, rather than accessing $_POST directly. These are covered in the Input & Security Class page of the manual.[/quote]

Thanks for that heads-up. Maybe this tutorial is a little old, I am surprised it doesn't mention that.


Simple tutorial problem: redirect problem - El Forum - 12-10-2009

[eluser]Unknown[/eluser]
to Jedd : sorry I'm a new study with CI, I have problem like in the top I have an error like

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home1/itsgotta/public_html/citest/system/application/controllers/blog.php:1)

Filename: helpers/url_helper.php

Line Number: 541

but I not understand as your answer for markowe! maybe you give me example please!thanks before that.


Simple tutorial problem: redirect problem - El Forum - 12-11-2009

[eluser]markowe[/eluser]
Michel, there are actually maaaany reasons why this error would appear and it is not specifically a CI problem, it just happens to occur perhaps more often due to the complexity of CI - I was having a problem with the code in that tutorial I mentioned so was able to be a BIT more specific, but it would be impossible to help you just from this error. In my limited experience, the "Cannot modify header information" error can appear due to a completely unrelated error happening before that and causing this error as a side-effect, though you should see that other error warning too in that case.

Otherwise, as you will find in MANY other explanations around the net, it often happen when you close your PHP code with "?>" an extra space is accidentally added AFTER the ">" - you should check your files to make sure you have deleted any such spaces.

Also, as I mentioned in my own solution, try saving your PHP files with a different encoding scheme - make sure it's UTF-8, and try variations of this if there are any.

The "header" error IS a confusing one, especially for people starting in PHP, as it doesn't usually explain the real nature of the error. The "actual" meaning of the error is that you have tried to change http header information AFTER already sending information to the browser. That may mean nothing to you, as to anyone finding their feet in web programming, but it means you SHOULD also check that you haven't done that - in particular, check that you haven't tried to ECHO something before doing a redirect, as that is not allowed but is quite a common mistake. As I said, in my experience, this is usually actually the LEAST likely cause..!