Page 1 of 1

Need to store the user_id in a session

Posted: Wed Jan 21, 2009 10:49 am
by Icethrill
At the moment this is a simple register form. Its working properly and everything except that I need to get the user_id from the database and store it in a session. At the moment all I save in the sessions is if your logged in and the username. But I need the user_id from the database to connect it to other tables in the database. I havent programmed the best security or anything its probably pretty incomplete so forget about that atm. I just want some suggestions how I could get the user_id in a session? I am stuck at the moment. :(

Anyway here is the code where I set the sessions.

Code: Select all

$query = "SELECT user_id FROM user WHERE username='{$anvandarnamn}' AND password='{$password}'";
        $resultLogin = mysql_query($query);
        confirm_query($resultLogin);
    
        if(mysql_num_rows($resultLogin) == 1){
            $_SESSION['inloggad'] = true; //Sätter SESSIONen så man blir inloggad
            $_SESSION['username'] = $anvandarnamn; //Stoppar in användarnamnet så vi kan skriva ut det på skärmen när man är inloggad
            redirect_to("index.php");
        }else{
            $felLogin = "Du skrev in fel användarnamn eller lösenord!"; //Visar texten om man skrev fel username eller password
        }
Please excuse the swedish comments I have made, just ignore them. There might be an easy solution for this but I aint that experienced programmer.

Re: Need to store the user_id in a session

Posted: Wed Jan 21, 2009 10:51 am
by Burrito
you'll have to loop over your $resultLogin resource and get the ID from the array.

you can do that using if($row = mysql_fetch_assoc($resultLogin)) and get rid of your mysql_num_rows() check.

Re: Need to store the user_id in a session

Posted: Wed Jan 21, 2009 10:55 am
by Icethrill
Burrito wrote:you'll have to loop over your $resultLogin resource and get the ID from the array.

you can do that using if($row = mysql_fetch_assoc($resultLogin)) and get rid of your mysql_num_rows() check.
Ok thanks! Gonna give it a try :D