Re: [PHP] Sessions in object oriented code



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



Relevant Pages

  • Re: headers sent issue
    ... The script ... Here I deliberately and explicitly send HTML headers in such a way that I ... why would I program those things in PHP when HTML is made for presenting ...
    (comp.lang.php)
  • Re: How could this possibly happen (HttpURLConnection + simple JSP page)
    ... common practice in PHP development is to put the HTML ... >> back as soon as you start, and more importantly the headers do not ...
    (comp.lang.java.help)
  • RE: [PHP] Cannot send a hyperlink
    ... [PHP] Cannot send a hyperlink ... Why is php refusing to parse as html? ... something like PHPMailer. ... You don't need to add two carriage returns at the end of the headers - ...
    (php.general)
  • PEAR guru, please help with HTML email header
    ... I am a novice at PHP but so far I've really been enjoying it. ... I'm having a problem, however, with some HTML e-mails I'm sending using ... There's a possibility that headers inserted by his ISP's anti-spam ... require_once('PEAR.php'); // These two files are part of Pear, ...
    (alt.php)
  • Re: PEAR guru, please help with HTML email header
    ... > I am a novice at PHP but so far I've really been enjoying it. ... all the headers, the text version, and then all the HTML ... > have the headers set right in the PHP code. ...
    (alt.php)