如何在没有表单,链接或按钮的情况下从一个PHP页面访问变量到另一个页面?
TLDR:- What is a good way to pass contents of a variable
from one PHP file
to another without involving a form
, link
or a button
.
Question:-
So there is a form
in a page/file called question_edit_form.php
and its action
attribute is already set to another file called question.php
. The variable of interest is being read-in from the user in question_edit_form.php
and is then obviously being sent to question.php
using $_POST
.
Now, there is a third file, named renderer.php
, and which is not linked to the other two files. I want to use that variable of interest in this file. So how can I access that variable which is set in question.php from inside renderer.php?
TLDR: - strong>传递 问题: - strong> p>
因此在名为 现在,有一个名为变量内容的好方法是什么 code>从一个
PHP文件 code>到另一个
表单 code>,
link code>或
button code>。 p>
表单 code> > question_edit_form.php code>及其
action code>属性已设置为另一个名为
question.php code>的文件。 感兴趣的变量正在用户在
question_edit_form.php code>中读入,然后使用
$ _ POST question.php code> /code>.
renderer.php code>的第三个文件,它没有链接到其他两个文件。 我想在此文件中使用该感兴趣的变量。 em> 那么如何从renderer.php中访问question.php中设置的变量? strong> p>
div>
first file -
session_start();
$_SESSION['your_variable'] = 'value';
other file -
session_start();
$var = $_SESSION['your_variable'];
this may help.
Generally there are two methods available for you to pass on the value
Cookies
Sessions
How to use cookies:-
setcookie(name, value, expire);
e.g.
setcookie("user", "Alex Porter", time()+3600);
Access it using echo $_COOKIE['user'];
Second is Sessions. Here is how to use sessions:-
session_start();
$_SESSION['varname']=value;
Accessing page:-
session_start();
echo $_SESSION['varname'];
Additional info if required:-
Make sure you use session_start() at top of your page if not you may face with an headers already sent error / warning which again can be fixed by output buffering ob_start()
It sounds like you are using Moodle, in which case renderer.php
is not an independent file; it contains the class definition for the renderer object used by question.php
.
So... there is no need to pass the parameter between the scripts. If you really must access the form value directly from the renderer, just use the standard methods from the Moodle framework: required_param($name, $type)
or optional_param($name, $default, $type)
.