Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php会话类OOP_Php_Mysql_Html - Fatal编程技术网

Php会话类OOP

Php会话类OOP,php,mysql,html,Php,Mysql,Html,我似乎在使用会话类中的会话变量名称登录时遇到问题。我不知道我丢失了什么代码。欢迎页面不会接收登录的用户。下面是代码 class Session { private $message; function __construct() { session_start(); } function getVariable($varname) { if(isset($_SESSION['$varname'])) #17 return

我似乎在使用会话类中的会话变量名称登录时遇到问题。我不知道我丢失了什么代码。欢迎页面不会接收登录的用户。下面是代码

class Session
{
   private $message;
   function __construct()
   {
      session_start();

   }

   function getVariable($varname)
   {
      if(isset($_SESSION['$varname'])) #17
      return $_SESSION['$varname'];
      else #19
      {
         $this->message = "No such variable in
         this session";
         return FALSE;
      }
   }

   function storeVariable($varname,$value)
   {
      if(!is_string($varname)) #29
      {
         throw new Exception("Parameter 1 is not a
         valid variable name.");
         return FALSE;
      }
      else
      $_SESSION['$varname'] = $value;
   }

   function getMessage()
   {
      return $this->message;
   }

   function login(Account $acct,$password) #44
   {
      if(!$acct->comparePassword($password)) #46
       {
          return FALSE;
       }
       $this->storeVariable("user_name", "user_name"); #47

       return TRUE;
    }


}
下面是我调用会话对象时登录页面的代码

//First time form is displayed. Form is blank. //
if (!isset($_POST['Button'])) #26
  {
    $form->displayForm();
    exit();
  }
// Process form that has been submitted with user info //
  else
{
   $sess = new Session(); #34

   try
   {
      $db = new Database("verybig.data"); #37
      $db->useDatabase("database"); #38
      $acct = new Account($db->getConnection(),"user");
   }
   catch(Exception $e)
   {
   echo $e->getMessage()."\n<br>";
   exit();
   }

 if(!$sess->login($acct,$_POST['password'])) #76
   {
      $GLOBALS['message_1'] = $acct->getMessage().
      " Please try again.";
      $form->displayForm();
      exit();

   }


   header("Location: companyhome.php"); #83
   exit();
try
{
  if($acct->selectAccount($newdata['user_name'])) #140
   {
     $GLOBALS['message_2'] =
       "Member ID already used.
            Select a new Member ID.";
     $form->displayForm();
     exit();
   }

   if(!$acct->createNewAccount($newdata)) #148
   {
      echo "Couldn’t create new account.
        Try again later.";
      exit();
   }

   $sess->storeVariable("user_name", $newdata['user_name']); #154
   $sess->storeVariable("user_dept", $newdata['dept_id']);
下面是我希望存储的会话变量出现在欢迎页面上的部分。在这里,欢迎页面不会选择存储的会话变量。问题是如何将会话变量传递到此页面

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



echo $_SESSION['user_name']. "<br>";



$admin = FALSE;
$base_url = "companyhome.php";
$trail = "<a href='$base_url'>Home</a>";
if (!isset($_SESSION['user_name']))
header("Location: login-oo.php");#31
else
{
if (isset($_SESSION['user_name'])
&& isset($_GET['dept_id']))
{
$admin = $_SESSION['user_dept'] == $_GET['dept_id'];
}
$left_nav_links = array();
$page["browse_level"] =
isset($_GET['browse_level']) ?
$_GET['browse_level'] : "home";

下面是account类中的一些代码

class Account
{
private $user_name = NULL;
private $cxn; // database connection object
private $table_name;
private $message;
function __construct( mysqli $cxn,$table)
{
$this->cxn = $cxn;
if(is_string($table)) #17
{
$sql = "SHOW TABLES LIKE '$table'"; #19
$result = $this->cxn->query($sql);
if($result->num_rows > 0) #21
{
$this->table_name = $table;
}
else #25
{
throw new Exception("$table is not a table
in the database");
return FALSE;
}
}
else #32
{
throw new Exception("Second parameter is not a
valid table name");
return FALSE;
}
}
function selectAccount($user_name)
{
$user_name = trim($user_name); #42
$sql = "SELECT user_name FROM $this->table_name
WHERE user_name ='$user_name'"; #44
if(!$result = $this->cxn->query($sql))
{
throw new Exception("Couldn't execute query: "
.$this->cxn->error());
return FALSE;
}
if($result->num_rows < 1 ) #51
{
$this->message = "Account $user_name
does not exist!";
return FALSE;
}
else #57
{
$this->user_name = $user_name;
return TRUE;
}
}
function comparePassword($password)
{
if(!isset($this->user_name)) #66
{
throw new Exception("No account currently selected");
exit();
} #70
$sql = "SELECT user_name FROM $this->table_name
WHERE user_name ='$this->user_name' AND
password = md5('$password')";
if(!$result = $this->cxn->query($sql)) #74
{
throw new Exception("Couldn't execute query: "
.mysql_error());
exit();
}
if($result->num_rows < 1 ) #80
{
$this->message = "Incorrect password for
account $this->user_name!";
return FALSE;
}
else #86
return TRUE;

}

我不会开始为您调试代码-您可能希望了解如何创建,但这里至少有一个明显的错误:

if(isset($_SESSION['$varname'])) #17
这些应该是双引号或无引号


PHP会话可能会出现很多问题-如果您在此处搜索一些答案,您将发现如何检测和纠正它们

$\u SESSION['$varname']~单引号将不允许解释$varname变量。最有可能的情况是,$\u SESSION[$varname]-这是在getVariable方法中,为什么在登录页面中创建新会话而不使用相同的会话值/SESSION\u start?好的。。。这是正确的,但是user_name是我回显会话时弹出的内容,而不是登录用户的名称。尽管如此,感谢您的帮助,但是存储的变量没有被回显