I trying to make small personal website in PHP + MYSQL . In my database there is a table named ' tblUsers ' . I am using this table for users Login .I displayed the username of the user after the login . But how to track the userID of the user .

in my table 3 fields

" intUserID (autoincrement ,primarykey) , txtPassword ,txtUsername"

how to record the userID as session value. please.......

Recommended Answers

All 7 Replies

Hi...
inorder to record the sessions, do the following:

login.php

<?php
session_start();
//most important line... as it marks the start of session
//suppose user submits the login button and redirects to this page
if($_POST['submit']=="submit")
{
//some condition above in if to detect user actually pressed login button
    //connect to database.. check username and password..
   //when done... user authenticated..
    $_SESSION['userid']=$row['intUserID']; //get this value from database
    $_SESSION['username']=$row['txtUsername'];
}
//thats it.. the userid and usrname stored in sessions
?>

To access these stored sessions in other page..

page_user.php

<?php
session_start();
//above line must on every page where you need to access the session value
echo $_SESSION['userid'];
echo $_SESSION['username'];

?>

Apart from the above example.. refer the session tutorials by searching them on google..

http://tizag.com/phpT - Refer sessions tutorial here.. easy and simple..

This is my dreamweavers code to find session variable and in this a session variable named ' MM_Username ' . And Using that i got the ' username' of the user , how to implement code to find ' UserID ' in to this please ...

<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['txtUsername'])) {
  $loginUsername=$_POST['txtUsername'];
  $password=$_POST['txtPassword'];
  $MM_fldUserAuthorization = "intAccessLevel";
  $MM_redirectLoginSuccess = "home.php";
  $MM_redirectLoginFailed = "index.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_nydata2, $nydata2);
  	
  $LoginRS__query=sprintf("SELECT txtUserName, txtPassword, intAccessLevel FROM tblusers WHERE txtUserName='%s' AND txtPassword='%s'",
  get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password)); 
   
  $LoginRS = mysql_query($LoginRS__query, $nydata2) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
    
    $loginStrGroup  = mysql_result($LoginRS,0,'intAccessLevel');
    
    
//declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	
	

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>

pla reply.....

$user_fname=$_SESSION['MM_Username'];
 $sql=mysql_query("SELECT * FROM table where user_fname='$user_fname'");
		while( $row = mysql_fetch_array($sql))
		{
                  $user_fname=$row['user_fname'];
		  $pass=$row['user_pass'];
		  $user_id=$row['user_id'];
		  $gender=$row['user_sex'];
                }

Change the column name according to your column id ...
fetch sql result in array..from that you take the value of whatever you want...

echo $user_id;//check data using echo
$user_fname=$_SESSION['MM_Username'];
 $sql=mysql_query("SELECT * FROM table where user_fname='$user_fname'");
		while( $row = mysql_fetch_array($sql))
		{
                  $user_fname=$row['user_fname'];
		  $pass=$row['user_pass'];
		  $user_id=$row['user_id'];
		  $gender=$row['user_sex'];
                }

Change the column name according to your column id ...
fetch sql result in array..from that you take the value of whatever you want...

echo $user_id;//check data using echo

this is not working in my Dreamweaver , please help me to correct . U checked it . please..........

In your query, you are not selecting userid

only
txtUserName, txtPassword, intAccessLevel FROM tblusers

select UserID

<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['txtUsername'])) {
  $loginUsername=$_POST['txtUsername'];
  $password=$_POST['txtPassword'];
  $MM_fldUserAuthorization = "intAccessLevel";
  $MM_redirectLoginSuccess = "home.php";
  $MM_redirectLoginFailed = "index.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_nydata2, $nydata2);
  	
  $LoginRS__query=sprintf("SELECT txtUserName, txtPassword, intAccessLevel,UserID FROM tblusers WHERE txtUserName='%s' AND txtPassword='%s'",
  get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password)); 
   
  $LoginRS = mysql_query($LoginRS__query, $nydata2) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  while( $row = mysql_fetch_array($LoginRS))
		{
		$user_fname=$row['txtUserName'];
		  $pass=$row['txtPassword'];
		  $user_id=$row['UserID'];
echo $user_fname;
echo $pass;		  
echo $user_id;
		}
			
  
  if ($loginFoundUser) {
    
    $loginStrGroup  = mysql_result($LoginRS,0,'intAccessLevel');
    
    
//declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	
	

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>

you are using code generated by dreamweaver, i suggest to create our own log in form since code generated by dreamweaver is not flexible.
try to experiment the code posted by sikka_varun

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.