how can i store global variables for whole web site

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

    how can i store global variables for whole web site

    hi

    vishal here. can anyone help me that how can i store the global
    variables for my web site. suppose i want to store the no. of users
    visited my site then what strategy should i use ?????

    can i implement some lock mechanism so that this global variable is not
    accessed by more than one application at some time which may create
    some confusion.

    thanks for your reply in advance........ .....

  • jonathan.beckett

    #2
    Re: how can i store global variables for whole web site

    It sounds like you want to store persistent data (i.e. it exists
    between sessions and/or server down-time).

    One solution is to put your variables in a file and just read/rewrite
    the file as required.

    Another solution is to have a table of key/value pairs in a database
    and update them as required.

    Another solution is to use the $_SESSION object if you don't need
    things to be persistent.

    Comment

    • vishal

      #3
      Re: how can i store global variables for whole web site

      thxs for information but what i want is that i want to store the
      information like how many users visited site. so global variable which
      is accessible by all forms and is there any mechanism to lock this
      variable so at a time only one application can access this variable.
      once the lock is unlocked then other application can access it.

      Comment

      • Daniel Tryba

        #4
        Re: how can i store global variables for whole web site

        vishal <vishal_panjabi @yahoo.co.in> wrote:[color=blue]
        > thxs for information but what i want is that i want to store the
        > information like how many users visited site. so global variable which
        > is accessible by all forms and is there any mechanism to lock this
        > variable so at a time only one application can access this variable.
        > once the lock is unlocked then other application can access it.[/color]

        Why not simply use a database for this (it's pretty good at counting :)?

        http://nl2.php.net/manual/en/ref.sem.php might be what you are looking
        for.

        Comment

        • NSpam

          #5
          Re: how can i store global variables for whole web site

          Daniel Tryba wrote:[color=blue]
          > vishal <vishal_panjabi @yahoo.co.in> wrote:
          >[color=green]
          >>thxs for information but what i want is that i want to store the
          >>information like how many users visited site. so global variable which
          >>is accessible by all forms and is there any mechanism to lock this
          >>variable so at a time only one application can access this variable.
          >>once the lock is unlocked then other application can access it.[/color]
          >
          >
          > Why not simply use a database for this (it's pretty good at counting :)?
          >
          > http://nl2.php.net/manual/en/ref.sem.php might be what you are looking
          > for.
          >[/color]
          <g>

          Comment

          • nospam@geniegate.com

            #6
            Re: how can i store global variables for whole web site

            In: <1109845030.773 001.145280@g14g 2000cwa.googleg roups.com>, "vishal" <vishal_panjabi @yahoo.co.in> wrote:[color=blue]
            >can i implement some lock mechanism so that this global variable is not
            >accessed by more than one application at some time which may create
            >some confusion.[/color]

            One of the things I regard as usually an asset, but sometimes a weakness is that
            there is no way to store global, application wide variables the way you can
            in java servlets. Part of this is the way php programs are parsed and run
            each time the page is loaded. Variables don't hang around after. The other part
            is the way fork() works.

            With mod_perl you can "sort of" share variables, but they only apply to the current
            process. (For this reason, synchronization is not an issue)

            On a UNIX platform, have a look at:




            As far as I know, (hopefully someone will correct me if I'm wrong) this is the
            only way to share _memory_.

            I've used shared memory in other applications (other languages) and
            generally recommend against it if at all possible. It can lead to a
            royal nuisance when it's time to free them. Maybe things have changed,
            maybe I was doing something wrong, but the shared memory was "leaked"
            when the program was terminated abruptly.

            Consider using a database, file, or even the serialize stuff for persistant
            storage, it's slower but unless you require the speed, it's safer.

            For obvious reasons, shared memory doesn't work across servers, so, if your
            page is ever served via multiple web servers, (load balancing, etc..) it
            won't work.

            Jamie
            --
            http://www.geniegate.com Custom web programming
            guhzo_42@lnubb. pbz (rot13) User Management Solutions

            Comment

            • R. Rajesh Jeba Anbiah

              #7
              Re: how can i store global variables for whole web site

              vishal wrote:[color=blue]
              > thxs for information but what i want is that i want to store the
              > information like how many users visited site. so global variable[/color]
              which[color=blue]
              > is accessible by all forms and is there any mechanism to lock this
              > variable so at a time only one application can access this variable.
              > once the lock is unlocked then other application can access it.[/color]

              DB may be the right choice. Or may try something like:
              <?php
              $filename = 'globals.txt';
              $fp = fopen($filename , 'rb+');
              flock($fp, LOCK_EX); // exclusive lock
              $contents = fread($fp, filesize($filen ame)+1);
              $MYGLOBALS = unserialize($co ntents);
              print_r($MYGLOB ALS); //test
              $MYGLOBALS['count'] += 1;
              $MYGLOBALS['foo'] += 5;
              print_r($MYGLOB ALS); //test
              rewind($fp);
              fwrite($fp, serialize($MYGL OBALS));
              flock($fp, LOCK_UN); //release lock
              fclose($fp);
              ?>

              --
              <?php echo 'Just another PHP saint'; ?>
              Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

              Comment

              Working...