php - How to use session variables to output logged in username and properties

969

I'm building an application and have just added a Login page which works well, communicating with a "username" and "password" field in the database. After logging in, it takes you a page users are authorized to see.

Is there a way I can display "Welcome, xxx" to the particular user logged in at the next page? And more importantly, output detailed information of the logged in user on that next page? More technically, to output properties of the username which are in different tables in the database.

Now I know I should begin with:

<?php session_start();
$_SESSION['username'] = $user_name; ?>

But how do I specify assigning the value of "$user_name" to the value inputted in the username textbox.

133

Answer

Solution:

Use this

$_SESSION['user_name'] = $_POST['username'];
298

Answer

Solution:

I think using sessions is unnecessary for this task.

Simply search the database for the user's name once they log in and display this back to the page. Only store what's needed in sessions, databases are a more efficient storage medium.

People are also looking for solutions to the problem: Shorten a text string in PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.