我是Php OOP的新手,已經編寫了一些代碼,用於使用PHP OOP在數據庫中存儲某些產品。我試圖將我的用戶名和密碼存儲在我的數據庫類的會話變量中。這是我的數據庫類代碼以及我的登錄表單。我在運行時出現以下錯誤。在數據庫類中存儲變量
解析錯誤:語法錯誤,意想不到 '$ _SESSION'(T_VARIABLE)在C:\ XAMPP \ htdocs中\ wdv341 \ PHP-OOP-CRUD級-3 \設置\ database.php中在第9行
database.php中
<?php
class Database{
// specify your own database credentials
private $host = "localhost";
private $db_name = "wdv341";
private $username = $_SESSION['username'];
private $password = $_SESSION['password'];
public $conn;
// get the database connection
public function getConnection(){
$this->conn = null;
try{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
}catch(PDOException $exception){
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
?>
userLogin.php
<?php
session_cache_limiter('none'); //This prevents a Chrome error when using the back button to return to this page.
session_start();
if (isset($_POST['username']) && isset($_POST['password'])) //This is a valid user. Show them the Administrator Page
{
$_SESSION['username']=$_POST['username']; //pull the username from the form
$_SESSION['password']=$_POST['password'];
//var_dump($_SESSION);
include_once 'config/database.php';
if (isset($_SESSION['username']) && ($_SESSION['password'])){
header("location:read_categories.php");
}
else
{
?>
<html>
<body>
<h2>Please login to the Administrator System</h2>
<form method="post" name="loginForm" action="userLogin.php" >
<p>Username: <input name="username" type="text" /></p>
<p>Password: <input name="password" type="password" /></p>
<p><input name="submitLogin" value="Login" type="submit" /> <input name="" type="reset" /> </p>
</form>
</body>
</html>
<?php
}
?>
請幫助!
非常感謝,這是修復! –