Store Session Username

Hello all, would like to wish everyone on here a very Merry Christmas and a prosporous new year! :smiley:

I want to enable users to comment on news stories, a user needs to login. I have created these two parts (the login and comments). I have a form which the user needs to complete to post a comment, such as title and message. I have a field which I want the users username to be echoed into (the field will be disabled).

Though not sure how I can carry the users username throughout the site to call it when needed. Any ideas?

The users name should be stored somewhere in the database.

You can store in the session the primary key that’s used to retrieve the user details from the database.

Hey dude, yeah I have the users name stored in the database. So how would I store the primary key in the session? Tried researching this one but can’t seem to find a solution very easily.

$_SESSION['username'] = $username_from_db; ? Or am I missing something?

Hi there, I’m using the following at the top of my restricted pages, seems to be be working but the username isn’t echoing out which I need really. Any further ideas? Merry Christmas by the way :smiley:

Apologies, here’s the script I’m using at the top of the page. Just not sure how I can echo out the username of the the user?

<?php
	//Start session
	session_start();
	include('includes/connection.php');
	$_SESSION['SESS_MEMBER_ID'];
	$_SESSION['SESS_LOGIN'] = $member['login'];
	
	//Check whether the session variable SESS_MEMBER_ID is present or not
	if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {
		header("location: access-denied.php");
		exit();
	}
?>

After session_start add this,
print_r($_SESSION);

your echo will look something like,
echo $_SESSION[‘SESS_USER_NAME’];
Depending on the print_r.

Hi dude, that’s perfect, that’s done it. Made it much easier to work out the error of my ways when using print_r - didn’t realize to use it. Thanks again dude :smiley: