-1

Third time posting here as it hasn't let me down yet, i have created a website, it is hosted offline @localhost, i am using Mysqli and php to allow users to register on the website and login, when the user registers they are re-directed to a login page, after the login they are then redirected to index.php, what i would like to have is, somewhere at the top of the page a line where it says Logged in as User X

Then maybe Click here to log out, just a simple line, so what i need is after the user logs in it has to retrieve the users name from the database and display it, it doesn't need to be combined with the click here to log out code as i can manage that.

I have been looking online for tutorials but haven't been able to find any that help a great deal, mostly for creating entire user profile structures and i don't want to go that far into it just yet.

As always any help at all is greatly appreciated!

Ryan.

5
  • You just need to maintain a $_SESSION or $_COOKIE variable, of the logged in user. If the user wants to log out, just unset the variable
    – Ikari
    Commented Aug 14, 2015 at 9:56
  • So you have the name/username stored in your "users" table?, do you also have a cookie or session value saved to identify that user?
    – Izion
    Commented Aug 14, 2015 at 10:00
  • @Izion Yes the name/username is stored in my users table, no I'm going to add that now after seeing the other reply.
    – Ryan
    Commented Aug 14, 2015 at 10:08
  • Perform a authentication query and upon successful authentication save the username in the session variable and display that session variable. for logout unset the session variable and destroy it. Commented Aug 14, 2015 at 10:46
  • I have it working now, thank you for answering tho!
    – Ryan
    Commented Aug 14, 2015 at 11:40

1 Answer 1

0

After login success, you must create a session that contain username:

$_SESSION['username']=?//username of logged in user

Now in header if your index page create a query from user table like this

$query = "select * from user where username='{$_SESSION['username']}'"

Now execute this query its return logged in user info

5
  • 2
    Be careful of session fixation, use a random hashed key that is generated and deleted with each login. Do not store the username in $_SESSION.
    – Izion
    Commented Aug 14, 2015 at 10:03
  • i described simplest login strategy
    – M Gholami
    Commented Aug 14, 2015 at 10:07
  • Ok ill try that now! Thanks for commenting, ill update in a few to let you know if it works or not.
    – Ryan
    Commented Aug 14, 2015 at 10:07
  • @MohammadGholami Your answer is correct, I was only pointing out that you should not use the username :)
    – Izion
    Commented Aug 14, 2015 at 10:09
  • Have it working now, thank you to everyone who replied!
    – Ryan
    Commented Aug 14, 2015 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.