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

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

问题描述

我有一个网站,其中有很多用户,并且有一个用于编辑用户的部分.我们将此部分页面称为A.php".单击此页面A"时,您将看到以表格形式在站点中注册的所有用户的列表.然后,有一个用于输入您要编辑的用户名的输入空间,这是一种 HTML 表单方法 (POST),用于验证信息,如果用户名不存在,则会出现错误页面.

I have a site in which there are many users, and there's a section for editing them. Let's call this section page 'A.php'. When clicking on this page 'A', you get to see a list of all the users registered in the site in form of a table. Then, there's an input space for entering the username you want to edit, this is an HTML form method (POST), that verifies the information and if the username doesn't exist, you get an error page.

但如果是这样,则会打开一个新页面,让我们将此页面称为B.php",您将看到其完整信息,以及更多输入空间,以防您想更改某些用户值.有一个用于保存更改的选项(页面C.php")和一个用于删除用户的选项(页面D.php").

But if it does, a new page is opened, let's call this one page 'B.php' and you get to see its full info, as well as more input spaces in case you want to change some of the user values. There's an option for saving changes (page 'C.php') and an option for deleting the user (page 'D.php).

我现在遇到的问题是,因为在页面A.php"中我通过表单方法=POST"发送信息,后来在页面B.php"中收到,我不知道如何将其重新发送到页面C.php"和D.php",因为这些页面不会从表单方法 =POST"接收任何数据.我尝试使用 include 功能,但它不起作用,有人可以帮我吗?

The problem I have right now is that, since in page 'A.php' I send the info via a form method="POST" and it is later received in page 'B.php', I don't know how to re send it to pages 'C.php' and 'D.php', since these pages don't receive any data from a form method="POST". I tried using the include function but it doesn't work, can anybody help me please?

以下是我将信息从页面A"发送到B"的方式:

Here's how I'm sending the info from page 'A' to 'B':

页面 A:

<form method="POST" action="edit_usr.php">
    <font size = "5">Username: <input type="text" name="username" />
    <input type="submit" name="submit" value="EDIT USER" /></font>
</form>

这是页面 B (edit_usr.php) 接收代码的方式:

This is how page B (edit_usr.php) receives the code:

$usr_mod = $_POST['username'];

我认为我唯一需要做的就是将变量 $usr_mod 导出到页面 'C' 和 'D',但我已经在这个页面和 PHP 手册上找到了它,但我找不到任何事物.有人可以帮我吗?伙计们真的很感激!:)

I assume the only thing I need to do is to export the variable $usr_mod to pages 'C' and 'D', but I've already looked for it on this page and the PHP manual and I could't find anything. Can anybody please help me? It would be really appreciated guys! :)

推荐答案

正如评论中提到的,会话可能是这里的方式.

As the comment mentioned, sessions are probably the way to go here.

  1. edit_usr.php的顶部,写上session_start();.
  2. 在页面C和页面D的顶部,也写session_start();.
  3. edit_usr.php 中,写入$_SESSION['usr_mod'] = $usr_mod;.或者,您可以只编写 $_SESSION['usr_mod'] = $_POST['username']; 并使用 $_SESSION['usr_mod'] 代替 $usr_mod 但这是一个偏好问题.
  4. 在页面 C 和页面 D 中,只需使用 $_SESSION['usr_mod'] 访问变量.
  1. At the top of edit_usr.php, write session_start();.
  2. At the top of page C and page D, also write session_start();.
  3. Inside edit_usr.php, write $_SESSION['usr_mod'] = $usr_mod;. Alternately, you can just write $_SESSION['usr_mod'] = $_POST['username']; and just use $_SESSION['usr_mod'] in place of $usr_mod but that's a matter of preference.
  4. Inside page C and page D, just use $_SESSION['usr_mod'] to access the variable.

当用户注销时,请确保销毁会话.

When the user logs out, make sure you destroy the session.

session_unset();
session_destroy();

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

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