Jump to content

[SOLVED] Displaying a username when a user is logged in, HELP!


SauloA

Recommended Posts

I've created a login in page and I want the login form to appear when a user isn't logged in and hide the login form and displaying the username when the user is logged in.  I think I'm close to solving the problem but I'm probably missing some code.

 

Here's my code.  I'd appreciate the help.

 

<?php require_once('../Connections/connBlog.php'); ?>

<?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['username'])) {

 $loginUsername=$_POST['username'];

 $password=$_POST['password'];

 $MM_fldUserAuthorization = "al_levelid";

 $MM_redirectLoginSuccess = "home.php";

 $MM_redirectLoginFailed = "failed.php";

 $MM_redirecttoReferrer = true;

 mysql_select_db($database_connBlog, $connBlog);

   

 $LoginRS__query=sprintf("SELECT user_id, user_sname, user_pass, al_levelid FROM user_tbl WHERE binary user_sname='%s' AND binary user_pass='%s'",

 get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password));

 

 $LoginRS = mysql_query($LoginRS__query, $connBlog) or die(mysql_error());

 $LoginArray= mysql_fetch_array($LoginRS); //Creates the array

 $loginFoundUser = mysql_num_rows($LoginRS);

 if ($loginFoundUser) {

   

   $loginStrGroup  = mysql_result($LoginRS,0,'al_levelid');

   

   //declare three session variables and assign them

   $_SESSION['MM_Username'] = $loginUsername;

   $_SESSION['MM_UserID'] = $LoginArray['user_id'];

   $_SESSION['MM_UserLevel'] = $LoginArray['al_levelid'];

   $_SESSION['MM_UserGroup'] = $loginStrGroup;        

 

   if (isset($_SESSION['PrevUrl']) && true) {

     $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];  

   }

   header("Location: " . $MM_redirectLoginSuccess );

 }

 else {

   header("Location: ". $MM_redirectLoginFailed );

 }

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Blog Login</title>

<link href="mystyles.css" rel="stylesheet" type="text/css" />

</head>

 

<body>

<p><a href="home.php">Reviews</a> | <a href="users.php">Users</a> | <a href="shops.php">Shops</a> | <a href="customers.php">Customers</a> | <a href="subscriptions.php">Subscriptions</a> | <a href="invoices.php">Invoices</a> | <a href="logout.php">Logout</a>  </p>

<?php if ($_SESSION['MM_Username'] == 0) { ?>

<form ACTION="<?php echo $loginFormAction; ?>" METHOD="POST" name="frmLogin" id="frmLogin">

 <table>

   <caption>

     Login to the site

   </caption>

   <tr>

     <th align="right" scope="row">Username:</th>

     <td><input name="username" type="text" id="username" size="10" maxlength="10" /></td>

   </tr>

   <tr>

     <th align="right" scope="row">Password:</th>

     <td><input name="password" type="password" id="password" size="10" maxlength="10" /></td>

   </tr>

   <tr>

     <th align="right" scope="row"> </th>

     <td><input type="submit" name="Submit" value="Login" /></td>

   </tr>

 </table>

</form>

<?php } else { ?>

<p>Welcome <?php echo $_SESSION['MM_Username']; ?></p>

<?php } ?>

</body>

</html>

Link to comment
Share on other sites

Well whats actually happening at the moment? Also do you think you could modify your post and put [ code] [ /code] tags around it so we get syntax highlighting; makes it a lot easier to read.

 

One little problem i can spot, however, is this part:

 

if (!isset($_SESSION)) {

  session_start();

}

 

You should put session_start() before any attempt to do anything at all with sessions. As far as i can tell, the way you've set it up at the moment will probably prevent the sessions from being set when the person trys to log in.

 

so just change it to session_start(); on its own. No need for the if statement.

Link to comment
Share on other sites

The problem isn't in the:

 

if (!isset($_SESSION)) {

  session_start();

}

 

The problem is most likely in this region near the bottom:

 

<?php if ($_SESSION['MM_Username'] == 0) { ?>
<form ACTION="<?php echo $loginFormAction; ?>" METHOD="POST" name="frmLogin" id="frmLogin">
 <table>
   <caption>
     Login to the site
   </caption>
   <tr>
     <th align="right" scope="row">Username:</th>
     <td><input name="username" type="text" id="username" size="10" maxlength="10" /></td>
   </tr>
   <tr>
     <th align="right" scope="row">Password:</th>
     <td><input name="password" type="password" id="password" size="10" maxlength="10" /></td>
   </tr>
   <tr>
     <th align="right" scope="row"> </th>
     <td><input type="submit" name="Submit" value="Login" /></td>
   </tr>
 </table>
</form>
<?php } else { ?>
<p>Welcome <?php echo $_SESSION['MM_Username']; ?></p>
<?php } ?>

Link to comment
Share on other sites

The problem isn't in the:

 

if (!isset($_SESSION)) {

  session_start();

}

 

Could you explain where you got to this conclusion? If I remember correctly $_SESSION is a predefined superglobal variable, meaning it's always set. If you're so confident that it's not wrong, please humor us by replacing it with just session_start().

 

As well, what output are you getting? We have no idea what's wrong if you don't tell us what output you get.

Link to comment
Share on other sites

Well, I changed:

 

if(!isset($_SESSION)) {

   session_start();

}

 

into:

 

session_start();

 

and I get no errors and the page works fine.  But, that wasn't the issue I was trying to solve.  I want the login form to appear when a user isn't logged in.  If a user is logged in then the form should be hidden and "Welcome username" should appear.

Link to comment
Share on other sites

I'm essentially trying to check is there is anything in my session variable.  If my session variable is empty then it should display the login form.  If there is something in the session variable then the login form should no longer display and "Welcome 'username' " should appear.  My " if " statement begins here:

 

<?php if ($_SESSION['MM_Username'] == 0) { ?>
<form ACTION="<?php echo $loginFormAction; ?>" METHOD="POST" name="frmLogin" id="frmLogin">
 <table>
   <caption>
     Login to the site
   </caption>
   <tr>
     <th align="right" scope="row">Username:</th>
     <td><input name="username" type="text" id="username" size="10" maxlength="10" /></td>
   </tr>
   <tr>
     <th align="right" scope="row">Password:</th>
     <td><input name="password" type="password" id="password" size="10" maxlength="10" /></td>
   </tr>
   <tr>
     <th align="right" scope="row"> </th>
     <td><input type="submit" name="Submit" value="Login" /></td>
   </tr>
 </table>
</form>
<?php } else { ?>
<p>Welcome <?php echo $_SESSION['MM_Username']; ?></p>
<?php } ?>

 

but I don't know how to do what I explained above.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.