I am new to PHP and sessions so go easy on me. This is my code
if(isset($_POST["username"]) || isset($_SESSION["username"])){
$_SESSION["username"] = $_POST["username"];
echo $_SESSION["username"];
} else {
exit();
}
I'm getting an undefined index in the line that is supposed to define $SESSION["username"]. What am I doing wrong?
I figured it out. The problem occurs if isset($_POST["username"]) is false. Then when $_POST["username"] doesn't exist so it returns an error
try it
if(isset($_POST["username"])){
$_SESSION["username"] = $_POST["username"];
echo $_SESSION["username"];
} else if (isset($_SESSION["username"])) {
echo $_SESSION["username"];
} else {
exit();
}
you're checking if your $_POST
variable is set or $_SESSION
variable is set, so what's happening here is your if
statement is returning true because your $_SESSION
variable is already set. And don't forget to start your session using session_start()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.