[英]how to access the variable to the page without using GET and POST methods and without using header function?
Below are two files first.php
and second.php
I want to access the $name
variable in the second file. 以下是我要访问第二个文件中的
$name
变量的两个文件first.php
和second.php
。 I used global but it just access values inside the file. 我使用了global,但是它只是访问文件中的值。 I do not want to use
POST
and GET
method because I used POST
already for redirecting to home.php
我不想使用
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 first.php
$name = "New York";
second.php second.php
include "path/to/first.php";
echo $name; //echo "New York"
Here is the manual for PHP: Include 这是PHP手册:Include
If you don't want everything from first.php on second.php, you should use sessions. 如果您不想从second.php上的first.php中获取所有内容,则应使用会话。
first.php first.php
session_start(); //start sessions, so you can use session variables
$_SESSION['name'] = "New York"; //set session variable called "name" to "New York"
second.php second.php
session_start(); //start session so you can use session variables
echo $_SESSION['name']; //echo "New York"
Session variables work basically the same as regular variables, but you access them like an array. 会话变量的工作原理与常规变量基本相同,但是您可以像访问数组一样访问它们。 You have to start the session on every page to access them.
您必须在每个页面上启动会话才能访问它们。 I usually just start sessions in my header file so it's always included.
我通常只是在头文件中开始会话,因此它总是包含在内。
More info about PHP sessions 有关PHP会话的更多信息
You could also use cookies, though I recommend using SESSIONS instead in most cases. 您也可以使用cookie,尽管在大多数情况下我建议使用SESSIONS。 Cookies are good for when that variable needs to last through multiple log in sessions or for a really long time, I usually use these for users settings themes in my application and such things that don't change often.
当该变量需要在多个登录会话中持续很长时间或非常长时间时,Cookie非常有用,我通常将其用于用户设置应用程序中的主题以及此类内容不会经常更改。
first.php first.php
$name = "New York"; //set variable
setcookie("name", $name, time() + (86400 * 30), '/'); //set cookie that expires in 1 day
seconds.php seconds.php
echo $_COOKIE['name']; //echo New York
Try to use $_SESSION variable. 尝试使用$ _SESSION变量。
<?php //first.php
session_start();
$_SESSION['name'] = 'New York';
?>
<?php //second.php
session_start();
echo $_SESSION['name'];
?>
The best and safe way so far is using include_once()
. 到目前为止,最好和安全的方法是使用
include_once()
。 If you want to include many files then you may use __autoload()
(if it is object orient approach) . 如果要包含许多文件,则可以使用
__autoload()
(如果它是面向对象的方法) 。
$_REQUEST['var'];
This adapt with both post and get. 这与发布和获取都适应。 Doesn't Metter what you are using.
不会告诉您您正在使用什么。 Or with
include 'file.php'
Or with setcookie('name', 'value'); $_COOKIE['name'];
或带有
include 'file.php'
或带有setcookie('name', 'value'); $_COOKIE['name'];
setcookie('name', 'value'); $_COOKIE['name'];
Or with session storage 或带有会话存储
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.