繁体   English   中英

应该使用什么来在 HTML 和 PHP 中的页面之间共享变量?

[英]What should be use to share variables between pages in HTML and PHP?

我需要创建一个集中页面,其中设置多个页面中所需的所有变量。 例如:

集中。php

$displayPath = "/var/www/html/wordpress/";

然后,我需要跨服务器的 html 和 php 页面才能访问它。 例如:

第1页.html

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
</body>
</html>

<?php
echo ($displayPath);
?>

page2.php

<?php
echo ($displayPath);
?>

我正在考虑使用 window.localStorage,但是这样做时,如何将 JS 变量中的值传递给 php 变量。

如果您只想从不同的页面读取变量,您可以创建一个包含所有变量的文件:

myVariables.php

<?php
    $displayPath = "/var/www/html/wordpress/";
    $myglobalvariable="hello";
?>

然后将它包含在需要它的每个页面中。

第1页.php

<?php
    require_once("./myVariables.php");    
?>

注意:这将不允许您在一个页面中更改变量,然后在另一页面中访问修改后的变量。 如果这是你想要的,你可以看看这个:

如何跨两个文件访问变量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM