简体   繁体   English

在两个 php 页面之间传递变量的最佳方法

[英]best way to pass a variable between two php pages

Need to pass a variable from index.php to edit.php .需要将一个变量从index.php传递给edit.php

The variable should be unvisible for visitors, so address bar is not allowed.该变量应该对访问者不可见,因此不允许使用地址栏。

First way - using a form on index.php with action attribute - edit.php .第一种方式 - 在index.php使用带有action属性的表单 - edit.php

Problem with using a form is a warning about re-submitting which appears each time edit.php is refreshed.使用表单的问题是每次刷新edit.phpedit.php出现有关重新提交的警告。

Second way - using ajax call on index.js to change a session variable on php side.第二种方式 - 在index.js上使用 ajax 调用来更改 php 端的会话变量。

Downside of using ajax is an extra path - firstly go to server side (to change php) - then to client side (success function to get the new value) - and again to server side (go to edit.php using the new variable).使用 ajax 的缺点是一条额外的路径 - 首先转到server side (更改 php) - 然后到client side (成功函数获取新值) - 然后再到服务器端(使用新变量转到edit.php ) .

What is the right way?什么是正确的方法?

Is there a simple way like this:有没有这样的简单方法:

location.href = 'edit.php', set x = 5

And on edit.php to get this:edit.php上得到这个:

echo $x (result 5); echo $x (结果 5);

You could create a custom cookie in index.js and append to document.cookie and when a request to edit.php is made, it will be available.您可以在index.js创建一个自定义 cookie edit.php加到document.cookie ,当对edit.php发出请求时,它将可用。

index.js索引.js

const custom_cookie = "x=5";
document.cookie = custom_cookie;
.....
window.location = 'edit.php';

edit.php编辑.php

$cookie_name = 'x';
echo $_COOKIE[$cookie_name]

Another way is to use PHP Sessions.另一种方法是使用 PHP Sessions。

//index.php
$_SESSION['varname'] = $var_value;

//edit.php
$var_value = $_SESSION['varname'];

Remember to run the session_start() ;记得运行session_start() statement on both these pages before you try to access the $_SESSION array, and also before any output is sent to the browser.在您尝试访问 $_SESSION 数组之前,以及在将任何输出发送到浏览器之前,在这两个页面上的语句。

If the parameter must remain invisible in the UI (ie, not in the address bar) and you need to avoid POST requests due to page reloading, you can achieve this with two requests...如果参数必须在 UI 中保持不可见(即不在地址栏中)并且您需要避免由于页面重新加载而导致 POST 请求,则可以通过两个请求来实现...

Either one AJAX POST then a GET, ie一个 AJAX POST 然后一个 GET,即

fetch('set-session-var.php', {
  method: 'post',
  body: 'x=5',
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  }
}).then(res => {
  if (res.ok) {
    window.location = 'edit.php'
  }
})

or use a Post / Redirect / Get pattern, eg或使用Post / Redirect / Get模式,例如

<form action="set-session-var.php" method="post">
  <!-- set the "x" value however and whenever you want -->
  <button type="submit" name="x" value="5">Go</button>
</form>
// set-session-var.php
session_start();
$_SESSION['x'] = $_POST['x'];
header('Location: edit.php'); // omit this if using the AJAX version

Use Cookie...使用饼干...

index.php:索引.php:

setcookie("x", 5);

edit.php:编辑.php:

echo $_COOKIE["x"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM