1

I've built my own educational MVC framework to learn more about PHP OOP, which I certainly have, but for the moment I have gotten myself into a pickle. I need to use sessions throughout most of the project, but also need to stream a file to the user at a certain page.

As I call session_start() by default before getting into my controller, I get the infamous Headers already sent thrown in my face when I need to stream a file from inside the controller to the user. Pretty logical.

As I make the session modification inside the controllers, I need the session_start() to be called in beforehand, but at that time, the controller obviously isn't loaded and there's no way my framework could tell whether It should call the session_start() or not. Creating a file with a white list of controller names that doesn't need sessions seems quite primitive.

What would be an appropriate way to get rid of sessions when I need to stream a file?

4
  • 1
    Not sure I understand why you can't just do a session_start() in the first lines of every relevant file? Commented Feb 26, 2011 at 16:10
  • header_remove() might or might not work under mod_php. Commented Feb 26, 2011 at 16:11
  • @Pekka - I believe that it's better to make a workaround for what transcends from the regular, rather than the other way around. Don't you? :) Commented Feb 26, 2011 at 16:16
  • Maybe it's me, but I still don't understand. Do you have an overwhelming reason to start a session based on some conditions in one of your classes, as opposed to always starting it? Do you not want to start the session when you're streaming the file? If so, why? Commented Feb 26, 2011 at 16:19

4 Answers 4

2

Starting the session belongs in the bootstrap, not in an object IMO.

I would tend to always start in one of the first lines of the bootstrap, regardless whether it will be needed in the current script or not. Performance implications are likely to be non-existent or minimal.

Sign up to request clarification or add additional context in comments.

10 Comments

Hi, yep - that's what I do also. The session_start() is called in my bootstrap by default before the controller is called, hence this question on how I would know whether to call the session_start() or not
@Industrial I don't see why the question arises in the first place. Why would you want to start session_start inside your class at all if it's called by default?
@Pekka - A majority of the controllers affect and/or are dependent on the $_SESSION variable, so thereby I've made the session_start() to be called by default. What I want is a clean way to prevent the session_start()-call from being made on specific controllers
@Industrial I see, but what for? (No need to explain it if you don't feel like it, just being curious)
@Pekka From what I can tell, there's no other way to stream a file to the user (causing a download dialogue) without sending it as the first output to the user. As session_start() is called - headers have already been sent and headache occurs.
|
2

Use OOP. Your controllers must somehow extend a base AbstractController. So, add a virtual method "NeedsSession()" that returns true by default. Now, override this method for the controllers that don't need a session. It's cleaner this way.

Comments

1

When you send any output means you are sending the header as well if one of the your file contain any white space after the enclosing tag header will be sent to server . Check your files if they does this thing

session_start must be called before any header sent to server. There should not be any space in the files which are included in between double check your files if they contain any space at starting or the end

Comments

0

If all of your requests are leading to index.php file, that's where you should start your session (Headers already sent problem should disappear). Remember about UTF-8 encoding in your file (without BOM)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.