View Full Version : php error?
ajfmrf
08-28-2012, 03:40 AM
I have this page here:
http://www.web-user.info/enlighten/index.htm
And the guestbook I am trying to add to this page is located here:
http://www.web-user.info/enlighten/comments/guestbooka.php
When I add this:
<?php include("comments/guestbooka.php"); ?>
to put the guestbook on that page I get an error:
Warning: session_start() [function.session-start]: Cannot send
session cache limiter - headers already sent
(output started at xxxxx/xxx/xxxx/xxxxxxx/index.php:120)
in xxxx/xxxxx/xxxxx/xxxxx/comments/guestbooka.php on line 1
I don't understand what the error means.
This is the page with the two together:
http://www.web-user.info/enlighten/index.php
and that page is also here:
http://www.web-user.info/enlighten/index.txt
as a text file.
Can someone explain what I messed up on?
If it would be easier I can post the code,instead of the text file
djr33
08-28-2012, 04:16 AM
To use sessions, you must begin with session_start().
session_start() uses a header to tell the browser what to do (an instruction, not content). (This is because sessions use cookies.)
Headers MUST be sent before any content is sent. The server tells the browser important information like the kind of document, its size and what to do with it, but that also includes things like cookies (and redirects, etc.). Just based on how the web words, that MUST happen before any content is sent.
The basic rule is the following, if you are using sessions:
Including the following line before ANYTHING ELSE*, and you won't have problems:
<?php session_start(); ?>
(*Even a single blank line can cause problems, or a space.)
However, in your case you are trying to use a full script rather than just start sessions. I assume the script is well written and usable (you shouldn't have to change it, at least not much), but it could be a little tricky.
There are three ways to try to solve this problem, ranked in order of difficulty:
1. Just place the include() statement before any content on your page, like the session_start() example above. Is this possible? Is there a reason that you want to include it later? Can you include it at the start, but wait until later to use its content? Depending on how the script/API works, this might be easy or difficult. (A well designed script should make this easy, but that's not guaranteed.)
2. Edit the script. Remove session_start() from it, and add the line I showed you above to the beginning of YOUR page. This is very easy. The potential problems are:
--finding session_start() and successfully removing it without causing problems. (Note: there's no problem at all if you start sessions early. Nothing to worry about.)
--it won't solve anything if there are OTHER headers sent in that script-- that would make things more difficult for you.
--just be careful when you edit that script (and save a backup).
3. If all else fails and you can't find a way to modify the script to work, or change how you include it, then you will have to forcefully avoid having any content before the session_start() and other headers. The way to do this is using output buffers. See this information at PHP.net:
http://php.net/manual/en/book.outcontrol.php
Follow the information there. At worst, you could set ob_start() at the very beginning of your entire script and then finally output everything at the end. That would immediately solve all of your problems-- process everything entirely, setting aside the content in a temporary variable, then sent it out at the end. But:
--this is generally considered bad coding. It's a last resort and usually means you're doing something wrong if you need this. (When dealing with other people's scripts, though, it does come up-- either rewrite everything or just use an output buffer-- it's not a horrible thing to do, just not preferred.)
--if there is a lot of content on the page (to store in the output buffer) or the page is otherwise intense for your server to process, or if you just have a lot of visitors, this could eventually be problematic (too much work) for your server. Avoid it if you can, but if you need it on some smaller pages it's not the worst idea.
Well, that's where to start. Does it help?
techzium
08-28-2012, 04:50 AM
In file index.php Header() function called already!check index.php,is any where called Header function or any redirect php codes there?
ajfmrf
08-28-2012, 12:01 PM
Hi Daniel,that worked as far as letting the form show correctly.How ever I still do not get any of the previous comments/posts to show.I have no clue as to where to start looking at getting that to work.
If you look at the guestbook/comments page itself they are below the form.But none of that shows when the file is added by the include function?
I am at a lose as to why it does not show the whole page???
djr33
08-29-2012, 02:18 AM
Hm, I don't know. It would require getting into the inner workings of that script (or just finding a helpful tutorial that has the answer).
Sometimes the most important part of finding a script is finding one that's easy for you to work with and integrate into your site.
I could debug this probably, but it would mean looking at the code for the script and if there's a lot there it might take a lot of time so I don't know if I can do that for you. My immediate guess is that you're not calling the function in the right place, so you'd need to look through that to figure out where you call the function that generates the posts. But that could be anywhere, or it could be something else going on. If you take the time to study the PHP in it you'll probably eventually understand it, but I don't know if that much time is worthwhile for you-- remember there are also many other comment scripts out there.
ajfmrf
08-30-2012, 01:13 AM
Well, I got it to show and it looks good.The only problem I have is that when you click on the send button it sends you to the guestbook file after it adds the new comment.
I would like it to stay on the index.php instead.
I moved the files for the guestbook to the same directory of the enlighten page to make it easier.
How do I get it do the form and stay on the index.php when the send button is clicked on?
djr33
08-30-2012, 01:42 AM
That I don't know. You'd need to move whatever code receives/saves the data to that page and then set the action of the form to the same page. (It's easy to change the action, but of course then it won't work at all-- moving the code would fix that, but it might be complicated.
A slightly easier approach might be adding in a redirect on the page that receives the form submission.
ajfmrf
08-31-2012, 04:48 PM
I guess I will have to try a redirect to keep it there-
Thanks again Daniel
ajfmrf
08-31-2012, 05:02 PM
I guess I will have to try a redirect to keep it there-
Thanks again Daniel
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.