Jump to content

Show username


ymc331

Recommended Posts

Hi, I'm new to php and I'm building a website for my school's Parents Teacher conference where you can register an appointment automatically. But I have a bit of problem. I have seperated the login procedure in 3 files

main_login.php

checklogin.php

login_success.php

 

The first file is the form itself

the second one is connecting to DB and checking the login info with the mysql table info

the third one is the page that you are redirected to when the login is successful. However I want to display the user name at this page and the other pages too. I really need this function because when parents want to register, I have to insert their ID into the mysql table. so can someone help me do this  please??

 

checklogin.php

<?php

ob_start();

$host="localhost"; // Host name

$username="xxxxxxx"; // Mysql username

$password="xxxxxxx"; // Mysql password

$db_name="xxxxxxxx"; // Database name

$tbl_name="members"; // 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");

 

// Define $myusername and $mypassword

$myusername=$_POST['myusername'];

$mypassword=$_POST['mypassword'];

 

// To protect MySQL injection (more detail about 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 username='$myusername' and 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";

}

 

ob_end_flush();

 

 

?>

 

main_login.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>PTCONFIG TEST</title>

</head>

 

<body>

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">

<tr>

<form name="form1" method="post" action="checklogin.php">

<td>

<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">

<tr>

<td colspan="3"><strong>Member Login </strong></td>

</tr>

<tr>

<td width="78">Username</td>

<td width="6">:</td>

<td width="294"><input name="myusername" type="text" id="myusername"></td>

</tr>

<tr>

<td>Password</td>

<td>:</td>

<td><input name="mypassword" type="password" id="mypassword"></td>

</tr>

<tr>

<td> </td>

<td> </td>

<td><input type="submit" name="Submit" value="Login"></td>

</tr>

</table>

</td>

</form>

</tr>

</table>

<center>

  <p>We have sent out emails to all parents which contains the username and the password for this site.</p></center>

 

</body>

</html>

 

Link to comment
https://forums.phpfreaks.com/topic/160926-show-username/
Share on other sites

In the checklogin.php file set a $_SESSION['username'] variable to the value of the member's username, once they've been validated. Then whenever you want to call the username just use that variable.

 

So change this bit:

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}

 

to

 

if($count==1){
$row = mysql_select_assoc($result);

// Register $myusername, $mypassword and redirect to file "login_success.php"

$_SESSION['username'] =$row['username'];
header("location:login_success.php");
}

Link to comment
https://forums.phpfreaks.com/topic/160926-show-username/#findComment-849278
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.