Jump to content

Recommended Posts

1. When they have logged in grab their name from the db table and assign it to a session id of name.

2. place on your page in the area you wish to display their name and echo that session ID.

 

If you need something more detailed or spell out then that, then please provide your database table structure and I can make an example for ya.

I have connected and be able to log in.

 

Now Yes I did grab the username and try to place it on the page. Nothing display.

 

Here is my table

 

CREATE TABLE IF NOT EXISTS `user` (
  `Use_ID` int(11) NOT NULL AUTO_INCREMENT,
  `Use_Name` varchar(20) NOT NULL,
  `Use_Password` varchar(20) NOT NULL,
  `Rol_ID` int(11) NOT NULL,
  PRIMARY KEY (`Use_ID`),
  Foreign KEY `Rol_ID` (`Rol_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="Examination"; // Database name
$tbl_name="User"; // Table name

// 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");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE Use_Name='$myusername' and Use_Password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// 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");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

 

<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");


}



?>
<html>
<head>
<title>Multiple choice</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<H1> Welcome Ms
<?php echo $myusername; ?>

</H1>
</body>
<noframes><body bgcolor="#FFFFFF" text="#000000">

</body></noframes>
</html>

Normally when a user logs in and you want to display their name somewhere, you want to use the data pulled from the database rather than a form post.

 

while($r = mysql_fetch_assoc($result){
$_SESSION['myusername'] = $r['Use_Name'];
}

 

Add that in below where your registering your session myusername.

 

Then on your html page

 

 echo $_SESSION['myusername']; 

The tutorial you've followed appears to be from phpeasystep.com, and like every tutorial I've seen on that site, is obsolete. session_register() is deprecated, and stripslashes() shouldn't be arbitrarily applied to form data. You'd be better served to find a current tutorial and learn the right way first, rather than relearn it later.

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.