Pass variables between php sheets

-1

Good evening, I'm doing an ATM on php with mysql and I have the following question:

use the username and password system of agustin ramos (on the internet you can find it)

which works as follows:

You have the enter.php where you get to enter the username and password

then the action of that form is called login and has all the comparisons to verify that they exist in the database

Then it sends you to the home where you have the options of connected

I have in my case instead of the user table with user and password I have the card table associated with it and it asks for card number and card code

I would like then that:

that card number (num_tar) will be saved in a variable and it will arrive to home.php and then use it, I thought of placing it in the login.php (where the functions are) but I get it in home.php not defined.

HERE THE LOGIN.php CODES

<?php
if(!empty($_POST)){
if(isset($_POST["num_tar"]) &&isset($_POST["cod_tar"])){
    if($_POST["num_tar"]!=""&&$_POST["cod_tar"]!=""){
        include "conexion.php";

        $user_id=null;
        $sql1= "select * from tarjeta where (num_tar=\"$_POST[num_tar]\" and cod_tar=\"$_POST[cod_tar]\")";
        $query = $con->query($sql1);
        while ($r=$query->fetch_array()) {
            $user_id=$r["num_tar"];
            break;
        }
        if($user_id==null){
            print "<script>alert(\"Acceso invalido.\");window.location='../index.php';</script>";
        }else{
            session_start();

HERE I TRY TO PASS IT BUT I DO NOT KEEP IT

            global $num;
            $num=$_POST['num_tar'];
            $_SESSION["conectado"]=$user_id;

            print "<script>window.location='../home.php';</script>";                
        }
    }
}
}
?>

I think the problem is that the system would have to pass it to login.php first and then to home.php but in home.php I will not declare the variables

    
asked by Victor Alejandro Alvarado Vilo 28.05.2016 в 06:40
source

2 answers

1

The best thing would be to send it as a session variable, that is, pass it through the form to login.php, if the login is correct, the session starts with session_start(); and that value is sent as a session variable like this: $_SESSION['nameKey']=value .

Then every time you log in with session_start(); you can get that variable, regardless of the php file you're in.

    
answered by 29.05.2016 в 04:45
1

As mentioned above, what you can do best is to use it as a session variable, but assign a name to the session session_start ("name_of_the_session"); You can put this in your function file at the beginning and include it normally.

You assign a value to the variable $ _SESSION ["name_of_the_variable"] = value; Every time you want to refer to that variable, you do it directly or pass the value to another.

echo $ _SESSION ["name_of_the_variable"];

$ variable = $ _SESSION ["name_of_the_variable"]; echo $ variable;

When you no longer need to use the information, you only use unset ($ _ SESSION ["name_of_the_variable"]); to destroy only the variable or session_destroy (); and all the content of the session is destroyed

    
answered by 31.05.2016 в 22:42