如何使用会话将值从一个 php 页面传递到另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12598133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:53:27  来源:igfitidea点击:

how to pass value from one php page to another using session

phpsessionpost

提问by A Mohammed Hussain

I can pass values form one page to another but I need to pass value like this,

我可以将值从一页传递到另一页,但我需要像这样传递值,

Page 1:

第 1 页:

Page4.php

第4页.php

Page3.php

第3页.php

I need to pass the value in a text field in the Page1.php to a text field in Page2.php, since the form is not directly redirectly to page2, I am unable to pass the value, I tried session, form post method and few other methods but I am yet to succeed.

我需要将 Page1.php 中的文本字段中的值传递给 Page2.php 中的文本字段,因为表单没有直接重定向到 page2,我无法传递该值,我尝试了 session、form post 方法和其他方法很少,但我还没有成功。

I would be very happy if you can help me with the code or some suggestions.

如果您能帮助我提供代码或一些建议,我将非常高兴。

Thanks!

谢谢!

Edit..........

编辑..........

I found the answer, thanks for the help, it was actually a careless mistake on my part, I used $_post instead of $_session.

我找到了答案,感谢您的帮助,这实际上是我的一个粗心大意的错误,我使用了 $_post 而不是 $_session。

Its working now.

它现在工作。

Thanks for the help.

谢谢您的帮助。

回答by Fluffeh

Use something like this:

使用这样的东西:

page1.php

页面1.php

<?php
session_start();
$_SESSION['myValue']=3; // You can set the value however you like.
?>

Any other PHP page:

任何其他 PHP 页面:

<?php
session_start();
echo $_SESSION['myValue'];
?>

A few notes to keep in mind though: You need to call session_start()BEFORE any output, HTML, echos - even whitespace.

不过要记住一些注意事项:您需要session_start()在任何输出、HTML、回声之前调用- 甚至是空格。

You can keep changing the value in the session - but it will onlybe able to be used after the first page - meaning if you set it in page 1, you will not be able to use it until you get to another page or refresh the page.

您可以在会话中不断更改该值 - 但它只能在第一页之后使用 - 这意味着如果您在第 1 页中设置它,您将无法使用它,直到您到达另一个页面或刷新页。

The setting of the variable itself can be done in one of a number of ways:

变量本身的设置可以通过多种方式之一完成:

$_SESSION['myValue']=1;
$_SESSION['myValue']=$var;
$_SESSION['myValue']=$_GET['YourFormElement'];

And if you want to check if the variable is set before getting a potential error, use something like this:

如果您想在出现潜在错误之前检查变量是否已设置,请使用以下内容:

if(!empty($_SESSION['myValue'])
{
    echo $_SESSION['myValue'];
}
else
{
    echo "Session not set yet.";
}

回答by Chris

Solution using just POST - no $_SESSION

仅使用 POST 的解决方案 - 没有 $_SESSION

page1.php

页面1.php

<form action="page2.php" method="post">
    <textarea name="textarea1" id="textarea1"></textarea><br />
    <input type="submit" value="submit" />
</form>

page2.php

页面2.php

<?php
    // this page outputs the contents of the textarea if posted
    $textarea1 = ""; // set var to avoid errors
    if(isset($_POST['textarea1'])){
        $textarea1 = $_POST['textarea1']
    }
?>
<textarea><?php echo $textarea1;?></textarea>


Solution using $_SESSION and POST

使用 $_SESSION 和 POST 的解决方案

page1.php

页面1.php

<?php

    session_start(); // needs to be before anything else on page to use $_SESSION
    $textarea1 = "";
    if(isset($_POST['textarea1'])){
        $_SESSION['textarea1'] = $_POST['textarea1'];
    }

?>


<form action="page1.php" method="post">
    <textarea name="textarea1" id="textarea1"></textarea><br />
    <input type="submit" value="submit" />
</form>
<br /><br />
<a href="page2.php">Go to page2</a>

page2.php

页面2.php

<?php
    session_start(); // needs to be before anything else on page to use $_SESSION
    // this page outputs the textarea1 from the session IF it exists
    $textarea1 = ""; // set var to avoid errors
    if(isset($_SESSION['textarea1'])){
        $textarea1 = $_SESSION['textarea1']
    }
?>
<textarea><?php echo $textarea1;?></textarea>


WARNING!!! - This contains no validation!!!

警告!!!- 这不包含验证!!!