You want to use $_SESSION.
When you determine a successful login has occurred, set a $_SESSION to = something, such as "loggedintrue" or whatever.
eg:
if($count==1)
{
$_SESSION['loggedin']['username'] = $myusername;
}
Then wherever you want to serve user specific data, do something like:
if (!empty($_SESSION['loggedin']['username']))
{
echo "Hi ".$_SESSION['loggedin']['username'];
// OR show some other content, ie allow then to see more menu items.
}
You will need to have session_start(); at the very top of any page you want to use the $_SESSION on.
Also, be aware that just not showing them a page or something they need to login first to see doesn't stop them accessing it. You'd need further checks on the page, such as:
if (empty($_SESSION['loggedin']['username']))
{
echo "You are not allowed to access this page. Please login first";
// Or redirect them etc
}
EDIT: Code you wanted.
You need to check if they are logged in, if yes serve their username and a link to logout.
If not logged in then show them the login/register link and text.
A pretty simple if else!
Where you have your div or whatever which holds the text "login/register", put this:
// They are logged in
if (!empty($_SESSION['loggedin']['username']))
{
// Display their username
echo $_SESSION['loggedin']['username'];
// Display the logout link
echo "<a href='logout.php'> (Logout)</a>";
}
// Not logged in
else
{
// Display the login/register link
echo "<a href='login-register.php'>Login/Register</a>";
}
The above code will work, however please note it's quite basic.
You should really have a separate class or function which you call on every single page to check if a user is logged in or not and return certain data if yes or no.
For example:
function logged_in()
{
if (!empty($_SESSION['loggedin']['username']))
{
return $_SESSION['loggedin']['username']."<a href='logout.php'> (Logout)</a>";
}
else
{
return "<a href='login-register.php'>Login/Register</a>";
}
}
Then include the file which has that above function in on every page you need it, then in the page where your username or login/register link is, do this:
echo logged_in();
This will display either they username, or a link to login/register.
Doing it this way means if you change anything you just change the function script file and it changes it everywhere you included the file and used the function.
Also you can simply use the logged_in() n your site wherever you want without checking sessions and setting data all the time.
It's a basic approach to separating business logic from presentation, but there's so much more to all this, such as where you keep your function files, how you include them within your application etc.
But way to much to talk about here. Read some tutorials on login and register with PHP and using include files in pages.
Side Notes:
Please make sure you validate and sanitise the user input data.
$myusername=$_POST["myusername"];
$mypassword=$_POST["mypassword"];
Maybe you do, but if not, as your code is they could wreak havoc on your database and/or website.
Also, consider using mysqli_ or PDO as the mysql_ functions are depreciated as from PHP version 5.5.0.
Also, md5() is no longer a secure method. See here for why and alternatives:
http://sg2.php.net/manual/en/faq.passwords.php#faq.passwords.fasthash