My websites have been using a php access control system for years. A recent "upgrade" to php 8.1 has caused the warning in the title to appear. I believe the code is still doing what it's supposed to, but the warnings get seen by the user, so I need to make them go away. Here's an example of my code that results in the warning.
// Check for credentials in the just-posted form or an existing session
// echo "Checking for credentials...<br>";
$uid = isset($_POST['uid']) ? $_POST['uid'] : $_SESSION['uid'];
$pwd = isset($_POST['pwd']) ? $_POST['pwd'] : $_SESSION['pwd'];
// echo "Found " & '$uid' & "<br>";
if(!isset($uid)) { // No credentials already set
// echo "No credentials found <br>";
// Present some HTML here to collect username and pwd to try again.
I tried the despised approach of using "@" to suppress the warning:
$pwd = @isset($_POST['pwd']) ? $_POST['pwd'] : $_SESSION['pwd'];
Didn't help. What's a good way out of this mess?
[UPDATE] Not very elegant but this works.
if (empty($_POST['uid'])) {
if (empty($_SESSION['uid'])) {
unset($uid);
} else {
$uid = $_SESSION['uid'];
}
} else {
$uid = $_POST['uid']; }
if (empty($_POST['pwd'])) {
if (empty($_SESSION['pwd'])) {
unset($pwd);
} else {
$pwd = $_SESSION['pwd'];
}
} else {
$pwd = $_POST['pwd']; }
if(empty($uid)) { //// No credentials
// echo "No credentials found <br>";
[SOLUTION] @Vee had the more elegant approach I was hoping for:
$uid = ($_POST['uid'] ?? ($_SESSION['uid'] ??''));
$pwd = ($_POST['pwd'] ?? ($_SESSION['pwd'] ??''));