Session Variables

A Member? Please Login  
type your username and password to login
Date: December 01, 2008, 09:21 PM
267041 members and 164013 Topics
Latest Member: seunakom
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Webmasters (Moderator: OmniPotens)  |  Session Variables
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: Session Variables  (Read 116 views)
phantomcat (m)
Session Variables
« on: July 29, 2008, 08:55 AM »

guys i need some help here.

I am working on a php mysql job, the issue I've created an app that i want to use session variables for. so that when a user logs in, he will be identified sitewide. i am working with dreamweaver. though i have put in the server behaviours on the pages it is not showing the loged in user details.

please i am open for suggestions
yawa-ti-de (f)
Re: Session Variables
« #1 on: July 29, 2008, 12:15 PM »

phantom,

At times like this, it helps to show us your code.  Perhaps you are using the code right but within the wrong context.  There is no way of knowing unless you paste something for us here.
Tech Pros (m)
Re: Session Variables
« #2 on: July 29, 2008, 03:21 PM »

post your code so we can help you out, yawe is right there is no way anyone can help without seeing your code.
phantomcat (m)
Re: Session Variables
« #3 on: July 29, 2008, 03:29 PM »

this is the source code from one of the pages.

<?php require_once('Connections/gidox.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  $_SESSION['MM_Username'] = NULL;
  $_SESSION['MM_UserGroup'] = NULL;
  $_SESSION['PrevUrl'] = NULL;
  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);
   
  $logoutGoTo = "logout.php";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
  // For security, start by assuming the visitor is NOT authorized.
  $isValid = False;

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
  // Therefore, we know that a user is NOT logged in if that Session variable is blank.
  if (!empty($UserName)) {
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
    // Parse the strings into arrays.
    $arrUsers = Explode(",", $strUsers);
    $arrGroups = Explode(",", $strGroups);
    if (in_array($UserName, $arrUsers)) {
      $isValid = true;
    }
    // Or, you may restrict access to only certain users based on their username.
    if (in_array($UserGroup, $arrGroups)) {
      $isValid = true;
    }
    if (($strUsers == "") && true) {
      $isValid = true;
    }
  }
  return $isValid;
}

$MM_restrictGoTo = "sorry.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo);
  exit;
}
?>
<?php
mysql_select_db($database_gidox, $gidox);
$query_news = "SELECT * FROM news ORDER BY `no` ASC";
$news = mysql_query($query_news, $gidox) or die(mysql_error());
$row_news = mysql_fetch_assoc($news);
$totalRows_news = mysql_num_rows($news);

$colname_user = "-1";
if (isset($_SESSION['ID'])) {
  $colname_user = (get_magic_quotes_gpc()) ? $_SESSION['ID'] : addslashes($_SESSION['ID']);
}
mysql_select_db($database_gidox, $gidox);
$query_user = sprintf("SELECT * FROM reg WHERE ID = %s", $colname_user);
$user = mysql_query($query_user, $gidox) or die(mysql_error());
$row_user = mysql_fetch_assoc($user);
$totalRows_user = mysql_num_rows($user);

// Trim by length (by FELIXONE.it)
function TrimByLength($str, $len, $word) {
  $end = "";
  if (strlen($str) > $len) $end = ", ";
  $str = substr($str, 0, $len);
  if ($word) $str = substr($str,0,strrpos($str," ")+1);
  return $str.$end;
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Go-GO Global member downline</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="images/BrightSide1.css" type="text/css" />
<style type="text/css">
<!--
.style3 {color: #CC0000; font-weight: bold; }
.style4 {
   color: #000099;
   font-style: italic;
}
.style5 {color: #330000}
.style1 {   color: #6297BC;
   font-weight: bold;
}
-->
</style>
</head>
<body>
<!-- wrap starts here -->
<div id="wrap">
  <div id="header">
    <h1 id="logo">Gidok<span class="green"> Global</span><span class="gray"> Enterpise</span></h1>
    <h2 id="slogan">, giving you a lift.</h2>
    <!-- Menu Tabs -->
<ul>
      <li><a href="index.html"><span>Home</span></a></li>
<li><a href="admin_area.php"><span>About</span></a></li>
<li><a href="registration.php"><span>Register</span></a></li>
<li><a href="support.php"><span>Support</span></a></li>
<li><a href="contact.php"><span>Contact</span></a></li>
</ul>
  </div>
  <!-- content-wrap starts here -->
  <div id="content-wrap"> <img src="images/headerphoto.jpg" width="820" height="120" alt="headerphoto" class="no-border" />
    <div id="sidebar" >
      <h1>&nbsp;</h1>
      <ul class="sidemenu">
        <li><a href="index.html">Home</a></li>
        <li><a href="about.php">About us </a></li>
        <li><a href="registration.php">Register</a></li>
        <li><a href="<?php echo $logoutAction ?>"> Logout</a></li>
        <li><a href="services.php">Services</a></li>
      </ul>
      <h1>Wise Words</h1>
      <p>&quot;Men are disturbed, not by the things that happen,
        but by their opinion of the things that happen.&quot;</p>
      <p class="align-right">- Epictetus</p>
    </div>
<div id="main">
      <h1>Gato <span class="green">Investment <span class="gray">Project</span> </span></h1>
      <table width="100%" border="1" cellspacing="1" cellpadding="1">
        <tr>
          <td colspan="2"><strong>Welcome <span class="style5">
            <?php if ($totalRows_user > 0) { // Show if recordset not empty ?>
              <?php /*START_PHP_SIRFCIT*/ if ($row_user['ID']=="-1"){ ?>
                <?php echo $row_user['firstname']; ?>
                <?php } /*END_PHP_SIRFCIT*/ ?>
              <?php } // Show if recordset not empty ?>
            <?php if ($totalRows_user > 0) { // Show if recordset not empty ?>
              <?php echo $row_user['surname']; ?>
              <?php } // Show if recordset not empty ?></span></strong></td>
        </tr>
        <tr>
          <td colspan="2"><div align="center" class="style4">Below is the status of your downline </div></td>
        </tr>
        <tr>
          <td width="33%"><span class="style3">First Stage </span></td>
          <td width="67%">&nbsp;</td>
        </tr>
        <tr>
          <td><span class="style3">Second Stage </span></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td><span class="style3">Third Stage </span></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td><span class="style3">Forth Stage </span></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td><span class="style3">Fifth Stage </span></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td><span class="style3">Sixth Stage </span></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td><span class="style3">Seventh Stage </span></td>
          <td>&nbsp;</td>
        </tr>
      </table>
      <p><br />
    </p>
    </div>
    <div id="rightbar">
      <h1>News</h1>
      <table width="100%" border="1" cellspacing="1" cellpadding="1">
        <tr>
          <td><?php do { ?>
              <table width="100%" border="1" cellspacing="1" cellpadding="1">
                <tr>
                  <td valign="top"><p><span class="style1"><?php echo $row_news['subject']; ?></span><br />
                          <?php echo $row_news['date']; ?><br />
                          <br />
                          <?php echo TrimByLength($row_news['news_details'], 100, true); ?></p>
                  </td>
                </tr>
              </table>
            <?php } while ($row_news = mysql_fetch_assoc($news)); ?></td>
        </tr>
      </table>
      <h1>&nbsp;</h1>
    </div>
    <!-- content-wrap ends here -->
  </div>
  <!-- footer starts here -->
  <div id="footer">
    <div class="footer-left">
      <p class="align-left"> &copy; 2008 <strong>Gidok Global Enterprise </strong> |
        Design by <a href="http://www.robertrover.com/">Robert Rover Ent.</a></p>
    </div>
  </div>
  <!-- footer ends here -->
  <!-- wrap ends here -->
</div>
</body>
</html>
<?php
mysql_free_result($news);

mysql_free_result($user);
?>


expecting ur reply

members_area.php
* members_area.php (8.29 KB - downloaded )
yawa-ti-de (f)
Re: Session Variables
« #4 on: July 29, 2008, 03:44 PM »

wow phantom!  When we said paste your code, we didn't really mean paste all of it  Tongue

In any event, I assume when you say, it is not showing any details, you mean the first and last name.  Am I right?  If not, let us know.  If I am, then other minor issues aside, I noticed the following in your code which might be the reason why:

1) You have this check done twice. why?
if (!isset($_SESSION)) {
  session_start();
}

2) Why would you need the following to return -1?
if ($row_user['ID']=="-1"){
Chances are that if it is not -1, the details won't show.

Get back to us please.  Thanks.  By the way, thanks to your indentation. The code was very easy to read and follow.
Abidemi_A
Re: Session Variables
« #5 on: July 29, 2008, 05:19 PM »

Why not set breakpoints and use variables to check if your program logic is correct?
yawa-ti-de (f)
Re: Session Variables
« #6 on: July 29, 2008, 05:27 PM »

@Abidemi
And what of if he doesn't know anything about breakpoints (I don't know that he does or doesn't but I am just asking)?  Perhaps a little tutorial wouldn't be too bad here  Tongue
webdezzi (m)
Re: Session Variables
« #7 on: July 29, 2008, 07:00 PM »

You are asking if $_SESSION['ID'] exist here

Code:
$colname_user = "-1";
if (isset($_SESSION['ID'])) {
  $colname_user = (get_magic_quotes_gpc()) ? $_SESSION['ID'] : addslashes($_SESSION['ID']);
}

did you create that session on a previous page, because dreamweaver uses $_SESSION['MM_Username'] to store the username of the logged in user

as far as i can see, the above code is correct

In whatever case. use this and it should work
Quote
<?php
mysql_select_db($database_gidox, $gidox);
$query_news = "SELECT * FROM news ORDER BY `no` ASC";
$news = mysql_query($query_news, $gidox) or die(mysql_error());
$row_news = mysql_fetch_assoc($news);
$totalRows_news = mysql_num_rows($news);

$colname_user = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_user = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_gidox, $gidox);
$query_user = sprintf("SELECT * FROM reg WHERE username = '%s'", $colname_user);
$user = mysql_query($query_user, $gidox) or die(mysql_error());
$row_user = mysql_fetch_assoc($user);
$totalRows_user = mysql_num_rows($user);

// Trim by length (by FELIXONE.it)
function TrimByLength($str, $len, $word) {
  $end = "";
  if (strlen($str) > $len) $end = ", ";
  $str = substr($str, 0, $len);
  if ($word) $str = substr($str,0,strrpos($str," ")+1);
  return $str.$end;
}
?>

change the bolden text to the username column name of your reg table

 Affordable Web Hosting. Since 2002!  A List Of Your Companion Utility Websites For Web Developer/designers  Traffic Building For Adsense:   Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 
Google
 
Web www.nairaland.com
Sections: TV/Movies (2) Music/Radio (2) Celebrities Job Talk Jobs/Vacancies (2) Career Talk Romance Books Politics Sports Fashion Travel
Health Schooling Religion General(2) Business Webmaster Programming Computers Phones Cars & Trucks

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.