Php 如何在不使用GET和POST方法以及不使用header函数的情况下访问页面的变量?

Php 如何在不使用GET和POST方法以及不使用header函数的情况下访问页面的变量?,php,Php,下面是两个文件first.php和second.php,我想访问第二个文件中的$name变量。 我使用了全局,但它只访问文件中的值。我不想使用POST和GET方法,因为我已经使用POST重定向到home.php first.php <?php $name = 'New York'; ?> second.php <?php // Access variable here from the above first.php file ?> first.p

下面是两个文件
first.php
second.php
,我想访问第二个文件中的
$name
变量。 我使用了全局,但它只访问文件中的值。我不想使用
POST
GET
方法,因为我已经使用
POST
重定向到
home.php

first.php
 <?php
 $name = 'New York';
 ?>

  second.php
 <?php
  // Access variable here from the above first.php file 
 ?>
first.php
second.php

尝试使用$\u会话变量

<?php //first.php
session_start();
$_SESSION['name'] = 'New York';
?>
<?php //second.php
session_start();
echo $_SESSION['name'];
?>

包括 first.php

$name = "New York";
include "path/to/first.php";
echo $name; //echo "New York"
session_start(); //start sessions, so you can use session variables
$_SESSION['name'] = "New York"; //set session variable called "name" to "New York"
session_start(); //start session so you can use session variables
echo $_SESSION['name']; //echo "New York"
$name = "New York"; //set variable
setcookie("name", $name, time() + (86400 * 30), '/'); //set cookie that expires in 1 day
echo $_COOKIE['name']; //echo New York
second.php

$name = "New York";
include "path/to/first.php";
echo $name; //echo "New York"
session_start(); //start sessions, so you can use session variables
$_SESSION['name'] = "New York"; //set session variable called "name" to "New York"
session_start(); //start session so you can use session variables
echo $_SESSION['name']; //echo "New York"
$name = "New York"; //set variable
setcookie("name", $name, time() + (86400 * 30), '/'); //set cookie that expires in 1 day
echo $_COOKIE['name']; //echo New York
这是你的使用手册


会议 如果您不需要从first.php到second.php的所有内容,那么应该使用会话

first.php

$name = "New York";
include "path/to/first.php";
echo $name; //echo "New York"
session_start(); //start sessions, so you can use session variables
$_SESSION['name'] = "New York"; //set session variable called "name" to "New York"
session_start(); //start session so you can use session variables
echo $_SESSION['name']; //echo "New York"
$name = "New York"; //set variable
setcookie("name", $name, time() + (86400 * 30), '/'); //set cookie that expires in 1 day
echo $_COOKIE['name']; //echo New York
second.php

$name = "New York";
include "path/to/first.php";
echo $name; //echo "New York"
session_start(); //start sessions, so you can use session variables
$_SESSION['name'] = "New York"; //set session variable called "name" to "New York"
session_start(); //start session so you can use session variables
echo $_SESSION['name']; //echo "New York"
$name = "New York"; //set variable
setcookie("name", $name, time() + (86400 * 30), '/'); //set cookie that expires in 1 day
echo $_COOKIE['name']; //echo New York
会话变量的工作原理与常规变量基本相同,但您可以像数组一样访问它们。您必须在每个页面上启动会话才能访问它们。我通常只是在头文件中启动会话,所以它总是包含在内


饼干 您也可以使用cookies,不过我建议在大多数情况下使用会话。Cookie适用于变量需要持续多次登录会话或很长时间的情况,我通常将其用于应用程序中的用户设置主题以及不经常更改的内容

first.php

$name = "New York";
include "path/to/first.php";
echo $name; //echo "New York"
session_start(); //start sessions, so you can use session variables
$_SESSION['name'] = "New York"; //set session variable called "name" to "New York"
session_start(); //start session so you can use session variables
echo $_SESSION['name']; //echo "New York"
$name = "New York"; //set variable
setcookie("name", $name, time() + (86400 * 30), '/'); //set cookie that expires in 1 day
echo $_COOKIE['name']; //echo New York
seconds.php

$name = "New York";
include "path/to/first.php";
echo $name; //echo "New York"
session_start(); //start sessions, so you can use session variables
$_SESSION['name'] = "New York"; //set session variable called "name" to "New York"
session_start(); //start session so you can use session variables
echo $_SESSION['name']; //echo "New York"
$name = "New York"; //set variable
setcookie("name", $name, time() + (86400 * 30), '/'); //set cookie that expires in 1 day
echo $_COOKIE['name']; //echo New York

这与post和get都相适应。不知道你在用什么。 或者使用
包含“file.php”
或者使用
setcookie('name','value')$_曲奇['name']

或者使用会话存储

到目前为止,最好且安全的方法是使用
include_once()
。如果要包含多个文件,则可以使用
\uu autoload()
(如果是面向对象的方法)


使用
包括'first.php'second.php
中的code>没有足够的上下文。所以你想让一个变量凭空出现?但它必须在某个点上有一个值?这是干什么用的?第一次解释是什么时候?什么类型的价值观?关于会话或环境变量,或者从数据库中获取它们呢?尝试使用解释文本/基本原理。谢谢mario!下一次我将解释thenfirst.php有太多的函数,所以我不认为包含文件会很困难safer@ashok我很高兴能帮上忙:)