Basically I'm having difficulty getting the 'user_id' value from a database and storing it in a session.
What I have:
I have a login page which requests the users username and password. Upon submitting, they are taken to an authentication page. Providing a matching combination is submitted, the user is granted access - If there is not a matching combination, they are redirected away.
In the MySQL database, I have a column for 'user_id', 'username' and 'password'. And my authentication page is:
<?php
require "connect.php"; // connects to db
connection();
session_start();
$username=$_POST['username'];
$password=$_POST['password'];
$user_id=$row['user_id'];
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
$_SESSION['user_id'] = $user_id;
$result= mysql_query("SELECT * FROM users WHERE username='$username' and password='$password' and type='user'");
if(($row = mysql_fetch_array($result)))
{
print_r($_SESSION);
//header('Location: welcome.php');
}
else
{
header('Location: not-authorized.php');
}
?>
When I print the session, I am displayed with:
Array ( [username] => cm89 [password] => conrad1 [user_id] => )
I have tried a number of different ways, and cannot get the users ID to store to the session. The 'user_id' is a numeric value, generated with auto incriminate.
If someone could put me on the right tracks, that would be great. Thanks.