简体   繁体   English

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

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

I need to create a centralized page where all the variables that are needed in more than one page get set.我需要创建一个集中页面,其中设置多个页面中所需的所有变量。 For example:例如:

centralized.php集中。php

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

Then, I need html and php pages across the server to have access to it.然后,我需要跨服务器的 html 和 php 页面才能访问它。 For example:例如:

page1.html第1页.html

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

<?php
echo ($displayPath);
?>

page2.php page2.php

<?php
echo ($displayPath);
?>

I was looking at using window.localStorage, but when doing so, how could pass the value from JS variables to php variables.我正在考虑使用 window.localStorage,但是这样做时,如何将 JS 变量中的值传递给 php 变量。

If you only want to read the variables from different pages, you can create a file with all of your variables:如果您只想从不同的页面读取变量,您可以创建一个包含所有变量的文件:

myVariables.php myVariables.php

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

And then include it in every page that needs it.然后将它包含在需要它的每个页面中。

page1.php第1页.php

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

NOTE : This will not allow you to change the variables in one page and then access the modified variables in another page.注意:这将不允许您在一个页面中更改变量,然后在另一页面中访问修改后的变量。 If that's what you want, you can take a look at this:如果这是你想要的,你可以看看这个:

How to access a variable across two files 如何跨两个文件访问变量

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

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