【问题标题】:What should be use to share variables between pages in HTML and PHP?应该使用什么来在 HTML 和 PHP 中的页面之间共享变量?
【发布时间】:2021-03-20 01:16:48
【问题描述】:

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

centralized.php

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

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

page1.html

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

<?php
echo ($displayPath);
?>

page2.php

<?php
echo ($displayPath);
?>

我正在研究使用window.localStorage,但是这样做时,如何将值从JS变量传递给php变量。

【问题讨论】:

    标签: javascript php html


    【解决方案1】:

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

    myVariables.php

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

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

    page1.php

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

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

    How to access a variable across two files

    【讨论】:

      猜你喜欢
      • 2013-04-22
      • 1970-01-01
      • 2014-08-29
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      相关资源
      最近更新 更多