在php中的函数和文件之间传递变量

在php中的函数和文件之间传递变量

本文介绍了在php中的函数和文件之间传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个名为 admin.php 的文件,其中有一个名为 send 的按钮.我想要做的是当我点击它时,使用户页面上的链接可见,user.php.我该怎么做?

I have a file called admin.php in which I have a button with the name send. What I want to do is when I click it, to make visible a link on the user's page, user.php. How can I do this?

我有一个名为functions.php的文件,其中包含一个名为onSubmit($var)的函数;我用值 $_POST['send'] 初始化变量 $var is admin.php 但是当我调用文件 user.php 中的函数时,我无法告诉他变量 $var 是谁,所以我得到 'undefined索引'.

I have a file with all my functions called functions.php in which I have a function called onSubmit($var); I initialize the variable $var is admin.php with the value $_POST['send'] but when I call the function in the file user.php I have no way of telling him who the variable $var is so I get 'undefined index'.

还有其他方法可以做到这一点吗?

Is there another way to do this?

编辑添加的代码这是 admin.php

EDIT Added codeThis is admin.php

<input type="button" name="send" value="Submit" /><br/>

require 'functions.php';
$posted = $_POST['send'];
onSubmit($posted);

这是 user.php

This is user.php

require 'functions.php';
onSubmit($var);  //here it says undefined index var because it doesn't know who the variable is
if($isSent == 1) {

<a style="visibility:visible;" href="test3.html" id="test3">Test3</a> <br/>

}

这是functions.php

And this is functions.php

global $isSent;
function onSubmit($var) {
if(isset($var)) {
$isSent = 1;
}
}

推荐答案

是的,我对缓存做了一些研究,这就是我想出的.这可能不是 100% 正确,但这是一个开始,就像我说过我自己从未尝试过一样,哈哈

Right I've done a bit of research on Caching and this is what I've come up with. It might not be 100% correct but it's a start as like I've said I've never tried it myself lol

在您的 admin.php 中,我将此函数放入:

In your admin.php I'd put this function in:

if(isset($_POST['send'])){
    if($enabled == true){
        $enabled == false;
    }
    else{
        $enabled == true;
    }
     apc_add('enabled',$enabled);
}

现在获取"我们的 $enabled 变量:

Now to 'get' our $enabled var:

$enabled = apc_fetch('enabled');

然后检查您的客户端页面中的变量:

Then to check the the var within your client page:

if($enabled == true){
    echo ' button';
}

现在我唯一没有完全了解的是apc_ 函数的安全性和客户端使用.我相信它适用于服务器的所有客户端,但我不是 100% 确定.这里php手册给出了更好的例子.

Now the only things I haven't fully looked at is the security of the apc_ function and the client usage. I believe it works for all clients of the server but I'm not 100% certain. Here the php manual to give better examples.

这是我想到的方法.但同样,我不确定它的安全性,但我相信你可以找到一些东西来保证它的安全.该视频实际上是 Youtube API 的教程.但他确实涵盖了将变量保存到缓存文本文件中,该文件应该对您有用:)

This is the method I was thinking of. But again I'm not sure on the security of it but I'm sure you can find something to keep it secure. The video is actually is tutorial for a Youtube API. But he does cover saving a variable to a cache text file which should be of use to you :)

这篇关于在php中的函数和文件之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 12:26