user1341926
user1341926

Reputation: 81

How to store a PHP variable until another request

How can I store a PHP variable in a PHP script, and then use that same variable in an another PHP script?

I tried this code, but it didn't work:

<?php                       
                    session_start();
                            $util=$_POST['util'];
                            $pw=$_POST['pw'];
                            include("abrebdvendas.php");
                            //testar a exist�ncia de liga��o ao MySQL
                            $ligax=mysql_connect($host,$user,$senha);
                            if (!$ligax) {
                                    echo 'Erro: Falha na liga&#231;&#227;o'.'<br>';
                                    exit;
                                    }
                                    //ligar � base de dados vendas
                                    mysql_select_db($dbname,$ligax);
                                    //criar uma query para inser��o do registo
                                    $lista="select * from utilizador where util='$util' and pw='$pw'";
                                    $result=mysql_query($lista) or die(mysql_error());
                                    $dados = mysql_fetch_array($result);
                                    $u=$dados['util'];
                                    $p=$dados['pw'];
                            if ($u=='' or $p=='')
                            {
                            echo "Tente de novo";
                            echo '<meta HTTP-EQUIV="Refresh" CONTENT="1; URL=http://localhost/ola1/Site/Sem%20Log%20In/longin.html">';
                            } 
                            else if ($u==$util and $p==$pw)
                                    {
                                    //obter tipo valor
                                    echo '<h2>Bem vindo '.$util.'</h2><br>';
                                    $paulo=$_POST['util'];
                                    echo '<meta HTTP-EQUIV="Refresh" CONTENT="100; URL=http://localhost/ola1/Site/Com%20LogIn/paginicial.php">';

                                     $_SESSION['myNumber']=$num;                
                                                                        }
                                    else
                                    {
                                    echo "Tente de novo";
                                    echo '<meta HTTP-EQUIV="Refresh" CONTENT="0.1; URL=http://localhost/ola1/Site/Sem%20Log%20In/longin.html">';
                                    }                                   
                    ?>

The code is from here.

Upvotes: 2

Views: 2547

Answers (3)

slackmart
slackmart

Reputation: 4934

Using sessions, the first line must be session_start();

Then you can store some values with $num = $_SESSION['myNumber']; and utilize it in others operations.

Upvotes: 1

Dhruvisha
Dhruvisha

Reputation: 2530

when you want to use value of some variable which is stored in another variable in another page ,you should use $_SESSION[] as below:

 $_SESSION['myNumber']=$num;

You need to start session at the top of the php page as below:

 <?php
ob_start();
@session_start();
?>

And get variable from another php file as below

$newnum = $_SESSION['myNumber'];

Upvotes: 8

Drew Dahlman
Drew Dahlman

Reputation: 4972

you can store them as session variables

$_SESSION['myNumber'] = $num;

Upvotes: 0

Related Questions