我只是轉移了我的 PHP 應用程式,我曾經在 example.com/app 上擁有我的應用程式,但現在我將應用程式轉移到了它自己的域 othersite.com。我的主站點和 PHP 站點與 apache 虛擬主機位于同一臺服務器上,它在 ubuntu 20.xx 上運行。該應用程式運行良好,但我遇到了 PHP 會話問題,當我檢查我的 apache error.log 時發現了這個
PHP Notice: Undefined index: user_type in /var/www/othersite.com/public_html/index.php on line 66, referer: https://example.com/
這就是我在那條線上的內容,
if( $_SESSION['user_type'] == 'admin' || $_SESSION['user_type'] == 'doctor' || $_SESSION['user_type'] == 'nurse' ) { drawDashboardChecks();}
您認為這里可能存在什么問題?我沒有構建應用程式,但我猜代碼很好,因為它可以在以前的站點上運行,并且由于某種原因,錯誤日志顯示了對我的主要站點的參考。注意:我翻譯了 PHP 行上的值。謝謝你。
uj5u.com熱心網友回復:
通知告訴你——你的陣列中沒有那個user_type鍵。如果沒問題,請以您實際上并不希望user_type始終存在的方式更改支票:
if(
!empty($_SESSION['user_type'])
and in_array($_SESSION['user_type'], ['admin','doctor','nurse']
){
....
uj5u.com熱心網友回復:
首先應該檢查isset條件..
if(isset($_SESSION['user_type'])){
//code
}
uj5u.com熱心網友回復:
檢查請求現在已經發送到哪里,這可能是由于表單中寫入的 URLS 引起的。請看這個例子:
<form action="http://example.com/app/login.php" method="POST">
<input type="email" placeholder="email">
<input type="password" placeholder="password">
<form>
在您的 login.php 頁面中,您有一個處理請求的代碼。如果$_POST['email']收到了,那么這僅僅意味著它可以被處理,如果沒有收到它只是未定義的索引,你會看到你現在收到的錯誤。
看看下面的網址。
<form action="http://otherwebsite.com/app/login.php" method="POST">
<input type="email" placeholder="email">
<input type="password" placeholder="password">
<form>
它應該是這樣的。
<form action="http://otherwebsite.com/login.php" method="POST">
<input type="email" placeholder="email">
<input type="password" placeholder="password">
<form>
因此,請在嘗試處理請求之前檢查您將請求發送到哪里,最佳做法是isset()在處理索引之前使用 php 內置函式。
if(isset($_POST['email'])) {
$email = trim($_POST['email']);
} else {
$email_err = "Email is required";
}
希望你覺得這很有幫助。
快樂編碼!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442136.html
