Novice
Novice

Reputation: 35

print php session username

I am trying to get the username from the logged in user to display on a forum post. When the user creates a topic or answers a thread, their username is automatically printed on their post. My forum is super basic and already connects to a seperate database.

I validate my login pages with the following:

    <?php session_start(); 
    if(!session_is_registered(myusername)) { 
    header("location:home.php"); 

} 
?>

Also, I want to make sure that the username that is posted remains on their post after they have logged out, and when the post is viewed by other members.

Any ideas?

Upvotes: 0

Views: 2014

Answers (4)

Joko Wandiro
Joko Wandiro

Reputation: 1987

Don't use session_register() and session_is_registered() for checking session variable because it's deprecated. I suggest you do like this :

// save variable session
$_SESSION['username']= "your_usename";

// check variable session
if(!isset($_SESSION['username'])){
   header("location:home.php"); 
}

Upvotes: 0

BRampersad
BRampersad

Reputation: 862

To be able to receive the username, you must be storing it somehow. If you are storing it using $_SESSION['username'] = 'Username'; then getting it is an simple as using the variable $_SESSION['username'].

Upvotes: 3

jlindenbaum
jlindenbaum

Reputation: 1881

You should be storing the user name in something like $_SESSION['username'] or something similar.

Upvotes: 0

Cloudson
Cloudson

Reputation: 65

which your php version? In the PHP 5.3 this function session_is_registred() is deprecated.

Upvotes: 0

Related Questions