Jump to content

Show Username in ECHO / PHP


PHPAndreas

Recommended Posts

Hello There.

My name is Andreas.

Well I have begin coding and come to the part at my menu where the username should be in my menu.

I have a login form and it look like this:

<?php

session_start();

$username = $_POST['username'];
$password = $_POST['password'];

if ($username&&$password)
{
	
	$connect = mysql_connect("localhost","root","") or die ("Could not connect to MySQL Host");
	mysql_select_db("host") or die ("Cound not find MySQL Database");
	
	$query = mysql_query("SELECT * FROM users WHERE username='$username'");
	
	$numrows = mysql_num_rows($query);
	
	if($numrows !=0)
	
	{
		
		while ($row = mysql_fetch_assoc($query))
		{
			
			$dbusername = $row['username'];
			$dbpassword = $row['password'];
			
		}
			if ($username==$dbusername&&$password==$dbpassword)
			{
				header ("Location: http://localhost/Host/index.php");
				$_SESSION['username']=$dbusername;
			}
			else
				echo "Incorrect password";
		}
		
		else
			die ("That username does not exists");
	
}
else
	die ("Please enter a username");
?>

But when I use this php code in my menu: 

<?php
		if ($_SESSION['username'])
		{
        echo "<li><a href='#'><span>$_SESSION['username'];</span></a>
        <ul>
        	<li><a href='client/account/logout.php'><span>Logout</span></a></li>
            <li><a href='client/account/profile.php'><span>Profile</span></a></li>
       	</ul>
        </li>";
		}
        ?>

But i get a error in the session section and I know its something with the $_session code.

But I dont know what to type in between the <span> and </span>.

This is the image I get in Adobe DreamWeaver:

imq6mq.png

Link to comment
https://forums.phpfreaks.com/topic/279579-show-username-in-echo-php/
Share on other sites

Hey Mate, 

 

Try this you are missing the " . . "

 

<?php
if ($_SESSION['username'])
{
echo "<li><a href='#'><span>" . $_SESSION['username'] . "</span></a>
<ul>
<li><a href='client/account/logout.php'><span>Logout</span></a></li>
<li><a href='client/account/profile.php'><span>Profile</span></a></li>
</ul>
</li>";
}
?>

or Create a variable like this

 

<?php
$username = $_SESSION['username'];
if ($username)
{
echo "<li><a href='#'><span>$username</span></a>
<ul>
<li><a href='client/account/logout.php'><span>Logout</span></a></li>
<li><a href='client/account/profile.php'><span>Profile</span></a></li>
</ul>
</li>";
}
?>

Hope this all helped, Also when echoing use ' instead of " single is faster only a small amount but it is faster in php. so instead of " . . " it would become ' . . ' in singles. 

 

Let me know if you have any problems! 

 

Brodey

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.