pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: apz <apz@nofate.com>
To: Ludwig Lim <lud_nowhere_man@yahoo.com>
Cc: PostgreSQL Mailing List <pgsql-php@postgresql.org>
Subject: Re: Questions Regarding Sessions
Date: Sat, 22 Mar 2003 01:40:42 -0500
Message-ID: <3E7C056A.1090408@nofate.com> (raw)
In-Reply-To: <20030322050118.17564.qmail@web80411.mail.yahoo.com>
References: <20030322050118.17564.qmail@web80411.mail.yahoo.com>

Ludwig Lim wrote:
>    I've tried testing simple session scripts and it
> works, but initializing and using sessions in a
> function seems to be a problem for me.

do session_start and session_register in global scope, then it seems to 
work.
http://www.php.net/manual/en/function.session-start.php


>    How can I make the script above to work?

I dont know if this is proper 'recommended' php way, but I have an 
include called myPageStart.inc.php that I load on every page that needs 
sessions:



-------/ START: t1.php /--------------
<?
     function func1()
     {
         global $HTTP_SESSION_VARS;
         $HTTP_SESSION_VARS["test"]="hello";
     }
?>
-------/ END: t1.php /--------------




-------/ START: t2.php /--------------
<?
   include ("myPageStart.inc.php");
   include ("t1.php");
   func1();
   if (session_is_registered($HTTP_SESSION_VARS["test"])){
    .....
?>
-------/ END: t1.php /--------------



-------/ START: myPageStart.inc.php /--------------
<?
   session_start();
   session_register("test");
   session_register($HTTP_SESSION_VARS["test"]);
?>
-------/ END: : mySessionStart.inc.php /--------------



another few notes (by quicly reading: 
http://www.php.net/manual/en/ref.session.php )

using $HTTP_SESSION_VARS is deprecated, try using just $_SESSION
    it was ok until PHP 4.0.6, since then use $_SESSION

when using $_SESSION, its always global, no need to ask for it to be
    from global scope in a function. It already is.

using session_register is now not needed

dont use session_is_registered, there is no need, just use as if any
    other var with isset, so your if should be just:
      if (isset($_SESSION[$_SESSION["test"]])) {

which brings me to another point. you seem to confuse the idea of
    session key with session value.
         $_SESSION["test"]="hello";
    here key = "test",  value = "hello"
    as if:
         $key   = "test";
         $value = "hello";
         $_SESSION[$key] = $value;
    but then, when you register/check if registered, the key is different
         $_SESSION["test"]="hello";
         session_register($_SESSION["test"]);
    is same as if:
         $_SESSION["test"]="hello";
         session_register("hello");
    or if you prefer to see it through $key , $value then:
         $key   = "test";
         $value = "hello";
         $_SESSION[$key] = $value;
         session_register($value);
    Now, when you do this session_register you tell it that there is
    another key within session, now we have two keys:
         $_SESSION["test"]
         $_SESSION["hello"]
    I dont think this is what you want, I am including here what I think
    you really wanted, with what should be up to newest php specs:



-------/ START: t1.php /--------------
<?
     function func1()
     {
         $_SESSION["test"]="hello";
     }
?>
-------/ END: t1.php /--------------




-------/ START: t2.php /--------------
<?
   include ("myPageStart.inc.php");
   include ("t1.php");
   func1();
   if (isset($_SESSION["test"])){
      echo ("Session is registerd <br>");
      $x = $_SESSION["test"];
      echo ("value of session = $x");
      session_destroy();
   }
?>
-------/ END: t1.php /--------------



-------/ START: myPageStart.inc.php /--------------
<?
   session_start();
?>
-------/ END: : myPageStart.inc.php /--------------


You mgith want to question myPageStart.inc.php, I also use it, besides 
starting session, start various counters which later I use to in 
myPageEnd.inc.php to log what parts of my page took how long to 
generate, so that I know what to look next to optimize.


hope this helps


/apz,  The moving cursor writes, and having written, blinks on.




view thread (3+ messages)  latest in thread

Message-ID: <3E7C056A.1090408@nofate.com>
Permalink:  ../3E7C056A.1090408@nofate.com/
Also on:    postgresql.org/message-id/3E7C056A.1090408@nofate.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: apz@nofate.com, lud_nowhere_man@yahoo.com
  Subject: Re: Questions Regarding Sessions
  In-Reply-To: <3E7C056A.1090408@nofate.com>
  References: <20030322050118.17564.qmail@web80411.mail.yahoo.com> <3E7C056A.1090408@nofate.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox