GuiceU
GuiceU

Reputation: 1038

Can't get the $_SESSION signed in username?

I'm trying to get the username that is signed in, but it wont work. Check this:

I've tried:

echo $_SESSION['username']; 

and I just get the

error: Notice: Undefined index: username

I know I have a 'variable' that is named username...

And i've also tried:

echo  $_GET['username']; 

but that wont work either, same error.

And i have session_start() and I have signed in to the database

Upvotes: 0

Views: 447

Answers (4)

Saic Siquot
Saic Siquot

Reputation: 6513

You need first aid. In php that is print_r

session_start();
print_r($_SESSION);

print_r will show the hole session array if username is not there, you must asign a value first to it

session_start();
$_SESSION['username'] = $_GET['username'];  // for example

Upvotes: 0

make sure when you set the $ _SESSION was you put session_start ()

Upvotes: 1

Manse
Manse

Reputation: 38147

Variables stored in the session are put there by your code - if you havent assigned anything to $_SESSION['username'] then you will recieve the error message Undefined index .. here is a basic example of using sessions :

<?php
// page1.php
session_start();
echo 'Welcome to page #1';
$_SESSION['favcolor'] = 'green'; // set a value
echo '<br /><a href="page2.php">page 2</a>';

then on page 2 :

<?php
// page2.php
session_start();
echo 'Welcome to page #2<br />';
echo $_SESSION['favcolor']; // green

In this simple example you can see that in page 1 you set a value $_SESSION['favcolor'] = 'green'; and then in page you retrieve the value '$_SESSION['favcolor']`

All docs for sessions are here

Upvotes: 0

You must manually assign a value to $_SESSION['username']. It is not put there automatically.

Upvotes: 0

Related Questions