简体   繁体   English

在PHP中的脚本之间传递变量

[英]Passing Variables Between Scripts in PHP

I have a simple question, with maybe not so simple of an answer. 我有一个简单的问题,答案可能不是那么简单。 I want to be able to set a variable in one script and pass that value and variable to another script. 我希望能够在一个脚本中设置一个变量并将该值和变量传递给另一个脚本。 Without passing it through the url, and having the ability to pass an array. 无需通过url传递,并具有传递数组的能力。

So I have index.php in there I have a variable 所以我那里有index.php我有一个变量

<?php
$myvariable = '';
<form action=editRecord.php>
do some form actions here and submit moving to editRecord.php
</form>
?>

Now in editRecord.php 现在在editRecord.php中

<?php
header('Location: go back to index.php);
run functions and edit the Mysql DB
//Ok now here is where I want to assign a value to $myvariable and pass it back to index.php
?>

Is this possible? 这可能吗? I am sorry for the amateurish question, but I am very green when it comes to php. 对于业余问题,我感到很抱歉,但是当涉及到php时,我非常环保。

Thank You. 谢谢。

you can set it in the $_SESSION variable: 您可以在$_SESSION变量中进行设置:

<?php
session_start();
$_SESSION["myVar"] = "blabla";
....

Of course you can store an array() in this variable too. 当然,您也可以在此变量中存储array()

Just pass that information in the querystring. 只需在查询字符串中传递该信息即可。 Then you can access it via $_GET . 然后,您可以通过$_GET访问它。 If it doesn't exist, just set the value to an empty string or whatever default value you want it to have. 如果不存在,则将值设置为空字符串或您希望它具有的任何默认值。

// editRecord.php
<?php
// do db dtuff
header('Location: index.php?myvariable=somevalue');
?>

// index.php
<?php
$myvariable = (isset($_GET['myvariable'])) ? $_GET['myvariable'] : '';
<form action="editRecord.php" >

</form>
?>

You can also use session variables: 您还可以使用会话变量:

// editRecord.php
<?php
session_start();
// do db stuff
$_SESSION['myvariable'] = 'somevalue';
header('Location: index.php');
?>

// index.php
<?php
session_start();
$myvariable = (isset($_SESSION['myvariable'])) ? $_SESSION['myvariable'] : '';
<form action="editRecord.php" >

</form>
?>

您可以使用会话,POST数据,GET数据,cookie和数据库数据。

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

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