cainam29 Posted June 18, 2013 Share Posted June 18, 2013 i wanted to display the user name after they logged in, i have the code below, this is my login.php <tr bgcolor="#FFFFFF"> <td width="71">Username</td> <td width="181"><input name="myusername" type="text" id="myusername" /></td> </tr> <tr bgcolor="#FFFFFF"> <td>Password</td> <td><input name="mypassword" type="password" id="mypassword" /></td> </tr> <tr bgcolor="#FFFFFF"> <td><input type="submit" name="Submit" value="Login" /></td> </tr> here is my checklogin.php <?php $host="*****"; // Host name $username="*****"; // Mysql username $password="*****"; // Mysql password $db_name="*****"; // Database name $tbl_name="*****"; // 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 = mysql_real_escape_string ($_POST['myusername']); $mypassword = mysql_real_escape_string ($_POST['mypassword']); $myusername = htmlspecialchars ($_POST['myusername']); $mypassword = htmlspecialchars ($_POST['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){ $row=mysql_fetch_array($result); session_start(); $_SESSION['login'] = "1"; $_SESSION['myusername'] = $row['username']; header("location: infratools.php"); } else { echo "Wrong Username or Password"; session_start(); $_SESSION['login'] = ''; } ?> and here is one of the page where i want to display the user name, infratools.php <?PHP session_start(); if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) { header ("Location: infralogin.php"); } ?> <p>Logged in as: <?php echo $_SESSION['myusername']; ?></p> Quote Link to comment Share on other sites More sharing options...
DaveyK Posted June 18, 2013 Share Posted June 18, 2013 So, what is the problem? Also, this is wrong: $myusername = mysql_real_escape_string ($_POST['myusername']); $mypassword = mysql_real_escape_string ($_POST['mypassword']); $myusername = htmlspecialchars ($_POST['myusername']); $mypassword = htmlspecialchars ($_POST['mypassword']); The second $myusername will overwrite the first $myusername. Try: $myusername = mysql_real_escape_string ( htmlspecialchars ($_POST['myusername'])); $mypassword = mysql_real_escape_string ( htmlspecialchars ($_POST['mypassword'])); Quote Link to comment Share on other sites More sharing options...
cainam29 Posted June 18, 2013 Author Share Posted June 18, 2013 the problem is the user's name is not being displayed... Quote Link to comment Share on other sites More sharing options...
Solution cainam29 Posted June 18, 2013 Author Solution Share Posted June 18, 2013 i think i figured it out...i just noticed that i am modifying the same file but saved on a different directory...code works perfect...sorry i just got so confused because of my php copying to another directory but then im still modifying the old one...thanks for the advise regarding this $myusername = mysql_real_escape_string ( htmlspecialchars ($_POST['myusername'])); $mypassword = mysql_real_escape_string ( htmlspecialchars ($_POST['mypassword'])); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.