no output for php session code being generated

47 Views Asked by At

I am having a problem regarding PHP sessions. this code is expected to display thank you for registering and the corresponding username and email id but it is not displaying anything! please help me out!

<?php
//initialize session data
session_start();
if(isset($_SERVER['username'])){
    echo "you are already logged in as: ".$_SERVER['username'];
}
elseif($_SERVER['REQUEST_METHOD' == 'POST']){
    if(!empty(trim($_POST['username'])) && !empty(trim($_POST['email']))){
        $uname = $_POST['username'];
        $email = $_POST['email'];
        echo "thanks for registering <br/>";
        echo "your username: $uname <br/>";
        echo "your email: $email <br/>";
    }
    else{
        echo "please fill both the fields properly";
    }
}
else{ ?>
<form action="index.html?username=overwritten" method="POST">
    <input type="text" placeholder="username here.." name="username"><br/>
    <input type="email" placeholder="email here.." name="email"><br/>
    <input type="submit" value="register">
</form>
<?php } 
?>

It is also displaying: Notice: Undefined offset: 0 in /Applications/XAMPP/xamppfiles/htdocs/samiary.com/index.php on line 7 on the top please help me out, I am just a beginner.

1

There are 1 best solutions below

0
On

You have error near $_SERVER['REQUEST_METHOD']. Try this:

<?php
//initialize session data
session_start();
if(isset($_SERVER['username'])){
    echo "you are already logged in as: ".$_SERVER['username'];
}
elseif($_SERVER['REQUEST_METHOD'] == 'POST'){
    if(!empty(trim($_POST['username'])) && !empty(trim($_POST['email']))){
        $uname = $_POST['username'];
        $email = $_POST['email'];
        echo "thanks for registering <br/>";
        echo "your username: $uname <br/>";
        echo "your email: $email <br/>";
    }
    else{
        echo "please fill both the fields properly";
    }
}
else{ ?>
<form action="" method="POST">
    <input type="text" placeholder="username here.." name="username"><br/>
    <input type="email" placeholder="email here.." name="email"><br/>
    <input type="submit" value="register">
</form>
<?php } 
?>