Jump to content

New to PHP - need help with something that's probably simple?...


ShootingBlanks

Recommended Posts

Hello.  Sorry if this is easy, but I'm drawing a blank here...

 

I have a simple login form.  Two fields - username/password.

 

I have a database table ("users") with user_id, username, pwd.  I have the login form working as far as either logging you in if you're int he database, or else directing you to an error page if you're not in the database.  But what I want to do is this...

 

...have it so that it somehow remembers the "user_id" throughout your browsing session.  Because I need that user_id to be used in other sections of the site after you log in.  Specifically, when you log in you are able to post text in fields of another database table ("documents").  I want to keep track of which users are populating the "documents" table, so if I could capture their user ID (from when they first logged in) whenever they submit a post, it would automatically put their "user_id" from the initial logon screen into the "user_id" field of the "documents" table that they're populating.

 

I assume this entails somehow using a hidden field for "user_id" on both the logon screen and the posting screen, but I'm not sure how to go about this.  Any help would be greatly appreciated.  Thanks!!!...

 

Link to comment
Share on other sites

I would assume the user enter it, along with a password, when they log in.  I only know what you have told us. Knowledge of php is no substitute for clairvoyance.

Well, that's part of the problem.  I'm asking for suggestions of how to capture the user_id from the login form.  It's just a primary key, so the user would not enter it (nor would they even KNOW their user_id).  If it needs to be captured in a hidden field (which I assume it does), I need help being instructed on how to go about doing that, as that is what I am unfamiliar with...

 

Thanks!...

Link to comment
Share on other sites

You could do it when you validate the login

session_start();

$username = sanitize($_POST['username']);
$pwd = md5($_POST['password']);

$sql = "SELECT user_id FROM user
          WHERE username = '$username' AND 'password' = '$pwd'";
$res = mysql_query($sql);
if ($row = mysql_fetch_row($res))
{
      // valid
      $_SESSION['user_id'] = $row[0];
      header("location: index.php");
      exit;
}
else header("location: login.php");
?>

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.