Re: [PHP] Sessions in object oriented code
- From: yeti@xxxxxxxxxx (Yeti)
- Date: Thu, 30 Oct 2008 16:12:55 -0800
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
.
- Follow-Ups:
- Re: [PHP] Sessions in object oriented code
- From: "Ben Stones"
- Re: [PHP] Sessions in object oriented code
- References:
- Sessions in object oriented code
- From: "Ben Stones"
- Sessions in object oriented code
- Prev by Date: Suggestions for ECommerce?
- Next by Date: Re: [PHP] Sessions in object oriented code
- Previous by thread: Sessions in object oriented code
- Next by thread: Re: [PHP] Sessions in object oriented code
- Index(es):
Relevant Pages
|