PHP session username

Soldato
Joined
28 Sep 2008
Posts
14,207
Location
Britain
Hi all,

after a login, the redirected page states

You are logged in as:

but for some reason this is coming up blank (or, if I change it to "user" it says Array).

I have a table (users) with username, firstname, lastname.

I want the firstname and lastname to be displayed on the redirected page.

Not sure whatother info you might need to help me with this?

The login php
PHP:
<?php
session_start();
require_once('includes/config.php');


if(isset($_POST['submitted'])) {

    $form = new wc_validation();

    $form->Text($_POST['username'],'UserName',false,0,50);
    $form->Text($_POST['password'],'Password',true);

    if (!$form->hasErrors){

        /* There no empty form issues. Process SQL requests to check if user is on the system */

        $sql = "SELECT * FROM users WHERE username = '" . $form->formData['UserName'] . "' ";

        if (!$result = $db->Select($sql)){

                /* no username match */

                   $form->addError('UserName','The username you entered does not appear to exist in our system. Please double check you have entered it correctly and try again');


        } else {

            /* username exists check password */

            $sql = "SELECT * FROM users WHERE username = '" . $form->formData['UserName'] . "' AND password = '" . md5($form->formData['Password']) . "' LIMIT 1";

            if (!$result = $db->Select($sql)){

                  /* user correct password not */
                  $form->addError('Password','The password does not match with the credentials we have on the system. Please try again');

            } else {

                    /* they have used the correct details and now can be redirected to the appropriate areas if they are admin or not. */
                    if ($result[0]['admin'] == '0'){

                        #redirect to non admin page 
                        $_SESSION['user']['username'] = $result[0]['firstname']." ".$result[0]['surname'];
                        $_SESSION['user']['level'] = $result[0]['admin'];


                       header('Location: client.php');
			exit;


                    }else {
                        #redirect to admin page

                         $_SESSION['user']['username'] = $result[0]['firstname']." ".$result[0]['surname'];
                        $_SESSION['user']['level'] = $result[0]['admin'];
                        header('Location: admin.php');
		   exit;

                    }
            }
        }
    }
}
?>

This is the php on the page that they get directed to:
PHP:
<?php
echo "<strong>You are logged in as:</strong> " . $_SESSION['user']['username'];
?>
 
Last edited:
session_start also on admin.php?

when it says "array" put "print_r()" instead of echo


This..

PHP:
<?php 
echo "<strong>You are logged in as:</strong> " . $_SESSION['user']['username']; 
?>

should be

PHP:
<strong>You are logged in as:</strong> <?php echo $_SESSION['user']['username']; ?>

Won't help but that's better formatting. Don't put unnecessary HTML in PHP.

Also try
PHP:
<pre> <?php print_r($_SESSION); ?> </pre>
 
Admin.php has this at the top

PHP:
<?php
session_start();
require_once('includes/config.php');

if(!$_SESSION['user']['username']) {
       //not logged in. redirect to login page
       header("Location: login.php");
       exit;
}


?>

Thanks for the HTML tip
 
Back
Top Bottom