在 PHP 中跨脚本读写全局变量

发布于 2024-08-04 18:53:30 字数 42 浏览 5 评论 0原文

PHP 是否具有可由一个运行脚本修改并由另一个运行脚本读取的全局变量?

Does PHP have global variables that can be modified by one running script and read by another?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

黑色毁心梦 2024-08-11 18:53:30

不,从设计上来说,PHP 是一种“无共享”架构,这意味着同时运行的进程之间或相继运行的请求之间不会共享任何内容。有多种方法可以共享数据,但您必须明确地执行。

如果您只想在同一用户的 2 个请求之间共享,则会话或 cookie 可能是最佳选择。

如果您想在多个用户之间共享,您可能需要某种共享持久性,无论是短期的缓存(例如 memcached)还是更强大的数据库。

无论哪种方式,数据实际上都是根据每个请求来检索和重建的。它只是在会话的情况下自动为您处理。

No, by design PHP is a "share nothing" architecture, which means nothing is shared between processes running at the same time or between requests running one after another. There are ways to share data, but you have to do it explicitly.

If you just want to share between 2 requests from the same user, sessions or cookies might be the way to go.

If you want to share between multiple users, you probably want some sort of shared persistence, either short term in a cache (eg. memcached) or more robust like a database.

Either way, the data is actually being retrieved and reconstructed on each request. It's just handled automatically for you in the case of sessions.

手心的温暖 2024-08-11 18:53:30

实际上,您可以使用 共享内存APC (它本身使用共享内存)。

You can actually do this using shared memory, or APC (which is using shared memory itself).

冰葑 2024-08-11 18:53:30

您可以使用$_SESSION,即:

script1.php

<?php
session_start();
$_SESSION['myVar'] = "something";
?>

script2.php

<?php
session_start();
echo $_SESSION['myVar'];
//something
?>

You can use $_SESSION, i.e.:

script1.php

<?php
session_start();
$_SESSION['myVar'] = "something";
?>

script2.php

<?php
session_start();
echo $_SESSION['myVar'];
//something
?>
随心而道 2024-08-11 18:53:30

唯一可以在脚本之间访问的是超全局 $_SESSION 数组。这是因为您存储在数组中的任何内容都会发送到 cookie,然后下一个 PHP 脚本可以获取该 cookie。

全局变量只是意味着它们可以在脚本中访问,无论范围如何;这并不意味着它们可以在脚本之间发送。

因此,要么您必须使用 $_SESSION 数组(这在客户端计算机上存储 cookie,因此不要通过该数组发送任何敏感信息)传输变量,要么您可以在脚本之间使用 POST 或 GET 来发送变量。

The only one which could be accessed between scripts is the superglobal $_SESSION array. This is because whatever you store in the array is sent to a cookie, which can then be picked up by the next PHP script.

Global variables simply mean that they can be accessed in the script regardless of the scope; that doesn't mean they can be sent between scripts.

So either you have to transfer the variables using the $_SESSION array (this stores a cookie on the client computer, so don't sent any sensitive information through that array) or you can either POST or GET between the scripts to send the variables.

若言繁花未落 2024-08-11 18:53:30

每个请求都由它自己的 php 实例处理。 php 中的全局变量只能从同一个 php 实例中访问。但是,您可以使用 memchached 模块 之类的东西在不同实例之间共享数据(通常应该比将数据写入文件系统)。

Each request is handled by a php instance of its own. Global variables in php are only accessible from within the same php instance. However you can use something like the memchached module to share data between different instances (which should usually be faster than writing the data to the filesystem).

酒儿 2024-08-11 18:53:30

并非如此,但您可以使用 cookie 或会话在用户浏览体验期间维护数据,或者如果信息需要在该时间之外保留,您可以写入数据库或磁盘上的文件。

Not as such, but you can use cookies or sessions to maintain data for duration of a user's browsing experience, or you can write to a database or file on-disk if the information needs to persist beyond that.

带刺的爱情 2024-08-11 18:53:30

PHP 中全局变量的另一种常见替代是共享使用 MySQL 等数据库(尽管不是完美的)

Another common substitution for global variables in PHP is the shared use of a database like MySQL (albeit not a perfect one)

酒浓于脸红 2024-08-11 18:53:30

全局变量在大多数编程中都是不好的。它们在像网络应用程序这样的多线程/多用户系统中尤其糟糕。避免。如果必须使用全局变量(而不是全局常量),请将它们放入具有事务保护更新的数据库中。

由于您谈论的是不同的脚本,听起来您真正想要的是采用更面向应用程序的语言的 Web 应用程序框架——例如 Django (python) 或 Rails (ruby)。这些让您将代码视为一个有凝聚力的程序,而不是许多处理 Web 请求的松散连接的脚本。

Global variables are bad in most programming. They're especially bad in multithreaded/multiuser systems like webapps. Avoid. If you must use global variables (rather than global constants) put them in a database with transactions guarding updates.

Since you talk about different scripts though, it sounds like what you really want is a web application framework in a more application oriented language --- something like Django (python) or Rails (ruby). These let you think of your code much more like a cohesive PROGRAM, rather than a lot of loosely connected scripts that process web requests.

久随 2024-08-11 18:53:30

我制作了一个小型库(~2 KB;<100 行),允许您执行以下操作:varDx

具有写入、读取、修改、检查、删除数据的功能。
它实现序列化,因此支持所有数据类型。

使用方法如下:

<?php
require 'varDx.php';
$dx = new \varDx\cDX; //create an object
$dx->def('file.dat'); //define data file

$val1 = "this is a string";
$dx->write('data1', $val1); //writes key to file
echo $dx->read('data1'); //returns key value from file

I made a tiny library (~2 KB; <100 lines) that allows you to do just this: varDx

It has functions to write, read, modify, check and delete data.
It implements serialization, and therefore supports all data types.

Here's how you can use it:

<?php
require 'varDx.php';
$dx = new \varDx\cDX; //create an object
$dx->def('file.dat'); //define data file

$val1 = "this is a string";
$dx->write('data1', $val1); //writes key to file
echo $dx->read('data1'); //returns key value from file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文