473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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

Dec 27 '05 #1
12 1809
Zeppelin wrote:
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).
Hi,

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


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

Just include a file.

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?
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

thanks


Dec 27 '05 #2
"Erwin Moller"
<si******************************************@spam yourself.com> wrote in
message news:43***********************@news.xs4all.nl...
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.


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

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
an*******************@gmail.com.NOSPAM.invalid
Dec 27 '05 #3
Kimmo Laine wrote:
"Erwin Moller"
<si******************************************@spam yourself.com> wrote in
message news:43***********************@news.xs4all.nl...
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.


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


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

Regards,
Erwin Moller

Dec 27 '05 #4
>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).
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?
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?


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
Dec 28 '05 #5
Zeppelin wrote:
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?


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.

Dec 28 '05 #6
> Have you actually MEASURED the performance to prove this?
Often, even program code isn't stored in memory most of the time
I have no stadistics about this, but every new version of applicattion,
servers goes 100% and crashes down. So we want be prepared.
If your web server gets restarted (software crash, someone trips
over power cable, maintenance), your variable goes poof! Is this
acceptable behavior?
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) .
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.


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.

Dec 28 '05 #7
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 ;).

Dec 28 '05 #8
> 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.


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.

Dec 28 '05 #9
Zeppelin wrote:
Have you actually MEASURED the performance to prove this?
Often, even program code isn't stored in memory most of the time

I have no stadistics about this, but every new version of applicattion,
servers goes 100% and crashes down. So we want be prepared.


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.
js*******@attglobal.net
==================
Dec 28 '05 #10
Zeppelin wrote:
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.

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.


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.
js*******@attglobal.net
==================
Dec 28 '05 #11
Zeppelin wrote:
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


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.

Dec 29 '05 #12
> 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).
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.
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?
The needed code is trivial, trust me, only an array mantained through
client connections.
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.


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 ;)

Dec 29 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Kasper K | last post by:
Hi, I'm using PHP with apache. From what I understand, global variables are only global to one php script (i.e. the file and all the files it includes), but they are not persisted between...
83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
5
by: Richard A. DeVenezia | last post by:
Dear Experts: Suppose I have global variables: x1, x2, x3 and I have a function that needs to assign a value to a new global variable x4 something like function foo () { count = 0;
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
6
by: Maarten | last post by:
Here is a nice problem. I have two scripts, one calling the other with via an include. The script that is included contains a variable, a function and a call to that function. The variable needs to...
148
by: onkar | last post by:
Given the following code & variable i . int main(int argc,char **argv){ int i; printf("%d\n",i); return 0; } here i is allocated from bss or stack ?
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
34
by: Kurda Yon | last post by:
Hi, As you have recommended I did not use the session_register(). In one file I have executed such line: $_session = 2.0; Then, in another file, I have executed the following line: $ex =...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.