维护 php 页面之间的会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11791534/
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 02:07:06  来源:igfitidea点击:

maintain session between the pages in php?

phpmysqlformssession

提问by user1565309

I am very new to PHP.

我对 PHP 很陌生。

I want to transfer data between pages.

我想在页面之间传输数据。

In my requirements I have the home page first, in that I have 3 fields: name, address, pinand then submit button.

在我的要求中,我首先拥有主页,因为我有 3 个字段:nameaddresspin然后提交 button

When I entered the above fields and then click on submit, it moves to page2.php, it has form data.

当我输入上述字段然后单击提交时,它会移动到page2.php,它有表单数据。

I have transferred the first form data to second page. Now in second page I have a submit button. When I click on that button the data is submitted to a MySQL database.

我已将第一个表单数据传输到第二页。现在在第二页我有一个提交按钮。当我单击该按钮时,数据将提交到 MySQL 数据库。

My problem is, how can I move first page values to insertdata.phppage and submit the data ?

我的问题是,如何将第一页值移动到insertdata.php页面并提交数据?

回答by asprin

There are two ways with which you can do this

有两种方法可以做到这一点

  1. Sessions
  2. Hidden input fields
  1. 会话
  2. 隐藏的输入字段

Sessions

会话

To pass data from one page to another, you first need to call session_start()on all pages that are going to use $_SESSIONsuperglobal variable. Then you can store your values in sessions by using

要将数据从一个页面传递到另一个页面,您首先需要调用session_start()将使用$_SESSION超全局变量的所有页面。然后您可以使用以下方法将您的值存储在会话中

$_SESSION['name'] = $_POST['name'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['pin'] = $_POST['pin'];

To use these values in the second page, simply call them by their names. Ex:

要在第二页中使用这些值,只需按名称调用它们即可。前任:

$name = $_SESSION['name']; // will contain the value entered in first page

==================================================================================

================================================== ================================

Hidden Input Fields

隐藏的输入字段

This is more of a tedious approach but it does the job none the less. The process involves storing data that needs to be passed onto to different pages in hidden fields and later accessing them via the $_POSTor $_GETsuperglobal.

这更像是一种乏味的方法,但它仍然可以完成工作。该过程涉及存储需要传递到隐藏字段中的不同页面的数据,然后通过$_POST$_GET超全局访问它们。

page1.php(which posts to page2.php)

page1.php(发布到 page2.php)

<input type="text" value="Page 1 content" name="content" />
<input type="text" value="Page 1 body" name="body" />

page2.php(which posts to page3.php)

page2.php(发布到 page3.php)

<input type="hidden" value="<?php echo $_POST['content']; ?>" name="content" />
<input type="hidden" value="<?php echo $_POST['body']; ?>" name="body" />
<input type="text" value="Page 2 content" name="content2" />
<input type="text" value="Page 2 body" name="body2" />

page3.php

页面3.php

echo $_POST['content']; // prints "Page 1 content"
echo $_POST['body']; // prints "Page 1 body"
echo $_POST['content2']; // prints "Page 2 content"
echo $_POST['body2']; // prints "Page 2 body"

回答by Varun Sridharan

just use the below code in your first page <?php session_start(); ?>and use the following code in your sencond page

只需在您的第一页中<?php session_start(); ?>使用以下代码并在您的第二页中使用以下代码

<?php
$name = $_SESSION['name'];
$address = $_SESSION['address'];
$pin = $_SESSION['pin'];
echo $name."<br/>";
echo $address."<br/>";
echo $pin."<br/>";
?>

or you can use postor getmethod as below

或者您可以使用postget方法如下

For GET Method

对于 GET 方法

<?php
$name = $_GET['name'];
$address = $_GET['address'];
$pin = $_GET['pin'];
echo $name."<br/>";
echo $address."<br/>";
echo $pin."<br/>";
?>

For POST METHOD

对于 POST 方法

    <?php
    $name = $_POST['name'];
    $address = $_POST['address'];
    $pin = $_POST['pin'];
    echo $name."<br/>";
    echo $address."<br/>";
    echo $pin."<br/>";
    ?>

回答by sundar

Like REQUEST, PHP maintains a map for session in which you could dump these values and across the pages. e.g.

像 一样REQUEST,PHP 维护一个会话映射,您可以在其中转储这些值和跨页面。例如

$_SESSION['name']='foo';

But the page where you would like to use session, you need to start the session using the method session_start();. But this method call should be the first line in your php.

但是你想使用会话的页面,你需要使用方法启动会话session_start();。但是这个方法调用应该是你的 php.ini 文件的第一行。

http://www.w3schools.com/php/php_sessions.asp

http://www.w3schools.com/php/php_sessions.asp

回答by Sinmok

To store data between page changes, you can use the superglobal $_SESSION array.

要在页面更改之间存储数据,您可以使用超全局 $_SESSION 数组。

Example

例子

Page 1

第 1 页

$_SESSION['name'] = "John";

Page 2

第2页

echo $_SESSION['name'];

Output

输出

John

Make sure you put session_start()at the top of each page that requires the use of sessions.

确保将session_start()需要使用会话的每个页面放在顶部。

Click here for more information on Sessions.

单击此处了解有关会话的更多信息。

回答by thedethfox

Basically when you submit the data in the first page save them in the session then redirect to the second page.

基本上,当您在第一页提交数据时,将它们保存在会话中,然后重定向到第二页。

In the second page just load them.

在第二页中只需加载它们。

Page 1:

第 1 页:

<?php session_start(); 

     $_SESSION["var_name"]  = $some_value;


?>

page 2:

第2页:

<?php session_start(); 

     $my_var = $isset($_SESSION["var_name"])?$_SESSION["var_name"]:null;




?>

you test the value of my_var, if it's empty then redirect to the first page or show an error message.

您测试 my_var 的值,如果它为空,则重定向到第一页或显示错误消息。

session tutorial http://www.w3schools.com/php/php_sessions.asp

会话教程 http://www.w3schools.com/php/php_sessions.asp

Mysql insert tutorial http://www.tizag.com/mysqlTutorial/mysqlinsert.php

Mysql 插入教程 http://www.tizag.com/mysqlTutorial/mysqlinsert.php

POST/GET tutorial http://www.tizag.com/phpT/postget.php

POST/GET 教程 http://www.tizag.com/phpT/postget.php