Hi,
could someone please tell me how to about putting a login name to the homepage of a user when they sign in. for example, after they have logged in using their username and password. Their name would come up in the homepage, like welcome Amy.
This is my checklogin.php if it's any help

<?php
ob_start();
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="pa"; // Database name
$tbl_name="user"; // Table name
 // Get info from the session

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword and $myusertype
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusertype=$_POST['myusertype'];


//$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and //password='$mypassword'";
//$result=mysql_query($sql);


$sql=("SELECT user_type FROM $tbl_name WHERE username='$myusername' and password='$mypassword' and user_type ='$myusertype'");

$result2=mysql_query($sql);


// Mysql_num_row is counting table row
$count=mysql_num_rows($result2);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1 ){
//Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
session_register("myusertype");
   if($myusertype=="admin"){
header("location:admin/index.php");
}else if($myusertype=="staff"){
echo $result2;
//header("location: tutors/index.php");
}else if($myusertype=="student"){
header("location: students/index.php");
}else{
echo "Wrong usertype";

exit;
}
} 
else{
echo "Wrong Username or Password";
}

//ob_end_flush();
?>

Recommended Answers

All 8 Replies

Save the username of the user into a cookie.
Use google to search for cookies, also check PHP Manual extensively.
That's all.

Good luck.

store the username in the session variable as
$_SESSION = $username and then on the other page echo the name using
echo $_SESSION;

I'm not sure what to search for exactly in google apart from cookies and sessions. Could someone please help me out as to what to put in my code above?

instead of using ob_start(); use session_start(); and store the variables in the session array.

like this:

<?php
session_start();
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="pa"; // Database name
$tbl_name="user"; // Table name
 // Get info from the session

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword and $myusertype
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusertype=$_POST['myusertype'];


//$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and //password='$mypassword'";
//$result=mysql_query($sql);


$sql="SELECT user_type FROM $tbl_name WHERE username='$myusername' and password='$mypassword' and user_type ='$myusertype'";

$result2=mysql_query($sql);


// Mysql_num_row is counting table row
$count=mysql_num_rows($result2);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1 ){
//Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['user'] = $myusername;
$_SESSION['pass'] = $mypassword;
$_SESSION['user_type'] = $myusertype;
   if($myusertype=="admin"){
header("location:admin/index.php");
}else if($myusertype=="staff"){
echo $result2;
//header("location: tutors/index.php");
}else if($myusertype=="student"){
header("location: students/index.php");
}else{
echo "Wrong usertype";

exit;
}
} 
else{
echo "Wrong Username or Password";
}

?>

got it thanks.. Can i just ask though, still using this code how do i encrypt the password. I have tried putting

//hash the password
$password = md5($password);

in the code above but it didn't encrypt the password. I'm i doing something wrong

$password = md5($password) should work, but you must know that md5 is one way encryption and you cannot decrypt the string. if you want to encrypt / decrypt use base64encode / base64decode.

what you could do is download a encryption class off the internet and use it the encrypt/decrypt your passwords.

Hmm.. I'll try that one out and see hw it turns out. I thought it was just as easy as putting the md5 on a line and that was it

what you could do is download a encryption class off the internet and use it the encrypt/decrypt your passwords.

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.