Sessions

View: New views
8 Messages — Rating Filter:   Alert me  

Sessions

by JasonCarson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello all,

Do I have to add session_start() at the beginning of every page so that
the $_SESSION variables work on all pages or do I use session_start() on
the first page and something else on other pages?

Thanks


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


Re: Sessions

by Daniel Brown-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Jul 2, 2009 at 23:27, Jason Carson<jason@...> wrote:
> Hello all,
>
> Do I have to add session_start() at the beginning of every page so that
> the $_SESSION variables work on all pages or do I use session_start() on
> the first page and something else on other pages?

    Yes, unless you're using session autoloading.  Also, in most
cases, you will only need to call session_start() once (before
referencing $_SESSION), even if $_SESSION is accessed in an included
file.

--
</Daniel P. Brown>
daniel.brown@... || danbrown@...
http://www.parasane.net/ || http://www.pilotpig.net/
Check out our hosting and dedicated server deals at http://twitter.com/pilotpig

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


Re: Sessions

by Luke Slater-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/7/3 Daniel Brown <danbrown@...>

> On Thu, Jul 2, 2009 at 23:27, Jason Carson<jason@...> wrote:
> > Hello all,
> >
> > Do I have to add session_start() at the beginning of every page so that
> > the $_SESSION variables work on all pages or do I use session_start() on
> > the first page and something else on other pages?
>
>     Yes, unless you're using session autoloading.  Also, in most
> cases, you will only need to call session_start() once (before
> referencing $_SESSION), even if $_SESSION is accessed in an included
> file.
>
> --
> </Daniel P. Brown>
> daniel.brown@... || danbrown@...
> http://www.parasane.net/ || http://www.pilotpig.net/
> Check out our hosting and dedicated server deals at
> http://twitter.com/pilotpig
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Some people have a file called init.php, which would contain
session_start(); as well as other things that need to be done every page
load (connect to the database perhaps?) and they just 'require' that at the
top of every page.

--
Luke Slater
http://dinosaur-os.com/
:O)

Re: Sessions

by Tom Chubb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/7/3 Luke <luke@...>

> 2009/7/3 Daniel Brown <danbrown@...>
>
> > On Thu, Jul 2, 2009 at 23:27, Jason Carson<jason@...> wrote:
> > > Hello all,
> > >
> > > Do I have to add session_start() at the beginning of every page so that
> > > the $_SESSION variables work on all pages or do I use session_start()
> on
> > > the first page and something else on other pages?
> >
> >     Yes, unless you're using session autoloading.  Also, in most
> > cases, you will only need to call session_start() once (before
> > referencing $_SESSION), even if $_SESSION is accessed in an included
> > file.
> >
> > --
> > </Daniel P. Brown>
> > daniel.brown@... || danbrown@...
> > http://www.parasane.net/ || http://www.pilotpig.net/
> > Check out our hosting and dedicated server deals at
> > http://twitter.com/pilotpig
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
> Some people have a file called init.php, which would contain
> session_start(); as well as other things that need to be done every page
> load (connect to the database perhaps?) and they just 'require' that at the
> top of every page.
>
> --
> Luke Slater
> http://dinosaur-os.com/
> :O)
>

Never thought of that. Sounds like quite a good idea.
Can anyone tell me if there's any reason for not doing that, even on pages
that do not require session data?
Or perhaps use an htaccess file to server side include a file file to all
files under an admin folder or something and another to destroy the session.
I'm thinking of smaller, low-traffic sites.
I know people are going to say, if they're small sites, why can't you only
start sessions on the relevant pages but it sounds like it could work well
for me.

Re: Sessions

by Ashley Sheridan-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Friday 03 July 2009 09:41:40 Tom Chubb wrote:

> 2009/7/3 Luke <luke@...>
>
> > 2009/7/3 Daniel Brown <danbrown@...>
> >
> > > On Thu, Jul 2, 2009 at 23:27, Jason Carson<jason@...> wrote:
> > > > Hello all,
> > > >
> > > > Do I have to add session_start() at the beginning of every page so
> > > > that the $_SESSION variables work on all pages or do I use
> > > > session_start()
> >
> > on
> >
> > > > the first page and something else on other pages?
> > >
> > >     Yes, unless you're using session autoloading.  Also, in most
> > > cases, you will only need to call session_start() once (before
> > > referencing $_SESSION), even if $_SESSION is accessed in an included
> > > file.
> > >
> > > --
> > > </Daniel P. Brown>
> > > daniel.brown@... || danbrown@...
> > > http://www.parasane.net/ || http://www.pilotpig.net/
> > > Check out our hosting and dedicated server deals at
> > > http://twitter.com/pilotpig
> > >
> > > --
> > > PHP General Mailing List (http://www.php.net/)
> > > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> > Some people have a file called init.php, which would contain
> > session_start(); as well as other things that need to be done every page
> > load (connect to the database perhaps?) and they just 'require' that at
> > the top of every page.
> >
> > --
> > Luke Slater
> > http://dinosaur-os.com/
> >
> > :O)
>
> Never thought of that. Sounds like quite a good idea.
> Can anyone tell me if there's any reason for not doing that, even on pages
> that do not require session data?
> Or perhaps use an htaccess file to server side include a file file to all
> files under an admin folder or something and another to destroy the
> session. I'm thinking of smaller, low-traffic sites.
> I know people are going to say, if they're small sites, why can't you only
> start sessions on the relevant pages but it sounds like it could work well
> for me.


It's easier to maintain if you use one include file like Luke said. You won't
get much overhead from a call to session_start() on a page that doesn't use
sessions.

Thanks,
Ash
http://www.ashleysheridan.co.uk

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


Re: Sessions

by Tom Chubb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/7/3 Ashley Sheridan <ash@...>

> On Friday 03 July 2009 09:41:40 Tom Chubb wrote:
> > 2009/7/3 Luke <luke@...>
> >
> > > 2009/7/3 Daniel Brown <danbrown@...>
> > >
> > > > On Thu, Jul 2, 2009 at 23:27, Jason Carson<jason@...>
> wrote:
> > > > > Hello all,
> > > > >
> > > > > Do I have to add session_start() at the beginning of every page so
> > > > > that the $_SESSION variables work on all pages or do I use
> > > > > session_start()
> > >
> > > on
> > >
> > > > > the first page and something else on other pages?
> > > >
> > > >     Yes, unless you're using session autoloading.  Also, in most
> > > > cases, you will only need to call session_start() once (before
> > > > referencing $_SESSION), even if $_SESSION is accessed in an included
> > > > file.
> > > >
> > > > --
> > > > </Daniel P. Brown>
> > > > daniel.brown@... || danbrown@...
> > > > http://www.parasane.net/ || http://www.pilotpig.net/
> > > > Check out our hosting and dedicated server deals at
> > > > http://twitter.com/pilotpig
> > > >
> > > > --
> > > > PHP General Mailing List (http://www.php.net/)
> > > > To unsubscribe, visit: http://www.php.net/unsub.php
> > >
> > > Some people have a file called init.php, which would contain
> > > session_start(); as well as other things that need to be done every
> page
> > > load (connect to the database perhaps?) and they just 'require' that at
> > > the top of every page.
> > >
> > > --
> > > Luke Slater
> > > http://dinosaur-os.com/
> > >
> > > :O)
> >
> > Never thought of that. Sounds like quite a good idea.
> > Can anyone tell me if there's any reason for not doing that, even on
> pages
> > that do not require session data?
> > Or perhaps use an htaccess file to server side include a file file to all
> > files under an admin folder or something and another to destroy the
> > session. I'm thinking of smaller, low-traffic sites.
> > I know people are going to say, if they're small sites, why can't you
> only
> > start sessions on the relevant pages but it sounds like it could work
> well
> > for me.
>
>
> It's easier to maintain if you use one include file like Luke said. You
> won't
> get much overhead from a call to session_start() on a page that doesn't use
> sessions.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>

Great,
Cheers Ash,

T

--
Tom Chubb
tom@... | tomchubb@...

Re: Sessions

by Stuart-47 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/7/3 Ashley Sheridan <ash@...>:

> On Friday 03 July 2009 09:41:40 Tom Chubb wrote:
>> 2009/7/3 Luke <luke@...>
>>
>> > 2009/7/3 Daniel Brown <danbrown@...>
>> >
>> > > On Thu, Jul 2, 2009 at 23:27, Jason Carson<jason@...> wrote:
>> > > > Hello all,
>> > > >
>> > > > Do I have to add session_start() at the beginning of every page so
>> > > > that the $_SESSION variables work on all pages or do I use
>> > > > session_start()
>> >
>> > on
>> >
>> > > > the first page and something else on other pages?
>> > >
>> > >     Yes, unless you're using session autoloading.  Also, in most
>> > > cases, you will only need to call session_start() once (before
>> > > referencing $_SESSION), even if $_SESSION is accessed in an included
>> > > file.
>> > >
>> > > --
>> > > </Daniel P. Brown>
>> > > daniel.brown@... || danbrown@...
>> > > http://www.parasane.net/ || http://www.pilotpig.net/
>> > > Check out our hosting and dedicated server deals at
>> > > http://twitter.com/pilotpig
>> > >
>> > > --
>> > > PHP General Mailing List (http://www.php.net/)
>> > > To unsubscribe, visit: http://www.php.net/unsub.php
>> >
>> > Some people have a file called init.php, which would contain
>> > session_start(); as well as other things that need to be done every page
>> > load (connect to the database perhaps?) and they just 'require' that at
>> > the top of every page.
>> >
>> > --
>> > Luke Slater
>> > http://dinosaur-os.com/
>> >
>> > :O)
>>
>> Never thought of that. Sounds like quite a good idea.
>> Can anyone tell me if there's any reason for not doing that, even on pages
>> that do not require session data?
>> Or perhaps use an htaccess file to server side include a file file to all
>> files under an admin folder or something and another to destroy the
>> session. I'm thinking of smaller, low-traffic sites.
>> I know people are going to say, if they're small sites, why can't you only
>> start sessions on the relevant pages but it sounds like it could work well
>> for me.
>
>
> It's easier to maintain if you use one include file like Luke said. You won't
> get much overhead from a call to session_start() on a page that doesn't use
> sessions.

It's also worth noting that every call to session_start() will result
in the expiry time of the session being updated. Not calling it for
pages that don't use the session could lead to the session expiring if
the user doesn't hit a page that uses it for a while.

-Stuart

--
http://stut.net/

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


Re: Sessions

by Richard Heyes-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

> ..

This is precisely what I do, albeit my file is called config.php, and
not init.php. Not that it makes a jot of difference. This file is used
to setup the environment, so that way everything I commonly need is
available simply by including one file. One thing to note though is
that a database connection is not established by default. I used to
get a lot of comment spam on my blog and because it was needlessly
connecting to the database, it was bringing down the server. So now I
simply use something like this to quickly and easily get a reference
to a database object:

$db = getDatabase();

Wunderbar.

--
Richard Heyes
HTML5 graphing: RGraph (www.rgraph.net - updated 3rd July)

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