简体   繁体   English

PHP 会话类 OOP

[英]Php Session Class OOP

I seem to be having problems logging in using the session variable name from the session class.我似乎在使用会话类中的会话变量名称登录时遇到问题。 I don't know what code I'm missing.我不知道我缺少什么代码。 The welcome page wont pick up the user logged in. below is the code.欢迎页面不会选择登录的用户。下面是代码。

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;
    }


}

The following is the code for the login page were I call the session object以下是我调用会话对象时登录页面的代码

//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']);

And here is the part were I want the stored session variable to appear on the welcome page.这是我希望存储的会话变量出现在欢迎页面上的部分。 Here the welcome page just wont pick up the stored session variables.这里的欢迎页面不会获取存储的会话变量。 Question is how can the session variables be passed on to this page问题是如何将会话变量传递到此页面

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";

Here is a little bit of code from the account class这是帐户类的一些代码

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;

}

I'm not going to start debugging your code for you - you may wish to read up on how to create a MCVE , but there is at least one glaring error here:我不会开始为你调试你的代码——你可能希望阅读如何创建MCVE ,但这里至少有一个明显的错误:

if(isset($_SESSION['$varname'])) #17

Those should be double quotes or no quotes.这些应该是双引号或没有引号。

(There are lots of things which can go wrong with PHP sessions - if you search some of the answers here you'll find out how to detect and remediate them) (PHP 会话可能会出现很多问题 - 如果您在这里搜索一些答案,您将了解如何检测和修复它们)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM