-1

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.".";
?>
Abulurd
  • 1,018
  • 4
  • 15
  • 31
sanoj lawrence
  • 951
  • 5
  • 29
  • 69
  • 1
    save the values in $_SESSION and fetch them in any page – Tushar Gupta Sep 23 '14 at 08:20
  • 2
    There are multiple ways to reach your goal. 1. Sessions 2. Pass through the URL 3. Post 4. Cookie 5. Globals. What do you exactly want? – GuyT Sep 23 '14 at 08:21
  • @Wehelie but i need pass variable to many pages – sanoj lawrence Sep 23 '14 at 08:22
  • You can use session variables on any page – Khushboo Sep 23 '14 at 08:22
  • @GuyT which is safe and best to pass variable to 2page continues – sanoj lawrence Sep 23 '14 at 08:23
  • @sanoj lawrence, if you read it carefully, it will make sense my friend, I promise. – Abulurd Sep 23 '14 at 08:24
  • @sanojlawrence No method is safe by themselves. You have to validate the user input. I would advice you to use `$_SESSION` in a case like this. – GuyT Sep 23 '14 at 08:25
  • @GuyT but how to do that passing value to three pages – sanoj lawrence Sep 23 '14 at 08:26
  • @sanojlawrence Like the others already mentioned: start with `session_start();` on every page and use `$_SESSION['name'] = $value` to set the session variable and `echo $_SESSION['name']` to use the value. – GuyT Sep 23 '14 at 08:28
  • [Saroj](http://stackoverflow.com/users/3836908/sanoj-lawrence) I think you should use either session or POST method in this type of case. Pass through URL is not a safe way and cookie may cause problem if user destroy in between. – Sunil Chhimpa Sep 23 '14 at 10:08

4 Answers4

2

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"] ;
Khushboo
  • 1,819
  • 1
  • 11
  • 17
Sunil Chhimpa
  • 404
  • 1
  • 4
  • 12
1

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'];
David Jones
  • 4,275
  • 6
  • 27
  • 51
0

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);
Vimalnath
  • 6,373
  • 2
  • 26
  • 47
0

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.