会话变量在网站中不起作用

时间:2013-09-01 02:39:51

标签: php html session-variables

我尝试使用Dreamweaver CS6创建一个网站,该网站显示分配给您的某些任务。用户名存储在名为' MM_Username'的会话变量中。但是,我无法识别的东西是错误的。这两个页面都是用php编写的。这是我的登录页面:

<?php require_once('Connections/RoBros_To_Do_List_Database.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 = "MainPageUser.php";
    $MM_redirectLoginFailed = "Invalid Login Page.html";
    $MM_redirecttoReferrer = false;
    mysql_select_db($database_RoBros_To_Do_List_Database, $RoBros_To_Do_List_Database);

    $LoginRS__query=sprintf("SELECT Username, Password FROM users WHERE Username=%s AND Password=%s",
      GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));

    $LoginRS = mysql_query($LoginRS__query, $RoBros_To_Do_List_Database) 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 );
    }
  }
?>
<!doctype html>
<html>
<head>
  <meta charset=utf-8>
  <title>User Login</title>
  <link href="Log-In Screen Styling.css" rel="stylesheet" type="text/css" />
  <link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
  <script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
</head>

<body>
  <div class="headBar">
    <a href="index(userLogin).php">
      <img src="logo.png" alt="RoBro's To Do List" longdesc="logo.png" />
    </a>
    <p class="headBarRight">
      <input type="button" class="signUpLinkButton" value="Sign Up" onclick="window.location='newUser.php'"/>
    </p>
  </div>
  <br />
  <br />
  <div class = "loginDiv">
    <div class="signInWords">User Sign In</div>
    <br />
    <form ACTION="<?php echo $loginFormAction; ?>" name="loginChecker" method="POST" id="loginChecker">
      <label for="username" class="loginLabels">Username:</label>
      <br />
      <span id="spryUsLogUs">
        <input name="username" type="text" class = "loginEntryField">
        <span class="textfieldRequiredMsg">A value is required.</span>
      </span>
      <br />
      <br />
      <label for="password" class="loginLabels">Password:</label>
      <br />
      <span id="spryUsLogPa">
        <input type="password" name="password" class = "loginEntryField"/>
        <span class="textfieldRequiredMsg">A value is required.</span>
      </span>
      <br />
      <br />
      <input type="submit" name="submit" class="submitPass" value="Sign In">
      <br />
      <a href="adminLogin.php" class="logSwitch">Admin Sign In</a>
    </form>
  </div>
  <script type="text/javascript">
    var sprytextfield1 = new Spry.Widget.ValidationTextField("spryUsLogUs", "custom");
    var sprytextfield2 = new Spry.Widget.ValidationTextField("spryUsLogPa", "custom");
  </script>
</body>
</html>

这是显示任务的代码:

<?php require_once('Connections/RoBros_To_Do_List_Database.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 = "index.php";
    if ($logoutGoTo) {
      header("Location: $logoutGoTo");
      exit;
    }
  }
?>
<?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;
    }
  }

  $colname_getTasks = "-1";
  if (isset($_SESSION['MM_Username'])) {
    $colname_getTasks = $_SESSION['MM_Username'];
  }
  mysql_select_db($database_RoBros_To_Do_List_Database, $RoBros_To_Do_List_Database);
  $query_getTasks = sprintf("SELECT * FROM tasks WHERE `For` = %s ORDER BY Completed ASC", GetSQLValueString($colname_getTasks, "text"));
  $getTasks = mysql_query($query_getTasks, $RoBros_To_Do_List_Database) or die(mysql_error());
  $row_getTasks = mysql_fetch_assoc($getTasks);
  $totalRows_getTasks = mysql_num_rows($getTasks);
?>
<!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=utf-8" />
  <title>
        To-Do List
  </title>
  <link href="Main Page Styling.css" rel="stylesheet" type="text/css" />
  <script>var __adobewebfontsappname__="dreamweaver"</script>
  <script src="http://use.edgefonts.net/aladin:n4:default.js" type="text/javascript"></script>
</head>

<body>
  <div class="headBar">
    <a href="MainPageUser.php">
      <img src="logo.png" alt="RoBro's To Do List" longdesc="logo.png" />
    </a>
    <p class="headBarRight">
      &nbsp; <?php echo $_SESSION['MM_Username']; ?>
      <br />
      <input type="button" class="logoutButton" value="Logout" onClick="window.location='<?php echo $logoutAction ?>'"/>
    </p>
  </div>
  <br />
  <table width="800" border="1" class="taskTable">
      <tr class="tableHeader">
        <td>Task</td>
        <td>Uploaded By</td>
        <td>Completion</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>

      <?php do { ?>
      <tr>
        <td><?php echo $row_getTasks['Task']; ?></td>
        <td><?php echo $row_getTasks['uploadedBy']; ?></td>
        <td><?php echo $row_getTasks['Completed']; ?></td>
        <td><input type="button" class="taskAction" value="Set As Completed" onClick="window.location='completeTask.php?taskID=<?php echo $row_getTasks['taskID']; ?>'"/></td>
        <td><input type="button" class="taskAction" value="Delete" onClick="window.location='deleteTask.php?taskID=<?php echo $row_getTasks['taskID']; ?>'"/></td>
      </tr>
      <?php } while ($row_getTasks = mysql_fetch_assoc($getTasks)); ?>
  </table>
</body>
</html>
<?php
  mysql_free_result($getTasks);
?>

2 个答案:

答案 0 :(得分:1)

if (empty(session_id())) {
  session_start();
}

用此代码替换ur session session,因为当设置$ _SESSION时,session不会启动,所以你无法访问数据

答案 1 :(得分:0)

你不应该像这样开始会议

if (!isset($_SESSION)) {
  session_start();
}

只需在脚本顶部使用session_start()