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

查看:239
本文介绍了如何跨两个文件访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个文件-global.php, test.phptest1.php

Global.php

Global.php

$filename;
$filename = "test";

test.php

$filename = "myfile.jpg";
echo $filename;

test1.php

test1.php

echo $filename;

我可以通过以下方式从test和test1文件中读取此变量: include 'global.php';

I can read this variable from both test and test1 files by include 'global.php';

现在我想在test.php中设置$filename的值,并在test1.php.

Now i want to set the value of $filename in test.php and the same value i want to read in test1.php.

我也尝试使用会话变量,但是由于两个不同的文件,我无法捕获该变量.

I tried with session variables as well but due to two different files i am not able to capture the variable.

如何实现这一目标........

How to achieve this........

谢谢您的帮助.....

Thanks for help in advance.....

推荐答案

使用:

global.php

global.php

<?php
if(!session_id()) session_start();
$filename = "test";
if(!isset($_SESSION['filename'])) {
    $_SESSION['filename'] = $filename;
}
?>

test.php

<?php
if(!session_id()) session_start();
//include("global.php");
$_SESSION['filename'] = "new value";
?>

test1.php

test1.php

<?php
if(!session_id()) session_start();
$filename = $_SESSION['filename'];
echo $filename; //output new value
?>

这篇关于如何跨两个文件访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆