How to pass two value from one page to another in php, passing using session-ThrowExceptions

Exception or error:

How to pass two value from one page to another in PHP using session.

 $account=$_SESSION["account_no"];

    $account1=$_SESSION["account_no"];
How to solve:

Session will be available through out the application (in all pages) until you destroy it.

To set a session,

<?php
session_start();
$_SESSION['variable_name_1'] = "value_1"; // or $_POST['accountno_1'];
$_SESSION['variable_name_2'] = "value_2"; // or $_POST['accountno_2'];
?>

In the other page, to get the values

<?php
session_start();
echo $_SESSION['variable_name_1'];
echo $_SESSION['variable_name_2'];

?>

Answer:

FILE-1: WHERE YOU NEED TO SAVE THE ACCOUNT TO SESSION

<?php   // NOTICE THAT THERE IS NO SPACE BEFORE <?php [THIS IS IMPORTANT!!!]

        // FILE-NAME: file_1.php WHERE YOU HAVE TO SET THE SESSION VARIABLE
        //FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
        if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
            session_start();
        }

        $_SESSION["account_no"] = $account;

FILE-2: WHERE YOU NEED TO GET THE ACCOUNT FROM SESSION

<?php   // NOTICE THAT THERE IS NO SPACE BEFORE <?php [THIS IS IMPORTANT!!!]

        // FILE-NAME: file_2.php WHERE YOU NEED TO READ THE SESSION VARIABLE
        //FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
        if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
            session_start();
        }

        // READ THE ACCOUNT NUMBER FROM SESSION DATA...
        $account = $_SESSION["account_no"];

Answer:

On the first page:

session_start();
$_SESSION['value1'] = 'First value';
$_SESSION['value2'] = 'Second value';

On the second page:

session_start();
$value1 = $_SESSION['value1'];
$value2 = $_SESSION['value2'];

Answer:

File:1 where data will be store Session

<?php
session_start(); //before HTML tag
$_SESSION['one'] = $account_no1;
$_SESSION['two'] = $account_no2;
?>

File2: where you like to retrieve session

 <?php
    session_start();
    echo $_SESSION['one'];
    echo $_SESSION['two'];

    ?>

Answer:

Sessions is a global variable in PHP.

Just create two session variables as use anywhere

<?php
session_start(); // should be at top of page or before any output to browser
$_SESSION['account'] = $account;
$_SESSION['account1'] = $account1;

Now access these session variables anywhere in any page but should start session before use, like:

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

Leave a Reply

Your email address will not be published. Required fields are marked *