Passing variable from 1st page to 3rd page in PHP [duplicate]

旧巷老猫 提交于 2020-01-07 09:43:44

问题


I need to pass a variable from page one to third page how do I make it?

  • I can pass variable from 1 page to next
  • how can i to pass variable from 1st to second page and same value to 3rd page

here is the example

<a href="page2.php?myname=Suraj Mittal&age=22&fav_color=White&fav_fruit=Grapes">Click here to send variables in the URL</a>

second page

<?php
 $name = $_GET['myname'];
$age = $_GET['age'];
$color = $_GET['fav_color'];
$fruit = $_GET['fav_fruit'];
        echo "My name is ".$name.". and I am ".$age." years old. My favourite color is ".$color." and my favourite fruit is ".$fruit.".";
?>

回答1:


You can use session to do this

 session_start();

 $_SESSION["myname"] = $_GET['myname'] ;

 $_SESSION["age"] = $_GET['age'] ;

and then you can access this SESSION variables on any page.

on your page where u want to access age, do like this

$age=$_SESSION["age"] ;



回答2:


You can just pass the variables again using the same method, passing them through the URL or put them in a session so they can be accessed anywhere, at least while the session is still active.

For example:

<?php

session_start();

$_SESSION['myname'] = $_GET['myname'];
$_SESSION['age'] = $_GET['age'];
$_SESSION['fav_color'] = $_GET['fav_color'];
$_SESSION['fav_fruit'] = $_GET['fav_fruit'];



回答3:


Why don't you use PHP session variables. Bascially, PHP session variables could be accessed anywhere inside the application.

session_start();
$_SESSION["myname"]    = $_GET['myname'];
$_SESSION["age"]       = $_GET['age'];
$_SESSION["fav_color"] = $_GET['fav_color'];
$_SESSION["fav_fruit"] = $_GET['fav_fruit'];

To Get the entire session variables/values in your third page:

echo '<pre>';
print_r($_SESSION);



回答4:


You can use like below:

On first page you used this URL:

<a href="page2.php?var_one=1&var_two=2&var_three=3">Go to second page</a>

Similarly on second page you can use below URL:

<a href="page3.php?var_one=1&var_two=2&var_three=3">Go to third page</a>

Let me know if it worked out for you. If details needed knock me.

Regards.



来源:https://stackoverflow.com/questions/25990205/passing-variable-from-1st-page-to-3rd-page-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!