Search code examples
phphtmlcsssessionusersession

user session and displaying user information


I am doing a project in which to enter the admin page you need username and password. The session is created once username and password are corrected or redirected to some other page is anyone of them is incorrect. My code for this is:

<?php

    include_once './connection.php'; 
    file_exists("connection.php");

    $username = $_POST['username'];
    $password = $_POST['password'];


    $query=pg_query("select password from users where username like '".$username."' ");

    $mypassword=pg_fetch_object($query)->password;

    if(($password==$mypassword)and($mypassword!=null)){
        $_SESSION["username"]="username";
        $_SESSION["password"]="passwoed";
        header("location:admin_page.php");
    }

    else{  
        header("location:attempt.php");
    }


    pg_close($connection); 


?>

In admin page, my code is :

<?php
    session_start();    
    if(!isset($_SESSION["username"])){
        header("location:homepage.php");
    }
?>

<!DOCTYPE html>
<html>
<title>Project</title>
<body>
<div id="loginContainer" align='center'>
    <font color='white'>Logged In As: <?php echo $_SESSION["username"]; ?></font>       </div>
</body>

The thing is I am directly redirected to homepage without being able to be in adminpage. Is something wrong with the session registration? But if I change the php code as, I am on the adminpage:

<?php
    session_start();    
    if(isset($_SESSION["username"])){
        header("location:homepage.php");
    }
?>

And I want to display the name of the user which I have saved through $_SESSION[] supervariable. I get the error message as Notice: Undefined index: username in C:\xampp\htdocs\admin_page.php.

Thanks in Advance


I changed the code as you told me.

But whether I write

<?php
    session_start();    
    if(!isset($_SESSION["username"])){
        header("location:homepage.php");
    }
?>

<!DOCTYPE html>
<html>
<title>Project</title>
<body>
<div id="loginContainer" align='center'>
    <font color='white'>Logged In As: <?php echo $_SESSION["username"]; ?></font>       </div>

OR

<?php
    session_start();    
    if(!isset($_SESSION["username"])){
        header("location:homepage.php");
    }
?>

<!DOCTYPE html>
<html>
<title>Project</title>
<body>
<div id="loginContainer" align='center'>
    <font color='white'>Logged In As: <?php echo $username; ?></font>       </div>
</body>

I get the same error. "Notice: Undefined index: username in C:\xampp\htdocs\admin_page.php." and I dont know what to do next. Please Suggest!!!


Solution

  • above code worked when I changed my code as:

    <?php
    
        session_start();
        include_once './connection.php'; 
        file_exists("connection.php");
    
        $username = $_POST['username'];
        $password = $_POST['password'];
    
    
        $query=pg_query("select password from users where username like '".$username."' ");
    
        $mypassword=pg_fetch_object($query)->password;
    
        if(($password==$mypassword)and($mypassword!=null)){
            $_SESSION["username"]=$username;//instead of "username"
            $_SESSION["password"]=$password;
            header("location:admin_page.php");
        }
    
        else{  
            header("location:attempt.php");
        }
    
    
        pg_close($connection); 
    
    
        ?>
    

    Thank You Geseft.