|
Posted by Jerry Stuckle on 12/05/06 23:00
Sanders Kaufman wrote:
> Jerry Stuckle wrote:
>
>> Sanders Kaufman wrote:
>
>
>>> It *resumes* a session?!
>>> If I put a bunch of values into session variables, how do I get those
>>> back with the resumed session... and how do I make sure I resumed the
>>> right session?
>>
>>
>> Just call session_start() on every page that uses sessions. PHP
>> ensures the correct session id is used again.
>
>
> So with this page:
>
> -----
> session_start();
> $_SESSION["MyVar"] = 123;
> ----
>
> When the user reloads, MyVar *will* equal 123?
> It's that simple?
>
It is if you have register_globals on - but that's a very bad thing to
have - a potential security risk.
What it will do is set $_SESSION('MyVar'] equal to 123. To get the
value out of the session on another page, just do:
session_start();
$MyVar = $_SESSION["MyVar"];
or, a better way (in case the user got to the second page without going
through the first one)
session_start();
$MyVar = isset($_SESSION['MyVar']) ? $_SESSION['MyVar'] : 0;
If $_SESSION['MyVar'] is set, the value in it will be placed in $MyVar.
But if $_SESSION['MyVar'] is not set, the code will set $MyVar to 0
(adjust the default value as you wish - even null is ok).
> When I was trying to learn about them, I ran into something to do with
> ob or ob_flush. Was I on a wrong tracK?
>
>
The problem with a session is you must start it before the headers are
sent to the browser. This will happen if there is *any* output sent,
even white space. So, for instance, if you have:
---top of file----
<?php
session_start();
?>
..... more of the file....
the session will fail because you sent output (a blank line) to the
browser. This caused the headers to be sent, and it's now too late to
start the session.
ob_start() buffers any output to the browser, so the headers aren't sent
right away. But IMHO this is a "quick and dirty" fix which just
bypasses the real problem. I think it's much better to structure code
properly so you issue the session_start() call at the beginning of your
page.
Others feel differently, but about the only time I use ob_start(), etc.,
is when I'm doing templating or similar, and need to parse the output
before sending it to the browser - i.e. substituting a persons name for
a [NAME] tag. But that's a little more advanced than we need to get
into right here.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|