Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP saving variables

There is a $variable, its value is a big Array().

It is created inside the function one() { ... } on the first.php page.

first.php has form with method="post", after submition page reloads on second.php

Is there any way to get value of $variable inside function two() { ... } on the second.php?

Seems I can post value of $variable within form, the problem is it can contain more than thousand symbols.

Thanks.

like image 499
James Avatar asked Aug 06 '10 10:08

James


1 Answers

Use "session_start()" function at the very beginning of any PHP web page, just after the first PHP start tag (<?php).

Then store the variable of yours into a superglobal session array variable, in the "first.php" page like:-

<?php
session_start(); // This line must be at the very beginning of this PHP page.

function one() {
    // blah, blah, ...

    if(isset($variable) && !empty($variable)) {
        $_SESSION['customVariable'] = $variable;
    }

    // some more blah, blah, ...
}
?>

Now if you come to the "second.php" page, you need to access this page's function as:-

<?php
function two() {
    // if any blah, blah, ...

    if(isset($_SESSION['customVariable']) && !empty($_SESSION['customVariable'])) {
        $variable = $_SESSION['customVariable'];
    }

    // next series of blah, blah, ...
}
?>

But in this "second.php" page, the "session_start()" function must be written at the very beginning of this page just after the first PHP start tag.

Hope it helps.

like image 140
Knowledge Craving Avatar answered Sep 29 '22 06:09

Knowledge Craving