简体   繁体   English

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

[英]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.phpsecond.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 我不想使用POSTGET方法,因为我已经使用POST来重定向到home.php

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

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

INCLUDE 包括

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


SESSIONS SESSIONS

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会话的更多信息


COOKIES 饼干

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

More information on cookies 有关Cookie的更多信息

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() (如果它是面向对象的方法)

Check the documentation 检查文件

$_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.

相关问题 如何在不使用$ _post的情况下访问同一页面上的文本字段值? - How to access text field value on same page without using $_post? 如何在不使用 $_POST 的情况下将 PHP 中的变量传递到另一个页面? - How to pass a variable in PHP to another page without using $_POST? 如何在不重新加载页面的情况下使用php和codeigniter发布和获取数据 - how to post and get data using php and codeigniter without page reload 重定向页面而不使用标题 - Redirect a page without using the header 使用不带表单的PHP将$ _POST变量发送到另一个页面 - Sending $_POST variable to another page using PHP without forms 如何在不使用“?”的情况下获取查询字符串中的变量 - How to get variable in query string without using “?” How Can i redirect to another PHP Page when i Confirm using Javascript without using PHP header Function - How Can i redirect to another PHP Page when i Confirm using Javascript without using PHP header Function 如何在不刷新页面的情况下使用POST Ajax更新PHP字符串? - How to update PHP string using POST Ajax without page refresh? 如何使用PHP在没有表单的情况下发回到同一页面? - How to post back to same page without a form using PHP? 如何使用ajax检索数据而无需发布页面? Laravel 5 - How to retrieve data using ajax and without going to post page? Laravel 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM