Jump to content

Display the FullName of a logged-in user


pritithangjam
Go to solution Solved by pritithangjam,

Recommended Posts

I'm trying to display the "name" of a user who has successfully logged in to my website. It is based a simple open source script.

 

Here's the fully working unmodified code which displays the name, uc as needed:

<?php
session_start();

$dbhost = "localhost";
$dbname = "member1";
$dbuser = "root";
$dbpass = "";

mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>

<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['uc']))
{
	 ?>
    
<h3>Welcome</h3>
<p>your uc is <strong><?=$_SESSION['uc']?></strong></p>
<p>your name is <strong><?=$_SESSION['name']?></strong></p>
<a href="logout.php">logout</a>

    <?php
}
elseif(!empty($_POST['uc']) && !empty($_POST['mobile']))
{
	 $uc = mysql_real_escape_string($_POST['uc']);
	 $name = mysql_real_escape_string($_POST['name']);
    $mobile = md5(mysql_real_escape_string($_POST['mobile']));
    
	 $checklogin = mysql_query("SELECT * FROM users WHERE uc = '".$uc."' AND name = '".$name."' AND mobile = '".$mobile."'");
    
    if(mysql_num_rows($checklogin) == 1)
    {
    	 $row = mysql_fetch_array($checklogin);
        
        $_SESSION['uc'] = $uc;
        $_SESSION['name'] = $name;
        $_SESSION['LoggedIn'] = 1;
        
    	 echo "<h1>Success - redirecting</h1>";
        echo "<meta http-equiv='refresh' content='2;rdvmembers-index.php' />";
    }
    else
    {
    	 echo "<h1>Error</h1>";
    }
}
else
{
	?>

    <h1>Members Login</h1>
	<form method="post" action="rdvmembers-index.php" name="loginform" id="loginform">
uc <input type="text" name="uc" id="uc" size="40" /> 
name <input type="text" name="name" id="name" size="40" /> 
mobile <input type="text" name="mobile" id="mobile" size="40"/>  
<input class="button_text" type="submit" name="login" id="login" value="Login" />
	</form>
    
   <?php
}
?>

</div>
</body>
</html>


Now, i want to remove the name field from the "Members Login" form so that it ask for only UC and Mobile. The name to be displayed must be taken from the database on successful login. I modified the form and made a few changes to the code but no matter what changes i do, it won't display the "name". Where am i going wrong?

here's the modified code:
 

<?php
session_start();

$dbhost = "localhost";
$dbname = "member1";
$dbuser = "root";
$dbpass = "";

mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>

<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['uc']))
{
	 ?>
    
<h3>Welcome</h3>
<p>your uc is <strong><?=$_SESSION['uc']?></strong></p>
<p>your name is <strong><?=$_SESSION['name']?></strong></p>
<a href="logout.php">logout</a>

    <?php
}
elseif(!empty($_POST['uc']) && !empty($_POST['mobile']))
{
	 $uc = mysql_real_escape_string($_POST['uc']);
	 $name = mysql_real_escape_string($_POST['name']);
    $mobile = md5(mysql_real_escape_string($_POST['mobile']));
    
	 $checklogin = mysql_query("SELECT * FROM users WHERE uc = '".$uc."' AND name = '".$name."' AND mobile = '".$mobile."'");
    
    if(mysql_num_rows($checklogin) == 1)
    {
    	 $row = mysql_fetch_array($checklogin);
        
        $_SESSION['uc'] = $uc;
        $_SESSION['name'] = $name;
        $_SESSION['LoggedIn'] = 1;
        
    	 echo "<h1>Success - redirecting</h1>";
        echo "<meta http-equiv='refresh' content='2;rdvmembers-index.php' />";
    }
    else
    {
    	 echo "<h1>Error</h1>";
    }
}
else
{
	?>

    <h1>Members Login</h1>
	<form method="post" action="rdvmembers-index.php" name="loginform" id="loginform">
uc <input type="text" name="uc" id="uc" size="40" /> 
mobile <input type="text" name="mobile" id="mobile" size="40"/>  
<input class="button_text" type="submit" name="login" id="login" value="Login" />
	</form>
    
   <?php
}
?>

</div>
</body>
</html>

ERROR:

Notice: Undefined index: name in /Applications/XAMPP/xamppfiles/htdocs/member1/rdvmembers-index.php on line 28

If i remove line 28, the error goes to line 30 and so forth. Can anyone figure this out for me? It seems plain simple.

Link to comment
Share on other sites

what does print_r($row); show?

 

to get undefined index errors from that code, your actual table columns that are being selected must have some letter-case difference from what you are referencing.

 

edit: my reply here of course refers to your CURRENT code that you have posted in some other php help forum that i also visit.

Edited by mac_gyver
Link to comment
Share on other sites

I'm looking at your second block of code.  You are not passing name from the form, so change to this:

$uc = mysql_real_escape_string($_POST['uc']);
//remove
//$name = mysql_real_escape_string($_POST['name']);
$mobile = md5(mysql_real_escape_string($_POST['mobile']));

//change this and remove $name
$checklogin = mysql_query("SELECT * FROM users WHERE uc = '".$uc."' AND mobile = '".$mobile."'");
Edited by AbraCadaver
Link to comment
Share on other sites

 

I'm looking at your second block of code.  You are not passing name from the form, so change to this:

$uc = mysql_real_escape_string($_POST['uc']);
//remove
//$name = mysql_real_escape_string($_POST['name']);
$mobile = md5(mysql_real_escape_string($_POST['mobile']));

//change this and remove $name
$checklogin = mysql_query("SELECT * FROM users WHERE uc = '".$uc."' AND mobile = '".$mobile."'");

 

I did as you asked and got the following error and output:

Error:

Notice: Undefined variable: name in /Applications/XAMPP/xamppfiles/htdocs/member1/rdvmembers-index.php on line 38

Output:

Welcome

your uc is 999

your name is

logout

here's the modified code as per your suggestion:

 

<?php
session_start();

$dbhost = "localhost";
$dbname = "member1";
$dbuser = "root";
$dbpass = "";

mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>

<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['uc']))
{
	 ?>
    
<h3>Welcome</h3>
<p>your uc is <strong><?=$_SESSION['uc']?></strong></p>
<p>your name is <strong><?=$_SESSION['name']?></strong></p>
<a href="logout.php">logout</a>

    <?php
}
elseif(!empty($_POST['uc']) && !empty($_POST['mobile']))
{

$uc = mysql_real_escape_string($_POST['uc']);
$mobile = md5(mysql_real_escape_string($_POST['mobile']));
    $mobile = md5(mysql_real_escape_string($_POST['mobile']));
    	$checklogin = mysql_query("SELECT * FROM users WHERE uc = '".$uc."' AND mobile = '".$mobile."'");
    
    if(mysql_num_rows($checklogin) == 1)
    {
    	 $row = mysql_fetch_array($checklogin);
        
        $_SESSION['uc'] = $uc;
        $_SESSION['name'] = $name;
        $_SESSION['LoggedIn'] = 1;
        
    	 echo "<h1>Success - redirecting</h1>";
        echo "<meta http-equiv='refresh' content='5;rdvmembers-index.php' />";
    }
    else
    {
    	 echo "<h1>Error</h1>";
    }
}
else
{
	?>

    <h1>Members Login</h1>
	<form method="post" action="rdvmembers-index.php" name="loginform" id="loginform">
uc <input type="text" name="uc" id="uc" size="40" /> 
mobile <input type="text" name="mobile" id="mobile" size="40"/>  
<input class="button_text" type="submit" name="login" id="login" value="Login" />
	</form>
    
   <?php
}
?>

</div>
</body>
</html>
Link to comment
Share on other sites

what does print_r($row); show?

 

to get undefined index errors from that code, your actual table columns that are being selected must have some letter-case difference from what you are referencing.

 

edit: my reply here of course refers to your CURRENT code that you have posted in some other php help forum that i also visit.

yes, that forum was just doing nothing but wasting my time and theirs by coming back to square one time and again. Felt like they are not reading my post but just replying for the sake of it.

 

I don't understand. Where do i place print_r($row);?

Link to comment
Share on other sites

i get this error when i do that:

Notice: Undefined index: uc in /Applications/XAMPP/xamppfiles/htdocs/member1/rdvmembers-index.php on line 37

Notice: Undefined index: name in /Applications/XAMPP/xamppfiles/htdocs/member1/rdvmembers-index.php on line 38

here's the modified code:

<?php
session_start();

$dbhost = "localhost";
$dbname = "member1";
$dbuser = "root";
$dbpass = "";

mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>

<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['uc']))
{
	 ?>
    
<h3>Welcome</h3>
<p>your uc is <strong><?=$_SESSION['uc']?></strong></p>
<p>your name is <strong><?=$_SESSION['name']?></strong></p>
<a href="logout.php">logout</a>

    <?php
}
elseif(!empty($_POST['uc']) && !empty($_POST['mobile']))
{

$uc = mysql_real_escape_string($_POST['uc']);
$mobile = md5(mysql_real_escape_string($_POST['mobile']));
    	$checklogin = mysql_query("SELECT * FROM users WHERE uc = '".$uc."' AND mobile = '".$mobile."'");
    
    if(mysql_num_rows($checklogin) == 1)
    {
    	 $row = mysql_fetch_array($checklogin);
        
		$_SESSION['uc'] = $row['uc'];
		$_SESSION['name'] = $row['name'];
        $_SESSION['LoggedIn'] = 1;
        
    	 echo "<h1>Success - redirecting</h1>";
        echo "<meta http-equiv='refresh' content='5;rdvmembers-index.php' />";
    }
    else
    {
    	 echo "<h1>Error</h1>";
    }
}
else
{
	?>

    <h1>Members Login</h1>
	<form method="post" action="rdvmembers-index.php" name="loginform" id="loginform">
uc <input type="text" name="uc" id="uc" size="40" /> 
mobile <input type="text" name="mobile" id="mobile" size="40"/>  
<input class="button_text" type="submit" name="login" id="login" value="Login" />
	</form>
    
   <?php
}
?>

</div>
</body>
</html>
Link to comment
Share on other sites

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.