Re: Sessions in object oriented code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Fri, 2008-10-31 at 00:33 +0000, Ben Stones wrote:
> Hi,
> 
> I can't really understand that. Not sure if you understand my problem
> properly (if I've not explained properly). Anyone can give me some solutions
> please?
> 
> Thanks.
> 
> 2008/10/31 Yeti <yeti@xxxxxxxxxx>
> 
> > OK I guess it's somehow like this ..
> >
> > <form>
> > <?php
> > if (isset($_POST['submit'])) {
> > include('sessions.php');
> > // include sessions.php
> > }
> > ?>
> > <!-- form innerhtml -->
> > </form>
> >
> > now this of course is something very bad to do and it wont work.
> > One way to prevent markup from being outputted is using ob_buffer() [1]
> >
> > EXAMPLE:
> > <?php
> > $form = <<<FORM
> > <form>
> > <!-- form inner xml -->
> > </form>
> > FORM;
> > ob_start();
> > echo $form;
> > $output_buffer = ob_get_contents();
> > ob_end_clean();
> > var_dump(nl2br(htmlentities($output_buffer)));
> > ?>
> >
> > So what we do here is simply start the output buffer befor echoing $form.
> > ob_get_contents() returns the outputbuffer as it is right now.
> > By calling ob_end_clean() buffering is stopped and the buffer cache
> > released.
> > Still keep in mind that headers will still be sent when buffering the
> > output.
> >
> > here is a more complex
> > EXAMPLE:
> > <?php
> > ob_start(); // starting the output buffer
> > ?>
> > <html>
> >        <body>
> >                <!-- inner xml -->
> >        {{replace_me}}
> >    </body>
> > </html>
> > <?php
> > $output_buffer = ob_get_contents();
> > ob_end_clean();
> > session_start();
> > $_SESSION['test'] = time();
> > echo str_replace('{{replace_me}}', '<p>This is the replaced string.<br
> > />SESSION[test] was set to: '.$_SESSION['test'].'</p>',
> > $output_buffer);
> > ?>
> >
> > Now we start the output buffer at the beginning of the script and the
> > session at the end.
> > It does not matter whether we close the PHP tag after starting the
> > ob_buffer. ( like with ?> )
> > As long as we do not flush_end or clean_end the output buffering
> > process it will continue caching the output (except headers).
> > So session_start should work after actually "outputting" markup.
> >
> > Another method could be like we did above the str_replace() [2] ...
> >
> > EXAMPLE:
> > <?php
> > $some_number = time();
> > $html = <<<HTML
> > <html>
> >        <body>
> >                <p>Time: $some_number</p>
> >                <p>{{replace_me}}</p>
> >        </body>
> > </html>
> > HTML;
> > echo str_replace('{{replace_me}}', 'This string was changed by PHP',
> > $html);
> > ?>
> >
> > There is still plenty of other possible solutions. Keep on rocking
> >
> > [1] http://in.php.net/manual/en/ref.outcontrol.php
> > [2] http://in.php.net/manual/en/function.str-replace.php
> >
> > //A yeti
> >

How are you currently including the external file that has the
session_start() call? What I always do is have a basic config include
that contains only code that should have no output, like config
variables, database connections, and the session initiation. As sessions
rely (in general but not always) on cookies, then you should be calling
them both at once, as the only way to create a cookie after the headers
have been sent is with the use of javascript, which shouldn't be relied
on for something as fundamental as what you are trying to do.

As long as you have no output prior to the session_start() call (and
that means not even a single space) then you should be fine, no matter
whether the call is made from an include file or not.

If this still is no help, maybe you can give us a code excerpt so that
we can see what is the problem?


Ash
www.ashleysheridan.co.uk


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux