Jump to content

Adding a session variable to the Log In Server Behavior, HELP.


SauloA

Recommended Posts

I am new to PHP and have been using Dreamweaver to develop my blog.  When a user logs into the website I want there "user_id" to be stored but Dreamweaver's Log In User Server Behavior doesn't store a session variable for the "user_id".  I tried modifying the code slightly to add a MM_UserID session variable but the "user_id" isn't being stored, I think.  When a user adds a comment the "user_id" is supposed to be filled into the form with the MM_UserID session variable into the hidden field.

Here's my code below.  The red text is what I added into the code.  I'd appreciate the help since this problem has been bugging me for 1 week.


[font=Verdana]<?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 [color=red]user_id[/color], user_sname, user_pass, al_levelid FROM user_tbl WHERE user_sname='%s' AND 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());
 $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;
[color=red]$_SESSION['MM_UserID'] = $loginFoundUser['user_id'];[/color]
   $_SESSION['MM_UserGroup'] = $loginStrGroup;      

   if (isset($_SESSION['PrevUrl']) && true) {
     $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
   }
   header("Location: " . $MM_redirectLoginSuccess );
 }
 else {
   header("Location: ". $MM_redirectLoginFailed );
 }
}
?>[/font]
Link to comment
Share on other sites

If you want to take a value from a query, you'll need to turn it into an array with mysql_fetch_array().  I've edited the code below, although it's untested so it might need something else.  Also, when posting your code, stick it in [ CODE ] tags.

[code]<?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 user_sname='%s' AND 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']; //Stores the user_id in a session variable.
    $_SESSION['MM_UserGroup'] = $loginStrGroup;       

    if (isset($_SESSION['PrevUrl']) && true) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl']; 
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>[/code]

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.