Jump to content

Best way to SELECT info belonging only to specific logged-in user...??


mac007

Recommended Posts

Hi, All:

 

I'm trying to figure out what way it's the best way to pull info belonging to a specific user based on whether he's a logged-in "member", and want to make sure he's not able to access any other member's details... would the best way be to try to match the user's "username" stored in a $_SESSION when fetching his info, something like this:

 

<?php	
// Assume the login combo is this:
$username = $_POST['username'];
$password = $_POST['password'];
// Assume he has already logged in:
$_SESSION['username'] = $username;

//EXAMPLE 1: SELECTING user info simply from actual DB username/password match:
if ($_SESSION['username'])
{
$userRecords  = mysql_query("SELECT * FROM users WHERE username = '$username'  AND password = '$password'");
$userInfo = mysql_fetch_array($userRecords);
echo $userInfo['id'] . $userInfo['username'] . $userInfo['first-name'] . $userInfo['last-name'] . $userInfo['date-register'];
}


// EXAMPLE 2: or  SELECTING user info based on $_SESSION['username'] value: 
if ($_SESSION['username'])
{
$userRecords  = mysql_query("SELECT * FROM users WHERE username = . $_SESSION['username'] .  AND password = '$password'");
$userInfo = mysql_fetch_array($userRecords);
echo $userInfo['id'] . $userInfo['username'] . $userInfo['first-name'] . $userInfo['last-name'] . $userInfo['date-register'];
}

?>

 

So, my question is, are this actually working differently? is one better than the other as far as security, preventing other users from hacking either on purpose or accidenally into other user's details?

 

thank! Appreciate any feedback...

Link to comment
Share on other sites

thanks joel...

I see what you are saying. So, if I was using the username as the $_SESSION['username'], then I should at least make sure this field is defined as "unique" or also primary in the db by default. Or then again, as you say, I could just simply use the primary user id field to set the $_SESSION.

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.