0

I have two PHP files, in which i'd like to store a session variable id like this

if(authentification($login,$mdp) == 0)
                {?>
                <script>alert('logging failed');</script>
                <?php
                header('Location: Contacts.php'); 
                }
                else{
                session_start();
                $_SESSION['login'] = $login;
                $_SESSION['mdp'] = $mdp;
                $a = authentification($login,$mdp);;
                $_SESSION['id'] = $a;
                 header('Location: space.php'); 

                }

And in the file space.php

session_start(); 
$list = Get_companies_by_user($_SESSION['id']);

in the function.php

function authentification($login,$mdp){

                parametrs();    

                $Log_query=mysql_query(
                    "   
                        SELECT user_id 
                        FROM  reg_users
                        WHERE username ='$login'
                        AND   password   ='$mdp'

                        ") or die(mysql_error());

                        if ($Log_query == true && mysql_num_rows($Log_query) >0) {

                          while ($Res_user = mysql_fetch_array($Log_query) ) {
                        return $Res_user;

                        }
                        }
                        else return 0;

}

The problem is in the value of the variable $_SESSION['id'] : why it is null in space.php? How can i fix this error?

Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191

1 Answers1

1

Try something like:

function authentification($login,$mdp){
    $Log_query = mysql_query("SELECT user_id FROM  reg_users WHERE username ='$login' AND   password   ='$mdp'") or die(mysql_error());

    //The user should be unique, not more than 0
    if($Log_query && mysql_num_rows($Log_query) === 1){
        while ($Res_user = mysql_fetch_array($Log_query) ) {
            $user = $Res_user['user_id'];
        }
        return $user;
    }else{
        return 0;
    }
}

You should also escape $login and $mdp with:

mysql_real_escape_string($var);
Perocat
  • 1,481
  • 7
  • 25
  • 48
  • 1
    Try to echo `$a` on the first script you posted and watch what it contains – Perocat Jul 28 '13 at 23:57
  • the value of `$a` is 6 – Lamloumi Afif Jul 29 '13 at 00:03
  • 1
    So the function authentification works... There is something else outside what you posted... If `$a = 6` then `$_SESSION['id']` is also `= 6`... If you post `$_SESSION['id'] on `space.php` it is `0`? Or the function `Get_companies_by_user` returns nothing? – Perocat Jul 29 '13 at 00:06
  • i change the code to `session_start(); require("function.php"); $a = $_SESSION['id']; $list = Get_companies_by_user($a);` and it works!!! – Lamloumi Afif Jul 29 '13 at 00:17
  • 1
    Yes, obviously you need to include the file which contains the function to call it. Please accept my answer if it helped you – Perocat Jul 29 '13 at 15:11