4

How can i change the "login/register" label in the vertical menu into the username after the user had login successfully.

Here is my checklogin.php

<?php
  define("DB_HOST","localhost");
  define("DB_NAME","user_database");
  define("DB_USER","root");
  define("DB_PASSWORD","");

    mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
    mysql_select_db(DB_NAME) 
        or die("Failed to connect to the database");

    $myusername=$_POST["myusername"];
    $mypassword=$_POST["mypassword"];
    $myencryptedpassword=md5($mypassword);  
    $query="SELECT * FROM users WHERE username='$myusername' AND     password='$myencryptedpassword'";
    $result=mysql_query($query);
    $count=mysql_num_rows($result);

        if($count==1){
        echo "Login Successful.Welcome ".$myusername;
        sleep(2);
        header("refresh:1;url=onlinestore.html");
             }
         else{
         echo "Wrong Username or Password.Please try again. ";
         sleep(2);
         header("refresh:2;url=login.html");}
         ?> 

This code will only show the username in a new page.What i want is like the images below.

Before before login

After after login

JK9
  • 350
  • 2
  • 5
  • 25

4 Answers4

6

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

James
  • 4,529
  • 4
  • 35
  • 48
  • Is this going to display username in my homepage(onlinestore.html) at the top right corner?? – JK9 Dec 16 '13 at 12:22
  • Have a read about PHP sessions. http://www.php.net/manual/en/intro.session.php. Once set, as it's a superglobal, it can be used anywhere in any page, as long as you call `session_start()` at the top of the page first. Obviously you'll have to place it in the top right corner yourself – James Dec 16 '13 at 12:27
  • Can i do this inside a html file instead of php? – JK9 Dec 16 '13 at 12:35
  • Not as standard, you'll need a .php file extension so the PHP server will parse PHP code in the file. There is another option: http://stackoverflow.com/questions/11312316/how-do-i-add-php-code-to-html-files – James Dec 16 '13 at 12:44
  • i have edited the question James. Can i do something like the that with the code you provide to me? – JK9 Dec 16 '13 at 12:59
  • @JK9 done :) I'm happy to help you and have provided some working code above, however we're getting to the point where you're asking for code without you understanding it yourself. You shouldn't really just be pasting code into your site without understanding it as you may be introducing errors, security risks, etc. You should spend some time reading PHP tutorials etc. Good luck. – James Dec 16 '13 at 22:04
  • it is still not working~ – JK9 Dec 18 '13 at 08:38
  • @JK9 You'll have to be more descriptive than that. Open a new question and, as always, provide all *relevant* code and a good description of what doesn't work, what you tried and what you want it to do – James Dec 18 '13 at 09:25
  • i'hv post a new question here: http://stackoverflow.com/questions/20654848/html-php-display-username-after-success-login – JK9 Dec 18 '13 at 09:57
  • can you just go and have a look and help me if possible – JK9 Dec 18 '13 at 09:57
1

You maintain a session following.

{

session_start();

$_SESSION['username'] = $myusername;

if (!empty($_SESSION['username']))

{

echo "Hi ".$_SESSION['username'];

}

else

{ echo "You are not loggin";

}
Funk Forty Niner
  • 74,372
  • 15
  • 66
  • 132
0

You should save all row data against logged-in user in session. Many CMS uses this approach. After this you can check if that session is not empty then change its html.

Umair Hamid
  • 3,310
  • 3
  • 20
  • 24
-1

You must have to create sessions for login. you can use any identifier of a user such as username or user-id for that session which tells that the user is logged in or not.

and regarding the label thing, you must check the session value is set and echo the label.

<?php
session_start();
if(isset($_SESSION['user_id']))
echo 'Loged in';
?> 
RononDex
  • 3,992
  • 21
  • 35
  • What if `$_SESSION['user_id'] = '';` ? They are logged in and it will echo out nothing for their username. – James Dec 16 '13 at 12:39