English 中文(简体)
php java in memory database
原标题:

i need to load data as array to memory in PHP.but in PHP if i write $array= array("1","2"); in test.php then this $array variable is initialized every time user requests.if we request test.php 100 times by clicking 100 times browser refresh button then this $array variable will be executed 100 times.

but i need to execute the $array variable only one time for first time request and subsequent request of test.php must not execute the $array variable.but only use that memory location.how can i do that in PHP.

but in JAVA SEVRVLET it is easy to execute,just write the $array variable in one time execution of init() method of servlet lifecycle method and subsequent request of that servlet dont execute init() method but service() method but service() method always uses that $array memeory location.

all i want to initilize $array variable once but use that memory loc from subsequent request in PHP.is there any possiblity in PHP?

问题回答

PHP works differently than a Java Servlet container. Every new request basically starts an entirely new instance of the PHP interpreter, therefore you don t have a global adress space across requests (you do have a session per user which gets usually persisted to a file to keep variables across requests for one user).

A thing that might come close to it would be to use memcached with PHP as your "database", but you will have to send a request to the memcached server every time you need your array. That is why I think your array (if it doesn t change) is best kept and initialized in the PHP file.

use session

start the session when user opens test.php and set the array in that session

<?php
session_start();

if(!isset($_SESSION[ user_action ])){ $_SESSION[ user_action ] = array("1","2"); } ?>

That code will just verify if the session variable "user_action" is set, if it isn t then it will set with that array.

Then you can change that variable later.

All variables are destroyed at request shutdown, there is no built-in mechanism to do what you want in php.

PHP has different kind of execution.
In general, it s impossible in PHP and it s OK.

You can try the following:

<?php
/* test.php */
if (empty($GLOBALS[ array ])) {
    $GLOBALS[ array ] = array("1", "2");
}
?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签