User-defined Globals?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Grieve

    User-defined Globals?

    Having come to PHP from another CGI, there's one thing I really miss...

    The previous tool used was Witango, which had one major difference: the
    interpreter ran as a service/daemon. One benefit of this was, you could
    have variables scoped at a number of different levels above the
    once-only variables in PHP (i.e. sessions).

    Specifically, it was possible to create variables at a system-wide and
    site-wide level. I could set a value and it would be available to any
    app run within that site -- a classic example would be a variable
    containing the title of the site.

    Can I replicate this functionality in PHP?

    I'd guess that the norm in PHP is to re-read this kind of information
    from a DB with *every* hit; a huge overhead.

    Can PHP be run in a more "persistant " manner?

    Thanks,
    Jon
  • Erwin Moller

    #2
    Re: User-defined Globals?

    Jon Grieve wrote:
    [color=blue]
    > Having come to PHP from another CGI, there's one thing I really miss...
    >
    > The previous tool used was Witango, which had one major difference: the
    > interpreter ran as a service/daemon. One benefit of this was, you could
    > have variables scoped at a number of different levels above the
    > once-only variables in PHP (i.e. sessions).
    >
    > Specifically, it was possible to create variables at a system-wide and
    > site-wide level. I could set a value and it would be available to any
    > app run within that site -- a classic example would be a variable
    > containing the title of the site.
    >
    > Can I replicate this functionality in PHP?
    >
    > I'd guess that the norm in PHP is to re-read this kind of information
    > from a DB with *every* hit; a huge overhead.
    >
    > Can PHP be run in a more "persistant " manner?
    >
    > Thanks,
    > Jon[/color]

    Hi Jon,

    Yes, a database can be used, but that indeed creates some overhead.

    I always include a file that contains 'global' variables, and typically put
    it just outside the public_html.
    Very easy.
    But if you want to change these values, you will have to open and change the
    file of course, but that is also very easy in PHP.

    I am unaware of a 'real' global solution, but that doesn't mean there isn't
    one. ;-)

    Regards,
    Erwin Moller

    Comment

    • Kevin Thorpe

      #3
      Re: User-defined Globals?

      Jon Grieve wrote:
      [color=blue]
      > Having come to PHP from another CGI, there's one thing I really miss...
      >
      > The previous tool used was Witango, which had one major difference: the
      > interpreter ran as a service/daemon. One benefit of this was, you could
      > have variables scoped at a number of different levels above the
      > once-only variables in PHP (i.e. sessions).
      >
      > Specifically, it was possible to create variables at a system-wide and
      > site-wide level. I could set a value and it would be available to any
      > app run within that site -- a classic example would be a variable
      > containing the title of the site.
      >
      > Can I replicate this functionality in PHP?
      >
      > I'd guess that the norm in PHP is to re-read this kind of information
      > from a DB with *every* hit; a huge overhead.
      >
      > Can PHP be run in a more "persistant " manner?[/color]

      You could write your own session handler. The default one simply
      serialises $_SESSION and saves it in a plain file. I've seen a
      discussion somewhere about using a session server instead. At this point
      it would be relatively easy to maintain $_SESSION['global'] and
      $_SESSION['local'] separately.

      It does beg the question though as to why you need site/application
      global variables. Constants are easily supported using a config file.
      I've never come across a requirement for global variables, only session
      specific ones.

      Comment

      • Jon Grieve

        #4
        Re: User-defined Globals?

        Kevin Thorpe wrote:[color=blue]
        > Jon Grieve wrote:
        >[color=green]
        >> Having come to PHP from another CGI, there's one thing I really miss...
        >>
        >> The previous tool used was Witango, which had one major difference:
        >> the interpreter ran as a service/daemon. One benefit of this was, you
        >> could have variables scoped at a number of different levels above the
        >> once-only variables in PHP (i.e. sessions).
        >>
        >> Specifically, it was possible to create variables at a system-wide and
        >> site-wide level. I could set a value and it would be available to any
        >> app run within that site -- a classic example would be a variable
        >> containing the title of the site.
        >>
        >> Can I replicate this functionality in PHP?
        >>
        >> I'd guess that the norm in PHP is to re-read this kind of information
        >> from a DB with *every* hit; a huge overhead.
        >>
        >> Can PHP be run in a more "persistant " manner?[/color]
        >
        >
        > You could write your own session handler. The default one simply
        > serialises $_SESSION and saves it in a plain file. I've seen a
        > discussion somewhere about using a session server instead. At this point
        > it would be relatively easy to maintain $_SESSION['global'] and
        > $_SESSION['local'] separately.
        >
        > It does beg the question though as to why you need site/application
        > global variables. Constants are easily supported using a config file.
        > I've never come across a requirement for global variables, only session
        > specific ones.[/color]

        A couple of examples would be...

        I have some dynamic site, where just about everything to do with layout
        and presentation is dynamic (title, width, colours, etc., etc.). From
        what I've seen so far, a common way to handle this would be to maybe
        include() a file that defines and initialises all these values. This
        might be from a DB or simply a static set of consts. So, every PHP app
        includes this massive overhead.

        Now, of course, you should handle not having to do that for every single
        page view - presumably by using session variables - but you still have
        to do it every time a new session is started.

        What I'm used to is more like this (pseudo code):

        if (!$_DOMAIN['initialised'])
        {
        \\ hit the hugely time-consuming db to load
        \\ hundreds of globals
        $_DOMAIN['initialised'] = TRUE;
        }

        echo "<head><title>" .$_DOMAIN['title']."</title></head>";

        The main point here is, the special site-wide variables will have been
        initialised by the first person to hit the site, and will persist for
        *every other* session forever - not just a single "visitor".


        Another example I've used this for in the past would be to maintain a
        hit counter. Obviously, it's a massive overhead to 1) Read current
        count from DB; 2) Increment by 1; 3) Write back to DB - for every hit on
        every page. What I've done in the past is simply $_DOMAIN['counter']++.
        I've then used a scheduled/cron task to fire every "n" minutes that
        commits the current value to the DB -- vastly reduced overhead. Sure,
        if the server blew up, I might lose "n-1" minutes with of hits from the
        DB value, but in this particular example, it wasn't vastly important.


        As I said in the original post, the fundamental difference between
        Witango and PHP is that Witango runs as a service. Now, it may well be
        that this is partly due to the fact it has to maintain an always-running
        element to maintain such variables, and if PHP is never going to head in
        this direction, then this is all irrelevant.

        My point all along has been, this was a real killer feature of Witango,
        and I miss it... ;)

        So, assuming there's no plan to duplicate Witango's functionality, are
        there any elegant solutions to emulate this?

        Jon

        Comment

        • Justin Koivisto

          #5
          Re: User-defined Globals?

          Jon Grieve wrote:[color=blue]
          > Specifically, it was possible to create variables at a system-wide and
          > site-wide level. I could set a value and it would be available to any
          > app run within that site -- a classic example would be a variable
          > containing the title of the site.[/color]

          One option is to use Apache's "SetEnv" in your httpd.conf or .htaccess
          [color=blue]
          > Can I replicate this functionality in PHP?
          >
          > I'd guess that the norm in PHP is to re-read this kind of information
          > from a DB with *every* hit; a huge overhead.
          >
          > Can PHP be run in a more "persistant " manner?[/color]

          Short of using sessions or cookies, the only other options I see for
          this is to use a config file (http://us4.php.net/parse_ini_file) or
          include/require a file with everything defined in it.

          --
          Justin Koivisto - spam@koivi.com
          PHP POSTERS: Please use comp.lang.php for PHP related questions,
          alt.php* groups are not recommended.

          Comment

          • Justin Koivisto

            #6
            Re: User-defined Globals?

            Jon Grieve wrote:[color=blue]
            > I have some dynamic site, where just about everything to do with layout
            > and presentation is dynamic (title, width, colours, etc., etc.). From
            > what I've seen so far, a common way to handle this would be to maybe
            > include() a file that defines and initialises all these values. This
            > might be from a DB or simply a static set of consts. So, every PHP app
            > includes this massive overhead.
            >
            > Now, of course, you should handle not having to do that for every single
            > page view - presumably by using session variables - but you still have
            > to do it every time a new session is started.
            >
            > What I'm used to is more like this (pseudo code):
            >
            > if (!$_DOMAIN['initialised'])
            > {
            > \\ hit the hugely time-consuming db to load
            > \\ hundreds of globals
            > $_DOMAIN['initialised'] = TRUE;
            > }
            >
            > echo "<head><title>" .$_DOMAIN['title']."</title></head>";[/color]

            Why would you initialize anything that you won't use for that particular
            request? If you break up your variables or constants into multiple ini
            files, you only have the overhead needed to complete that request.
            [color=blue]
            > The main point here is, the special site-wide variables will have been
            > initialised by the first person to hit the site, and will persist for
            > *every other* session forever - not just a single "visitor".[/color]

            I don't think this is possible (at least I haven't heard of it).
            Basically, HTTP is a stateless protocol, so unless you are using a
            database, files, or _SESSION, even a single "visitor" won't be able to
            save the information from request to request.
            [color=blue]
            > So, assuming there's no plan to duplicate Witango's functionality, are
            > there any elegant solutions to emulate this?[/color]

            No, not that I see. However, you could likely create your own solution
            to this in the form of some kind of PHP extension.

            --
            Justin Koivisto - spam@koivi.com
            PHP POSTERS: Please use comp.lang.php for PHP related questions,
            alt.php* groups are not recommended.

            Comment

            • Tony Marston

              #7
              Re: User-defined Globals?


              "Jon Grieve" <jgrieve@southd own-co-uk> wrote in message
              news:10dgcqcjrq te27@corp.super news.com...[color=blue]
              > Kevin Thorpe wrote:[color=green]
              > > Jon Grieve wrote:
              > >[color=darkred]
              > >> Having come to PHP from another CGI, there's one thing I really miss...
              > >>
              > >> The previous tool used was Witango, which had one major difference:
              > >> the interpreter ran as a service/daemon. One benefit of this was, you
              > >> could have variables scoped at a number of different levels above the
              > >> once-only variables in PHP (i.e. sessions).
              > >>
              > >> Specifically, it was possible to create variables at a system-wide and
              > >> site-wide level. I could set a value and it would be available to any
              > >> app run within that site -- a classic example would be a variable
              > >> containing the title of the site.
              > >>
              > >> Can I replicate this functionality in PHP?
              > >>
              > >> I'd guess that the norm in PHP is to re-read this kind of information
              > >> from a DB with *every* hit; a huge overhead.
              > >>
              > >> Can PHP be run in a more "persistant " manner?[/color]
              > >
              > >
              > > You could write your own session handler. The default one simply
              > > serialises $_SESSION and saves it in a plain file. I've seen a
              > > discussion somewhere about using a session server instead. At this point
              > > it would be relatively easy to maintain $_SESSION['global'] and
              > > $_SESSION['local'] separately.
              > >
              > > It does beg the question though as to why you need site/application
              > > global variables. Constants are easily supported using a config file.
              > > I've never come across a requirement for global variables, only session
              > > specific ones.[/color]
              >
              > A couple of examples would be...
              >
              > I have some dynamic site, where just about everything to do with layout
              > and presentation is dynamic (title, width, colours, etc., etc.). From
              > what I've seen so far, a common way to handle this would be to maybe
              > include() a file that defines and initialises all these values. This
              > might be from a DB or simply a static set of consts. So, every PHP app
              > includes this massive overhead.[/color]

              *massive* is a relative term. Many sites load static values from a config
              file with every page, but it is usually up to the underlying file system to
              hold this in cache memory so as to avoid a disk read each time. With modern
              servers being so fast you should not notice any discernable delay.

              --
              Tony Marston

              This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




              Comment

              • Chung Leong

                #8
                Re: User-defined Globals?

                "Jon Grieve" <jgrieve@southd own-co-uk> wrote in message
                news:10dg4p8i5e cjqba@corp.supe rnews.com...[color=blue]
                > Having come to PHP from another CGI, there's one thing I really miss...
                >
                > The previous tool used was Witango, which had one major difference: the
                > interpreter ran as a service/daemon. One benefit of this was, you could
                > have variables scoped at a number of different levels above the
                > once-only variables in PHP (i.e. sessions).
                >
                > Specifically, it was possible to create variables at a system-wide and
                > site-wide level. I could set a value and it would be available to any
                > app run within that site -- a classic example would be a variable
                > containing the title of the site.
                >
                > Can I replicate this functionality in PHP?[/color]

                Use a temporary file to store the system wide variables.

                To load these, just acquire a shared lock, read the content, then
                unserialize it back to the array:

                $f = fopen('../sysvar', 'rb');
                flock($f, LOCK_SH);
                $SYSVARS = unserialize(fre ad($f, 1000000));
                fclose($f);

                To save them, acquire an exclusive lock, then write the serialized array to
                the file:

                $f = fopen('../sysvar', 'wb');
                fwrite($f, serialize($SYSV ARS));
                fclose($f);

                Basically you're doing what PHP does with session variable, except these
                will be shared among different users.

                Keep in mind that variables are supposed to be variable--i.e. they are meant
                to change over time. Unless the title of your site change from day to day,
                it should be stored in a variable. The preferred way of stored system
                configuration info is the use of .ini files.


                Comment

                Working...