Jump to content

how to display user's after log in


Go to solution Solved by cainam29,

Recommended Posts

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>

 

Link to comment
https://forums.phpfreaks.com/topic/279303-how-to-display-users-after-log-in/
Share on other sites

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'])); 
  • Solution

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']));
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.