Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #17722 > unrolled thread

PHP sessions

Started bymadwomans Dad <dsvirtual58@gmail.com>
First post2018-05-06 09:19 +0100
Last post2018-05-06 13:21 -0400
Articles 12 — 5 participants

Back to article view | Back to comp.lang.php


Contents

  PHP sessions madwomans Dad <dsvirtual58@gmail.com> - 2018-05-06 09:19 +0100
    Re: PHP sessions "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-05-06 12:51 +0200
      Re: PHP sessions madwomans Dad <dsvirtual58@gmail.com> - 2018-05-06 12:59 +0100
        Re: PHP sessions "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-05-06 14:11 +0200
        Re: PHP sessions Gregor Kofler <usenet@gregorkofler.com> - 2018-05-06 14:13 +0200
          Re: PHP sessions madwomans Dad <dsvirtual58@gmail.com> - 2018-05-06 14:08 +0100
            Re: PHP sessions "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-05-06 15:22 +0200
              Re: PHP sessions madwomans Dad <dsvirtual58@gmail.com> - 2018-05-06 16:01 +0100
                Re: PHP sessions "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-05-06 17:27 +0200
                  Re: PHP sessions madwomans Dad <dsvirtual58@gmail.com> - 2018-05-06 19:17 +0100
        Re: PHP sessions "J.O. Aho" <user@example.net> - 2018-05-06 18:57 +0200
    Re: PHP sessions Richard Damon <Richard@Damon-Family.org> - 2018-05-06 13:21 -0400

#17722 — PHP sessions

Frommadwomans Dad <dsvirtual58@gmail.com>
Date2018-05-06 09:19 +0100
SubjectPHP sessions
Message-ID<pcmdpu$2ek$1@dont-email.me>
I'm new to this group and to PHP.
I'm very familiar with Java and I'm trying to compare PHP session 
handling with J2EE session handling and I've come up with a bit of a 
problem.

I have the following code in step1.php. code for sessionStatus shown after

step1.php, redirect commented out

<?php
require '../functions/utilities.php';

echo "step1 - check session - ";
sessionStatus();
echo "<br> starting session";
session_start();
echo "<br> session started";
sessionStatus();
//header("location: step2.php");
?>

I get the following output
step1 - check session -
there is no session
starting session
session started
there is an active session

if I then enable the redirect to step2.php

step2.php

<?php
require '../functions/utilities.php';

echo "step2 - check session - ";
sessionStatus();
?>

I would expect to see "there is an active session" but what I get is

step2 - check session -
there is no session

Huh! what am I missing
thanks

code for sessionStatus

function sessionStatus(){
    echo "<br>";
    switch (session_status()) {
	case PHP_SESSION_DISABLED:
         echo "session is disabled";
	break;
	case PHP_SESSION_NONE:
	echo "there is no session";
	break;
	case PHP_SESSION_ACTIVE:
	echo "there is an active session";
	break;
	default:
	echo "unknown status";
	}
}

[toc] | [next] | [standalone]


#17723

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2018-05-06 12:51 +0200
Message-ID<pcmmoc$b4p$1@solani.org>
In reply to#17722
On 06.05.2018 at 10:19, madwomans Dad wrote:

> I'm very familiar with Java and I'm trying to compare PHP session
> handling with J2EE session handling and I've come up with a bit of a
> problem.
> 
> I get the following output
> step1 - check session -
> there is no session
> starting session
> session started
> there is an active session
> 
> if I then enable the redirect to step2.php
> 
> I would expect to see "there is an active session" but what I get is
> 
> step2 - check session -
> there is no session
> 
> Huh! what am I missing

You have to start the session for each request, since PHP closes
sessions automatically at the end of the request.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#17724

Frommadwomans Dad <dsvirtual58@gmail.com>
Date2018-05-06 12:59 +0100
Message-ID<pcmqnr$n17$1@dont-email.me>
In reply to#17723
On 06/05/2018 11:51, Christoph M. Becker wrote:
> On 06.05.2018 at 10:19, madwomans Dad wrote:
> 
>> I'm very familiar with Java and I'm trying to compare PHP session
>> handling with J2EE session handling and I've come up with a bit of a
>> problem.
>>
>> I get the following output
>> step1 - check session -
>> there is no session
>> starting session
>> session started
>> there is an active session
>>
>> if I then enable the redirect to step2.php
>>
>> I would expect to see "there is an active session" but what I get is
>>
>> step2 - check session -
>> there is no session
>>
>> Huh! what am I missing
> 
> You have to start the session for each request, since PHP closes
> sessions automatically at the end of the request.
> 
I clicked reply. I should have clicked follow up. Apologies to C M B

Anyway

What the heck is the point of a session object that closes automatically 
at the end of a request? The whole point of a session is that it stays 
active for a particular sequence of requests or a users 'session' on  a 
particular web site.

This makes no sense at all. There must be way to track a session in PHP.

Go on, what's the secret.

Madwomans Dad

[toc] | [prev] | [next] | [standalone]


#17725

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2018-05-06 14:11 +0200
Message-ID<pcmre3$emj$1@solani.org>
In reply to#17724
On 06.05.2018 at 13:59, madwomans Dad wrote:

> What the heck is the point of a session object that closes automatically
> at the end of a request? The whole point of a session is that it stays
> active for a particular sequence of requests or a users 'session' on  a
> particular web site.

The session is closed, but not destroyed.  You simply have to start the
session again.  A simplified example:

step1.php
<?php
session_start();
$_SESSION['foo'] = 'bar';
header('Location: step2.php');
?>

step2.php
<?php
session_start();
echo $_SESSION['foo'];
?>

If you don't want to call session_start() at the beginning of each
script, you can also set session.auto_start=1 in your php.ini.  See
<http://www.php.net/manual/en/session.configuration.php#ini.session.auto-start>.

See also <http://www.php.net/manual/en/book.session.php>.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#17726

FromGregor Kofler <usenet@gregorkofler.com>
Date2018-05-06 14:13 +0200
Message-ID<pcmrhu$t5l$1@dont-email.me>
In reply to#17724
Am 2018-05-06 um 13:59 schrieb madwomans Dad:
> On 06/05/2018 11:51, Christoph M. Becker wrote:
>> On 06.05.2018 at 10:19, madwomans Dad wrote:
>>
>>> I'm very familiar with Java and I'm trying to compare PHP session
>>> handling with J2EE session handling and I've come up with a bit of a
>>> problem.
>>>
>>> I get the following output
>>> step1 - check session -
>>> there is no session
>>> starting session
>>> session started
>>> there is an active session
>>>
>>> if I then enable the redirect to step2.php
>>>
>>> I would expect to see "there is an active session" but what I get is
>>>
>>> step2 - check session -
>>> there is no session
>>>
>>> Huh! what am I missing
>>
>> You have to start the session for each request, since PHP closes
>> sessions automatically at the end of the request.
>>
> I clicked reply. I should have clicked follow up. Apologies to C M B
> 
> Anyway
> 
> What the heck is the point of a session object that closes automatically
> at the end of a request? The whole point of a session is that it stays
> active for a particular sequence of requests or a users 'session' on  a
> particular web site.
> 
> This makes no sense at all. There must be way to track a session in PHP.
> 
> Go on, what's the secret.

To read the manual?

http://php.net/manual/en/function.session-start.php

says
"session_start — Start new or *resume* existing session"

If you care to read the few paragraphs on the aforementioned page you
will understand how sessions are handled. It's quite easy.

Gregor

[toc] | [prev] | [next] | [standalone]


#17727

Frommadwomans Dad <dsvirtual58@gmail.com>
Date2018-05-06 14:08 +0100
Message-ID<pcmunn$j5s$1@dont-email.me>
In reply to#17726
On 06/05/2018 13:13, Gregor Kofler wrote:
> Am 2018-05-06 um 13:59 schrieb madwomans Dad:
>> On 06/05/2018 11:51, Christoph M. Becker wrote:
>>> On 06.05.2018 at 10:19, madwomans Dad wrote:
>>>
>>>> I'm very familiar with Java and I'm trying to compare PHP session
>>>> handling with J2EE session handling and I've come up with a bit of a
>>>> problem.
>>>>
>>>> I get the following output
>>>> step1 - check session -
>>>> there is no session
>>>> starting session
>>>> session started
>>>> there is an active session
>>>>
>>>> if I then enable the redirect to step2.php
>>>>
>>>> I would expect to see "there is an active session" but what I get is
>>>>
>>>> step2 - check session -
>>>> there is no session
>>>>
>>>> Huh! what am I missing
>>>
>>> You have to start the session for each request, since PHP closes
>>> sessions automatically at the end of the request.
>>>
>> I clicked reply. I should have clicked follow up. Apologies to C M B
>>
>> Anyway
>>
>> What the heck is the point of a session object that closes automatically
>> at the end of a request? The whole point of a session is that it stays
>> active for a particular sequence of requests or a users 'session' on  a
>> particular web site.
>>
>> This makes no sense at all. There must be way to track a session in PHP.
>>
>> Go on, what's the secret.
> 
> To read the manual?
> 
> http://php.net/manual/en/function.session-start.php
> 
> says
> "session_start — Start new or *resume* existing session"
> 
> If you care to read the few paragraphs on the aforementioned page you
> will understand how sessions are handled. It's quite easy.
> 
> Gregor
> 
> 
Thunderbird is driving me nuts.

I meant to say to the ng

It's pointlessly complicated and a very poor abstraction of a user 
session. I wish I didn't have to use PHP. Life is just too short for 
this crap.

[toc] | [prev] | [next] | [standalone]


#17728

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2018-05-06 15:22 +0200
Message-ID<pcmvi9$hdd$1@solani.org>
In reply to#17727
On 06.05.2018 at 15:08, madwomans Dad wrote:

> Thunderbird is driving me nuts.
> 
> I meant to say to the ng

Then just do so. :)

> It's pointlessly complicated and a very poor abstraction of a user
> session. I wish I didn't have to use PHP. Life is just too short for
> this crap.

Then just don't use PHP. :)

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#17729

Frommadwomans Dad <dsvirtual58@gmail.com>
Date2018-05-06 16:01 +0100
Message-ID<pcn5d7$39g$1@dont-email.me>
In reply to#17728
On 06/05/2018 14:22, Christoph M. Becker wrote:
> On 06.05.2018 at 15:08, madwomans Dad wrote:
> 
>> Thunderbird is driving me nuts.
>>
>> I meant to say to the ng
> 
> Then just do so. :)
> 
>> It's pointlessly complicated and a very poor abstraction of a user
>> session. I wish I didn't have to use PHP. Life is just too short for
>> this crap.
> 
> Then just don't use PHP. :)

Unfortunately the LAMP stack is part of my life for the next 3 months.
L I can live with, I use it every day anyway, A isn't a container so I 
could care less, M, I've been using for years. But P, I've avoided it so 
far so I suppose it had to happen eventually. I'll stop bitching and 
whining now and suck it up. Depressing but survivable I suppose.

Madwomans Dad

[toc] | [prev] | [next] | [standalone]


#17730

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2018-05-06 17:27 +0200
Message-ID<pcn6sg$mmc$1@solani.org>
In reply to#17729
On 06.05.2018 at 17:01, madwomans Dad wrote:

> On 06/05/2018 14:22, Christoph M. Becker wrote:
>
>> Then just don't use PHP. :)
> 
> Unfortunately the LAMP stack is part of my life for the next 3 months.
> L I can live with, I use it every day anyway, A isn't a container so I
> could care less, M, I've been using for years. But P, I've avoided it so
> far so I suppose it had to happen eventually. I'll stop bitching and
> whining now and suck it up. Depressing but survivable I suppose.

Way better attitude, thanks!

PHP in a LAMP stack is similar to CGI applications: each request starts
with a clean slate.  Therefore the session has to be started/resumed
each time.

Wrt. object serialization in sessions, see
<http://www.php.net/manual/en/language.oop5.serialization.php>.
Personally, I suggest to use autoloading anyway.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#17733

Frommadwomans Dad <dsvirtual58@gmail.com>
Date2018-05-06 19:17 +0100
Message-ID<pcngr8$o2u$1@dont-email.me>
In reply to#17730
On 06/05/2018 16:27, Christoph M. Becker wrote:
> On 06.05.2018 at 17:01, madwomans Dad wrote:
> 
>> On 06/05/2018 14:22, Christoph M. Becker wrote:
>>
>>> Then just don't use PHP. :)
>>
>> Unfortunately the LAMP stack is part of my life for the next 3 months.
>> L I can live with, I use it every day anyway, A isn't a container so I
>> could care less, M, I've been using for years. But P, I've avoided it so
>> far so I suppose it had to happen eventually. I'll stop bitching and
>> whining now and suck it up. Depressing but survivable I suppose.
> 
> Way better attitude, thanks!
> 
> PHP in a LAMP stack is similar to CGI applications: each request starts
> with a clean slate.  Therefore the session has to be started/resumed
> each time.
> 
> Wrt. object serialization in sessions, see
> <http://www.php.net/manual/en/language.oop5.serialization.php>.
> Personally, I suggest to use autoloading anyway.

Well here's a thing.
The class is defined in a remote script file that contains stuff to do 
with pesistence. This file is included in the script that creates an 
Object. In the script that loads the object from the session I saw no 
need to include this file as I wasn't using any of the resources therin.

I read something about exposing the definition of the class to a script 
that needs to load an instance from the session so I included the file 
and de-serialization occured without a problem. This seems like an 
inteligent solution, I'll be more convinced when I try to load more 
complex Objects.

Madwomans Dad

[toc] | [prev] | [next] | [standalone]


#17731

From"J.O. Aho" <user@example.net>
Date2018-05-06 18:57 +0200
Message-ID<fl8qf8FahvfU1@mid.individual.net>
In reply to#17724
On 05/06/18 13:59, madwomans Dad wrote:
> On 06/05/2018 11:51, Christoph M. Becker wrote:
>> On 06.05.2018 at 10:19, madwomans Dad wrote:
>>
>>> I'm very familiar with Java and I'm trying to compare PHP session
>>> handling with J2EE session handling and I've come up with a bit of a
>>> problem.
>>>
>>> I get the following output
>>> step1 - check session -
>>> there is no session
>>> starting session
>>> session started
>>> there is an active session
>>>
>>> if I then enable the redirect to step2.php
>>>
>>> I would expect to see "there is an active session" but what I get is
>>>
>>> step2 - check session -
>>> there is no session
>>>
>>> Huh! what am I missing
>>
>> You have to start the session for each request, since PHP closes
>> sessions automatically at the end of the request.
>>
> I clicked reply. I should have clicked follow up. Apologies to C M B
> 
> Anyway
> 
> What the heck is the point of a session object that closes automatically
> at the end of a request?

Why use a session on a page which do not require a such? ;)


> The whole point of a session is that it stays
> active for a particular sequence of requests or a users 'session' on  a
> particular web site.

As people have already pointed out, the session_start will resume the
current session as long as it hasn't expired or nullified.


> This makes no sense at all. There must be way to track a session in PHP.

auto_prepend_file in php.ini file can load a script automatically for
you which can have the session_start, then it's more like you want it to
be, but don't forget to document that or else next guy fixing the site
won't know how the session is started by magic.


-- 

 //Aho

[toc] | [prev] | [next] | [standalone]


#17732

FromRichard Damon <Richard@Damon-Family.org>
Date2018-05-06 13:21 -0400
Message-ID<EQGHC.86618$6C5.82271@fx07.iad>
In reply to#17722
On 5/6/18 4:19 AM, madwomans Dad wrote:
> I'm new to this group and to PHP.
> I'm very familiar with Java and I'm trying to compare PHP session
> handling with J2EE session handling and I've come up with a bit of a
> problem.
> 
> I have the following code in step1.php. code for sessionStatus shown after
> 
> step1.php, redirect commented out
> 
> <?php
> require '../functions/utilities.php';
> 
> echo "step1 - check session - ";
> sessionStatus();
> echo "<br> starting session";
> session_start();
> echo "<br> session started";
> sessionStatus();
> //header("location: step2.php");
> ?>
> 
> I get the following output
> step1 - check session -
> there is no session
> starting session
> session started
> there is an active session
> 
> if I then enable the redirect to step2.php
> 
> step2.php
> 
> <?php
> require '../functions/utilities.php';
> 
> echo "step2 - check session - ";
> sessionStatus();
> ?>
> 
> I would expect to see "there is an active session" but what I get is
> 
> step2 - check session -
> there is no session
> 
> Huh! what am I missing
> thanks
> 
> code for sessionStatus
> 
> function sessionStatus(){
>    echo "<br>";
>    switch (session_status()) {
>     case PHP_SESSION_DISABLED:
>         echo "session is disabled";
>     break;
>     case PHP_SESSION_NONE:
>     echo "there is no session";
>     break;
>     case PHP_SESSION_ACTIVE:
>     echo "there is an active session";
>     break;
>     default:
>     echo "unknown status";
>     }
> }
> 

One other comment on what I see here, for this code to even be working
as well as it is I suspect you must have turned on output buffering and
letting that let your get around sequence requirements in your code.

One requirement is that header() says it MUST be called before ANY
output is sent. Thus your location: header will only be effective if you
have buffering happening so the previous echos are still sitting in the
buffer and haven't been actually sent yet. I find this risky as it isn't
hard to accidentally get the data sent early and then you wonder why the
header command suddenly stopped working.

The second issue is related in that session_start() also needs to be
called before any output if you want to use cookie based sessions, (as
the cookie will be sent as part of the header). If you don't use cookie
based sessions, then you need to send the SESSION_ID as part of the URL,
which you didn't in the location header, so the session can only be
propagated if it was able to generate and send the cookie (which means
that no output has actually been sent).

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web