Skip to main content
Known Participant
September 29, 2009
Question

How and where to add code to login page

  • September 29, 2009
  • 13 replies
  • 2781 views

I have a login page which uses the Dreamweaver Login Behavior.  I want to add code to:

     Check the status of the person loging in (status is the last field in the mySQL authentication table).

          a.     If the person is already logged in, display an error message, and reject the attempt.

          b.     If the person is NOT currently logged in, set his status accordingly, and log him in.

Since the behavior is generated code, and well above my head, I don't know what code to enter, and where to enter it.  Here's the code for the form:

<?php require_once('Connections/login.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['username'])) {
  $loginUsername=$_POST['username'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "WOTCPg1.php";
  $MM_redirectLoginFailed = "loginerror.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_login, $login);
 
  $LoginRS__query=sprintf("SELECT UserID, Password FROM authentication WHERE UserID=%s AND Password=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
  
  $LoginRS = mysql_query($LoginRS__query, $login) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
   
    //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 );
  }
}
?>
<!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>

<script type="text/javascript">
function setFocus()
{
document.getElementById("username").focus();
}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Log-in to IOD</title>
<style type="text/css">
<!--
#apDiv3 {
position:absolute;
left:5px;
top:101px;
width:760px;
height:612px;
z-index:2;
font-family: Arial, Helvetica, sans-serif;
font-size: 14pt;
font-style: normal;
line-height: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
color: #009;
text-align: center;
background-image: url(images/grid_gray.gif);
border: medium ridge #009;
}
#apDiv1 {
position:absolute;
left:94px;
top:227px;
width:629px;
height:282px;
z-index:3;
font-family: Arial, Helvetica, sans-serif;
font-size: 14pt;
font-style: normal;
line-height: normal;
font-weight: bold;
font-variant: normal;
text-transform: none;
color: #009;
text-align: center;
}
#apDiv2 {
position:absolute;
left:178px;
top:248px;
width:564px;
height:254px;
z-index:3;
font-family: Arial, Helvetica, sans-serif;
font-size: 14pt;
font-style: normal;
line-height: normal;
font-weight: bold;
font-variant: normal;
text-transform: none;
color: #009;
text-align: left;
border: medium groove #036;
}
#apDiv4 {
position:absolute;
left:178px;
top:245px;
width:566px;
height:242px;
z-index:3;
border: medium groove #009;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
font-size: 14pt;
font-style: normal;
line-height: normal;
font-weight: bold;
font-variant: normal;
text-transform: capitalize;
color: #009;
}
.SubmitButton {
color: #009;
}
-->
</style>
</head>

<body onLoad="setFocus()" >

<span style="position:absolute; left:3px; top:2px; width:758; height:89;"><img src="sitebuilder/preview/sitebuilder/clipart/bars/regular/horizontal/sleekLines_blue.gif" width="760" height="90" alt="" /></span>
<div id="e2" style="position:absolute; left:13px; top:14px; width:670px; height:32;"><span class="text"><b><span style="font-size: 22px"><font color="#FFFFFF">Sign-in to Information On Demand</font></span><font color="#FFFFFF" size="4"><span style="font-size:22px;line-height:26px;"><br soft="soft" />
</span></font></b></span></div>
<div id="apDiv3">
  <p><span style="text-align: center; text-decoration: underline; color: #009; text-transform: capitalize; font-variant: normal; font-weight: bold; line-height: normal; font-style: normal; font-size: 18pt; font-family: Arial, Helvetica, sans-serif;">Customer Sign-In</span><span style="position:absolute; left:12px; top:23px; width:151px; height:65;"><img src="sitebuilder/images/redInfoOnDemandLogo-150x65.jpg" width="149" height="65" alt="" /></span></p>
</div>
<div id="apDiv4">
<form ACTION="<?php echo $loginFormAction; ?>" id="login" name="login" method="POST">
    <p>     </p>
    <p>User Name:<br /><input name="username" type="text" id="username" tabindex="1" size="30" maxlength="30" />
    </p>
    <p>User's Password:<br />
      <input type="password" name="password" id="password" tabindex="2" />
    </p>
    <p>
      <input name="submit" type="submit" class="SubmitButton" id="submit" tabindex="3" style="color:#009; font-weight:bold" value="Sign-in" />
   <input type="reset" name="reset" id="reset" value="Reset" tabindex="4" style="color:#009; font-weight:bold">

    </p>
  </form>
</div>
<br />
</div>
</body>
</html>

Here's the code for the script "included" in the first line of the page:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_login = "localhost";  // Change to "mysql" when uploaded to Yahoo
$database_login = "infoondemand";
$username_login = "root";   // Change to "creacontech" when uploaded to Yahoo
$password_login = "raisin4312";
$login = mysql_pconnect($hostname_login, $username_login, $password_login) or trigger_error(mysql_error(),E_USER_ERROR);
?>

Could someone help me with this?  I'd very much appreciate any assistance.

This topic is closed to new replies. Start a new post to keep the conversation going.

13 replies

Known Participant
October 9, 2009

There's no sense in beating a dead horse. We've pretty much chased this problem as far as it can go, so thanks everyone for the help

Lon_Winters
Inspiring
October 10, 2009

I guess I'm not clear on why a database field needs to be checked to see if the user is logged in or not.  This can be done very easily just by checking for the existence of the session variable that DW created when someone logs in using the standard behavior.  What am I missing?

Known Participant
October 10, 2009

More likely that I'm the one who's unclear about the whole schmear, being a dinosaur newbie (50 years of programming and brand new to web programming).  After all that time, when I look at these web languages, I get the distinct impression that I've never seen a computer in my life.  That said, I guess I'm unclear about your answer.

For example, if User-A logs in, admittedly certain session variables are created, but I was under the impression that they were ONLY for that session.  So are you saying then, that if User-B logs in while User-A is still in, that User-B will get the EXACT SAME session variables?  I would have thought each user would have his own session, hence his own session variables.  Please 'splain this to me.  My only concern is to disallow two users to concurrently use the same login (i.e. username & password), and that's why I included a "login status" field in the MySQL authentication table.

If there's a better way to do that then my "hammer & tongs" methodology, by all means, please tell me what it is, because I'm learning and very much appreciate any and all the help that I can get.

Known Participant
October 9, 2009

PLEASE, someone help me with this problem.  The "authentication" table also has a customer id field "CustID", that I need to pass via session variables, to the succeeding pages, in order for them to access the customer and applicant tables.  I've tried to add code to the (DW Generated code) above, but every time I do, the Dreamweaver "Server Behaviors" panel duplicates the behavior, and I get error messages indicating that DW can't tell which one to use.  For example:

First, I added CustID to the SELECT line as follows:

  $LoginRS__query=sprintf("SELECT UserID, Password, UserLevel, CustID FROM authentication WHERE UserID=%s AND Password=%s",
  GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
 

and that didn't cause any problems yet.  But then when I added a line to fetch the field:

    $loginStrGroup  = mysql_result($LoginRS,0,'UserLevel');
$custid   = mysql_result($LoginRS,0,'CustID'); // this is the line that I added
  

the server behavior panel showed duplicate "Log In User" entries, and I got the error message.  I also tried other code, such as:

  $arrLogin=mysql_fetch_assoc($LoginRS);
  $custid  = $arrLogin[CustID];


but that gave me the same result.  I then tried to edit the behavior, but when I selected that from the behavior panel plus sign drop menu, the "Server Behaviors" box was empty, and wouldn't accept anything, and the "Edit" & "Remove" buttons were greyed out.

What can I do to make this work?

Participating Frequently
October 9, 2009

>I've tried to add code to the (DW Generated code)

>above, but every time I do, the Dreamweaver "Server

>Behaviors" panel duplicates the behavior, and I get

>error messages indicating that DW can't tell which

>one to use.

That's the problem with server behaviors. Once you modify them, it can confuse DW. The server behaviors usually don't provide enough functionality for me to be really useful, and modifying them causes problems. So I usually code by hand.

Known Participant
October 9, 2009

That's the problem with server behaviors. Once you modify them, it can confuse DW.

Well that answers a lot of questions in my mind, and removes a lot of my confusions.

The server behaviors usually don't provide enough functionality for me to be really useful, and modifying them causes problems. So I usually code by hand.


I could agree with that if I were experience enough to correctly write the manual code.  The fact that I'm a dunce learner entices me to use generated code when it's available.

The fact is that when DW started giving me these problems, I tried to code it myself and failed miserably.  I cut an pasted code I found on various helpful websites, such as w3schools, homeandlearn, html-form-guide and others.  I must have tried dozens of these, but non of them worked.

In any event, after correcting some glaring errors, now when I run the login page, everything seems to work OK in the browsers, so I guess it doesn't matter that the behavior panel is screwy.

Thanks for the reply, however.  At least you've put a blessing on what's happening, and I appreciate it.

Known Participant
September 29, 2009

My discovered solution to my "Rock and Hard Place" question partly answered this one as well.  However, I still need to know where, what kind & how to add code to change the status field in my login database from "Out" to "In" on login, so that someone else can't login with the same user/password until the one loged in logs out.  Then, of course visa versa on logout.