Connecting Tech Pros Worldwide Forums | Help | Site Map

Mantain a variable through several scripts like global.asa variables in asp

Zeppelin
Guest
 
Posts: n/a
#1: Dec 27 '05
Hi,

I have to port from asp a script that adds an ip to a list in plain
text or an array. I need not using a database or filesystem, it has to
be stored in memory in order that giving us best performance (it should
be accessed continuously).

I've tried defining $_ENV vars, using apache_setenv(..), creating a
class with a static var... but nothing results.

In asp the script writes an reads an Application variable defined in
global.asa... is it impossible to port? isn't it a very big error of
php?

thanks


Erwin Moller
Guest
 
Posts: n/a
#2: Dec 27 '05

re: Mantain a variable through several scripts like global.asa variables in asp


Zeppelin wrote:
[color=blue]
> Hi,
>
> I have to port from asp a script that adds an ip to a list in plain
> text or an array. I need not using a database or filesystem, it has to
> be stored in memory in order that giving us best performance (it should
> be accessed continuously).[/color]

Hi,

The easiest way is just including a file at top of your scripts.
Into that file you define some constants or variables.

[color=blue]
>
> I've tried defining $_ENV vars, using apache_setenv(..), creating a
> class with a static var... but nothing results.[/color]

Don't.
:-)

Just include a file.
[color=blue]
>
> In asp the script writes an reads an Application variable defined in
> global.asa... is it impossible to port? isn't it a very big error of
> php?[/color]

No, this isn't a big error.

If you worry about performance, don't.
Files that are accessed a lot of time will be placed in cache (memory) if
possible by the OS.
Thus no performancepenalty.

On a sidenote: I don't understand why so many people worry about performance
when they do not have a performanceproblem.
My guess is that over 90% of all performanceproblems are databaserelated
(bad queries, no indexing, etc) and not scriptinglanguage related.

If you absolutely want to use shared memory in PHP, that is possible.
I do n't remember the name of the package, but if you look around I am sure
you will find it.
My advise is however: Just include a file.

Regards,
Erwin Moller
[color=blue]
>
> thanks[/color]

Kimmo Laine
Guest
 
Posts: n/a
#3: Dec 27 '05

re: Mantain a variable through several scripts like global.asa variables in asp


"Erwin Moller"
<since_humans_read_this_I_am_spammed_too_much@spam yourself.com> wrote in
message news:43b1326a$0$11070$e4fe514c@news.xs4all.nl...[color=blue]
> If you absolutely want to use shared memory in PHP, that is possible.
> I do n't remember the name of the package, but if you look around I am
> sure
> you will find it.[/color]

Shared memory operations aka. shmop?
http://www.php.net/manual/en/ref.shmop.php

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
antaatulla.sikanautaa@gmail.com.NOSPAM.invalid


Erwin Moller
Guest
 
Posts: n/a
#4: Dec 27 '05

re: Mantain a variable through several scripts like global.asa variables in asp


Kimmo Laine wrote:
[color=blue]
> "Erwin Moller"
> <since_humans_read_this_I_am_spammed_too_much@spam yourself.com> wrote in
> message news:43b1326a$0$11070$e4fe514c@news.xs4all.nl...[color=green]
>> If you absolutely want to use shared memory in PHP, that is possible.
>> I do n't remember the name of the package, but if you look around I am
>> sure
>> you will find it.[/color]
>
> Shared memory operations aka. shmop?
> http://www.php.net/manual/en/ref.shmop.php
>[/color]

That is the one. :-)
I couldn't remember its impossible name. :P

Regards,
Erwin Moller

Gordon Burditt
Guest
 
Posts: n/a
#5: Dec 28 '05

re: Mantain a variable through several scripts like global.asa variables in asp


>I have to port from asp a script that adds an ip to a list in plain[color=blue]
>text or an array. I need not using a database or filesystem, it has to
>be stored in memory in order that giving us best performance (it should
>be accessed continuously).[/color]

Have you actually MEASURED the performance to prove this?
Often, even program code isn't stored in memory most of the time.

If your web server gets restarted (software crash, someone trips
over power cable, maintenance), your variable goes poof! Is this
acceptable behavior?
[color=blue]
>I've tried defining $_ENV vars, using apache_setenv(..), creating a
>class with a static var... but nothing results.[/color]
[color=blue]
>In asp the script writes an reads an Application variable defined in
>global.asa... is it impossible to port? isn't it a very big error of
>php?[/color]

The closest PHP comes to this is one of:
- Sessions (does not apply to unrelated users)
- Files
- A database
- System V shared memory segments (shm_* functions)

Global variables are in general bad programming practice. Making
varibles that are "ultra-global" over all web page accesses by
unrelated users is asking for even more problems. How, for example,
do you prevent several simultaneous page accesses from simultaneously
updating (and screwing up) the variable? There's a reason why
databases have so many features relating to locking, transactions,
and consistent views of the data.

Gordon L. Burditt
Chung Leong
Guest
 
Posts: n/a
#6: Dec 28 '05

re: Mantain a variable through several scripts like global.asa variables in asp


Zeppelin wrote:[color=blue]
> In asp the script writes an reads an Application variable defined in
> global.asa... is it impossible to port? isn't it a very big error of
> php?[/color]

Not a big error, just a limit imposed by PHP's cross-platform nature.
It's easy enough to have shared variables on Windows. On Unixes it's
rather more difficult.

There is usually more than one way to skin a cat. If you give us some
details on what the code does, we might be able to suggest an
alternative--and perhaps superior--solution.

Zeppelin
Guest
 
Posts: n/a
#7: Dec 28 '05

re: Mantain a variable through several scripts like global.asa variables in asp


> Have you actually MEASURED the performance to prove this?[color=blue]
> Often, even program code isn't stored in memory most of the time[/color]

I have no stadistics about this, but every new version of applicattion,
servers goes 100% and crashes down. So we want be prepared.
[color=blue]
> If your web server gets restarted (software crash, someone trips
> over power cable, maintenance), your variable goes poof! Is this
> acceptable behavior?[/color]

Nowadays, the variable is lost and no problems registered. Only than
first 60 clients don't have the best service ... but its only a few
mseconds...
The script could be improved saving the variable to bd every 10 minutes
;)

I will try with System V shared memory segments (shm_* functions) .
[color=blue]
> Global variables are in general bad programming practice. Making
> varibles that are "ultra-global" over all web page accesses by
> unrelated users is asking for even more problems. How, for example,
> do you prevent several simultaneous page accesses from simultaneously
> updating (and screwing up) the variable? There's a reason why
> databases have so many features relating to locking, transactions,
> and consistent views of the data.[/color]

The problem is that this script is not a part of a CMS or simmilar
application... it is the minnimun piece of a big system. It has a very
clear mission and does nothing more.

Zeppelin
Guest
 
Posts: n/a
#8: Dec 28 '05

re: Mantain a variable through several scripts like global.asa variables in asp


As I respond later, we haver to worry about performance: we expect to
have about 10 millions of connections in a very few days... lots of
them concurrent.

Each day it is not the situation, but we are going to release a new
version and we know there will be lots of new users. Last time we
released, server crashed down serveral times... Nowadays, machine is
better, but users number has grown up no proportionaly.

The variable is changing everytime, I can't include a file because
variable is shared. The change that makes a user is needed by next
user.

I will try with shard memory as you say.

In any case, thank you ;).

Zeppelin
Guest
 
Posts: n/a
#9: Dec 28 '05

re: Mantain a variable through several scripts like global.asa variables in asp


> There is usually more than one way to skin a cat. If you give us some[color=blue]
> details on what the code does, we might be able to suggest an
> alternative--and perhaps superior--solution.[/color]

Ok, really simple:

detect user's real ip
load users ip table (this is my shared variable)
send 60 equispaced items of the table to user (as body of http
response)
rotate ip's, loosing first item and adding the detected ip at the end
end

I suppose it would be more effective if we used a dedicated mini-server
for this simple script or write it in another language (bash?) and
redirect the requests of this service with apache to it. But I thought
it would be simplier using php.

Jerry Stuckle
Guest
 
Posts: n/a
#10: Dec 28 '05

re: Mantain a variable through several scripts like global.asa variables in asp


Zeppelin wrote:[color=blue][color=green]
>>Have you actually MEASURED the performance to prove this?
>>Often, even program code isn't stored in memory most of the time[/color]
>
>
> I have no stadistics about this, but every new version of applicattion,
> servers goes 100% and crashes down. So we want be prepared.
>[/color]

So, instead of determining where the slowdown is, you're grasping at
straws? Spending a lot of time trying to code something which could
crash the server and may or may not resolve the problem, instead of
determining the cause of the problem?

I would think offloading some of the work would be more efficient. For
instance, if you're supplying a download, have another server actually
perform the download. Using a database? Put the database on a
different server. And so on.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Jerry Stuckle
Guest
 
Posts: n/a
#11: Dec 28 '05

re: Mantain a variable through several scripts like global.asa variables in asp


Zeppelin wrote:[color=blue][color=green]
>>There is usually more than one way to skin a cat. If you give us some
>>details on what the code does, we might be able to suggest an
>>alternative--and perhaps superior--solution.[/color]
>
>
> Ok, really simple:
>
> detect user's real ip
> load users ip table (this is my shared variable)
> send 60 equispaced items of the table to user (as body of http
> response)
> rotate ip's, loosing first item and adding the detected ip at the end
> end
>
> I suppose it would be more effective if we used a dedicated mini-server
> for this simple script or write it in another language (bash?) and
> redirect the requests of this service with apache to it. But I thought
> it would be simplier using php.
>[/color]

Won't work. Detecting the user's real ip only works if they are
connecting directly to you or using a proxy which forwards the real IP
address. Most proxies, especially corporate ones, are set up to not
forward internal ip addresses. All you'll get is the proxy address -
which may be shared by multiple users (as is the case in my company).

Additionally, may large corporations and some hosting companies (i.e.
AOL) have redundant proxies. It's entirely possible for two requests
from the same user to go through two different proxies.

How would you handle these cases?


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Chung Leong
Guest
 
Posts: n/a
#12: Dec 29 '05

re: Mantain a variable through several scripts like global.asa variables in asp


Zeppelin wrote:[color=blue]
> detect user's real ip
> load users ip table (this is my shared variable)
> send 60 equispaced items of the table to user (as body of http
> response)
> rotate ip's, loosing first item and adding the detected ip at the end
> end[/color]

Don't see a good way to do this other than to serialize an array, write
the string to a file, then read it back. Not a terribly scalable
solution, as disk write is relatively slow. But unless you are
anticipating a lot of cocurrent access, it's adequate.

Try the shared memory functions as suggested by others as well. I'm a
little skeptical, as you can't use shared memory safely without
synchronization objects, which as far as I know aren't available in
PHP.

Zeppelin
Guest
 
Posts: n/a
#13: Dec 29 '05

re: Mantain a variable through several scripts like global.asa variables in asp


> Won't work. Detecting the user's real ip only works if they are[color=blue]
> connecting directly to you or using a proxy which forwards the real IP
> address. Most proxies, especially corporate ones, are set up to not
> forward internal ip addresses. All you'll get is the proxy address -
> which may be shared by multiple users (as is the case in my company).[/color]

I know, and we will try do the best on this, but the effects of having
only proxy ips are not critical, our network will go slowly.
[color=blue]
> So, instead of determining where the slowdown is, you're grasping at
> straws? Spending a lot of time trying to code something which could
> crash the server and may or may not resolve the problem, instead of
> determining the cause of the problem?[/color]

The needed code is trivial, trust me, only an array mantained through
client connections.
[color=blue]
> I would think offloading some of the work would be more efficient. For
> instance, if you're supplying a download, have another server actually
> perform the download. Using a database? Put the database on a
> different server. And so on.[/color]

We are working on migrating other unrelated services to another
machines.

--------
I have now a beta version of the script using shared memory working,
thank you all for your time. I will be watching this thread, it could
be that anyone wanted know more about it, or if you liked discussing
more.

Thank you all ;)

Closed Thread