www.webdeveloper.com
Results 1 to 3 of 3

Thread: How can I display username of the logged in user?

  1. #1
    Join Date
    Apr 2011
    Location
    Nairobi
    Posts
    14

    How can I display username of the logged in user?

    I am trying to display the username of the logged in user in all my pages with php extensions, and when the user is logged, the link to 'logout' is displayed.

    I am using Adobe Dreamweaver CS5 for user authentication, so far the registration and login forms are working perfectly, able to direct user to right pages.

    Now my only problem, as I said above is to display the username of the logged in user, something like 'Welcome Somebody,'

    I have tried several ways from all over the net, but none seems to work at all, I know I must be going wrong somewhere, but I cannot tell where.

    I have therefore added my login form and the php below;

    This is the loggin form;

    Code:
     <form id="form4" name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
       <div class="reg_column"> 
       <span>Your Email: </span>
          <label for="m_email"></label>
          <input type="text" name="m_email" id="m_email" />
       </div>
       <div class="reg_column">  
       <span> Password: </span>
          <label for="m_password"></label>
          <input type="password" name="m_password" id="m_password" />
       </div>
         <div class="reg_column">
          <input type="submit" name="login" id="login" value="Login" />
        </div>
    </form>
    And this is the php before the html header, auto-generated by Dreamweaver

    Code:
    <?php require_once('Connections/myDB.php'); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }
    
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
    }
    ?>
    <?php
    // *** Validate request to login to this site.
    if (!isset($_SESSION)) {
      session_start();
    }
    
    $loginFormAction = $_SERVER['PHP_SELF'];
    if (isset($_GET['accesscheck'])) {
      $_SESSION['PrevUrl'] = $_GET['accesscheck'];
    }
    
    if (isset($_POST['m_email'])) {
      $loginUsername=$_POST['m_email'];
      $password=$_POST['m_password'];
      $MM_fldUserAuthorization = "";
      $MM_redirectLoginSuccess = "index.php";
      $MM_redirectLoginFailed = "registration_error.html";
      $MM_redirecttoReferrer = false;
      mysql_select_db($database_myDB, $myDB);
      
      $LoginRS__query=sprintf("SELECT useremail, password FROM members_table WHERE useremail=%s AND password=%s",
        GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
       
      $LoginRS = mysql_query($LoginRS__query, $myDB) or die(mysql_error());
      $loginFoundUser = mysql_num_rows($LoginRS);
      if ($loginFoundUser) {
         $loginStrGroup = "";
        
    	if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
        //declare two session variables and assign them
        $_SESSION['MM_Username'] = $loginUsername;
        $_SESSION['MM_UserGroup'] = $loginStrGroup;	      
    
        if (isset($_SESSION['PrevUrl']) && false) {
          $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
        }
        header("Location: " . $MM_redirectLoginSuccess );
      }
      else {
        header("Location: ". $MM_redirectLoginFailed );
      }
    }
    ?>
    That is it, just to let you know, from what I have check elsewhere, the most common is the one which says I should add a username to global variable:

    Code:
    $_SESSION['username'] = $username;
    the display the username on the page;

    Code:
    <?php echo $_SESSION['username']; ?>
    Looks like it is so simple but for me its just not working...
    I'm not sure where and on which page should I add the global variable.

    Please someone help, I'll really appreciate.

    Cheers.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,331
    You need to do a session_start(); on each and every page where you want to reference the $_SESSION array. The session_start() must be done before anything at all gets output to the browser, and before any reference to $_SESSION (whether reading from or saving to it).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2011
    Location
    Nairobi
    Posts
    14
    Thanks NogDog,
    Problem solved...
    I wast starting the session on each page.. and other problem is that i hadn't noticed that username variable is already defined by Dreamweaver as


    $_SESSION['MM_Username']


    It was actually that simple...

    Thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles