将 PHP 中的变量从一个文件传递到另一个文件 [英] Passing variables in PHP from one file to another

查看:45
本文介绍了将 PHP 中的变量从一个文件传递到另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.现在我如何在另一个文件中使用 $pubname.

So this is my code. Now how do I use $pubname in another file.

mysqli_select_db($connect,"membership");
$retname = "select username from users where email='$globalname' limit 1";
$rn = mysqli_query($connect,$retname) or die(mysqli_error($connect));
$name = mysqli_fetch_array($rn);
    //connecting for mathcing username with fullname and displaying it
$pubname = mysqli_real_escape_string($name['username']);

include('profile.php');

echo $pubname;

还有这个代码安全吗?我这样做了......还没有工作.

and also is this code secure? I did that...does not work yet.

推荐答案

包含您希望变量可在其中访问的文件,就像这样

Include the file you would like the variable to be accessible within, like so

include('somefile.php')

并且在该文件的顶部您可能需要放置类似[取决于服务器配置]

and at the top of that file you might need put something like [depending on server configurations]

global $pubname

但在大多数情况下,您不需要这样做.

But in most cases you would not need to do this.

关于安全性,根据 $pubname 的设置方式,您的查询可能会或可能不会容易受到 sql 注入.

In regards to security, depending on how $pubname is set, your query may or may not be prone to sql injection.

注意:还有其他方法可以include() 文件,例如include_once()require()require_once(),来自 php.net:

Note: There are other means to include() files such as include_once(), require() and require_once(), from php.net:

以下文档也适用要求().这两个构造是除了他们如何处理失败.include() 产生一个警告而 require() 导致致命错误.换句话说,使用require() 如果你想要一个丢失的文件停止处理页面.include() 不会这样,无论如何,脚本将继续.一定要有合适的include_path 设置也是如此.是警告需要解析错误文件不会导致处理停止在 PHP 4.3.5 之前的 PHP 版本中.从这个版本开始,它确实如此.

The documentation below also applies to require(). The two constructs are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well. Be warned that parse error in required file doesn't cause processing halting in PHP versions prior to PHP 4.3.5. Since this version, it does.

这篇关于将 PHP 中的变量从一个文件传递到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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