猿问

将变量传递给另一个 PHP 文件并执行它

我想发送一个我在一个 PHP 文件中接收到的变量,该变量是从 Android APP 发送的相对于 mysqli 查询,并根据接收到的变量执行第二个 PHP 文件。例如:


在“file1.php”中,我收到了从 Android APP 发送的两个变量:


$month= $_REQUEST['month'];

$year= $_REQUEST['year'];

然后,我想将这两个变量发送到“file2.php”并对其执行查询。我不需要从'file2.php' 接收任何信息,只需执行其中的查询,将一些数据记录到Mysql 数据库中。


我在 file1.php 中使用了一个包含命令,例如


include 'file2.php'; 

效果很好,但我必须在“file2.php”中对变量进行硬编码,例如


$month= '5';

$year = '2019';

我想要的是发送我在 file1.php 中收到的变量


$month= $_REQUEST['month'];

$year= $_REQUEST['year'];

并将其重新发送到 file2.php


心有法竹
浏览 123回答 2
2回答

POPMUISE

<?php$value="this value";include 'file2.php';&nbsp;include 'file1.php';&nbsp;这$value将对两个类都可见。所以只需使用file2.php两者中的变量就已经是可见的file2.php

蝴蝶不菲

SESSIONS 是这里的另一个选项,会话变量可用于跨页面访问这些变量。<?php&nbsp; &nbsp; // file1.php&nbsp; &nbsp; ob_start();&nbsp; &nbsp; session_start();&nbsp; &nbsp; $month= $_REQUEST['month'];&nbsp; // check for sql injections and XSS&nbsp; &nbsp; $year= $_REQUEST['year'];&nbsp; // check for sql injections and XSS&nbsp; &nbsp; $_SESSION['month'] = $month;&nbsp; &nbsp; $_SESSION['year'] = $year;?>删除硬编码值并替换为会话,<?php&nbsp; &nbsp; ob_start();&nbsp; &nbsp; session_start();&nbsp; &nbsp; //file2.php&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; remove hardcoded values and replace with&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $month= '5';&nbsp; &nbsp; &nbsp; &nbsp; $year = '2019';&nbsp; &nbsp; */&nbsp; &nbsp; $month= $_SESSION['month'];&nbsp; &nbsp; $year = $_SESSION['year'];&nbsp; &nbsp; /*&nbsp; &nbsp; your logic goes blow&nbsp; &nbsp; */?>
随时随地看视频慕课网APP
我要回答