user225269
user225269

Reputation: 10913

User welcome message in php

How do I create a user welcome message in php. So that the user who has been logged on will be able to see his username. I have this code, but it doesn't seem to work.

    <?php
$con = mysql_connect("localhost","root","nitoryolai123$%^");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("school", $con);





$result = mysql_query("SELECT * FROM users
WHERE Username='$username'");

while($row = mysql_fetch_array($result))
  {
  echo $row['Username'];
  echo "<br />";
  }
?>

I'm trying to make use of the data that is inputted in this login form:

<form name="form1" method="post" action="verifylogin.php">
<td>
<table  border="0" cellpadding="3" cellspacing="1" bgcolor="">
<tr>

<td  colspan="16" height="25"  style="background:#5C915C; color:white; border:white 1px solid; text-align: left"><strong><font size="2">Login User</strong></td>
</tr>
<tr>

<td width="30" height="35"><font size="2">Username:</td>
<td width="30"><input  name="myusername" type="text" id="idnum" maxlength="5"></td>
</tr>

<tr>

<td width="30" height="35" ><font size="2">Password:</td>
<td width="30"><input name="mypassword" type="password" id="lname" maxlength="15"></td>


</tr>

<td align="right" width="30"><td align="right" width="30"><input type="submit" name="Submit" value="Submit" /></td> <td align="right" width="30"><input type="reset" name="Reset" value="Reset"></td></td>
</tr>
</form>

But this, verifylogin.php, seems to be in the way.

<?php
$host="localhost";
$username="root"; 
$password="nitoryolai123$%^"; 
$db_name="school"; 
$tbl_name="users"; 


mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];


$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);


$count=mysql_num_rows($result);


if($count==1){

session_register("myusername");
session_register("mypassword"); 
header("location:userpage.php");
}
else {
echo "Wrong Username or Password";
}
?>

How do I do it? I always get this error when I run it:

Notice: Undefined variable: username in C:\wamp\www\exp\userpage.php on line 53

Can you recommend of an easier on how I can achieve the same thing?

Upvotes: 0

Views: 2059

Answers (3)

William Password
William Password

Reputation: 1

and most importantly: if a new user logs in, it needs to show a different name and not show the previous one

Upvotes: 0

Lizard
Lizard

Reputation: 44992

You will also need to add an exit(); after the following code:

session_register("myusername");
session_register("mypassword"); 
header("location:userpage.php");
exit(); ## EXIT REQUIRED

If you don't add this and the client refreshes they will be prompted with somethingh like 'Are you sure you wish to resubmit the posted variables?' And if they click yes then all you login logic will be executed again. In this case this may not be fatal thing, but you should till have it in anyway.

Upvotes: 0

Axarydax
Axarydax

Reputation: 16603

$result = mysql_query("SELECT * FROM users WHERE Username='$username'"); 

you forgot to define and fill $username from somewhere

Upvotes: 3

Related Questions