Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7866473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:24:43+00:00 2026-06-03T00:24:43+00:00

I just started learning PHP and am having some trouble getting a simple login

  • 0

I just started learning PHP and am having some trouble getting a simple login page to work. When I load this script the first time, the text “Wrong password/username.” and the logout button are printed but not the login form. Why is this happening, and how would I change the code to make the login form and logout button work together as expected?

<?php

if (isset($_POST['log_out'])) { // If the page was reloaded as a result of the user having clicked the logout button,
session_destroy(); // kill session.
}

session_start(); // Start a new session with

$_SESSION['user'] = 'foo'; // a static username, and
$_SESSION['pass'] = 'bar'; // a static password.
// If I insert $_SESSION['logged_in'] = 'false'; here to start things off, a blank alert box will be returned no matter from where on the page I alert() the value with JS. On the other hand, without this line the same alert will return "1" both here and further down in the script.

if (($_POST['username'] == $_SESSION['user']) && ($_POST['password'] == $_SESSION['pass'])) { // If the username and password filled in before the page reload match the static ones,
    $_SESSION['logged_in'] = true; // the user is logged in.
} else { // If there is no match ...
    echo 'Wrong password/username.';
}

include("head.php"); // HTML snippet with everything from the DOCTYPE to the opening BODY tag.

?>
            <div> // HTML interlude ...
<?php

// If the user is logged in, print out a logout button in HTML at the top of the page:

if (isset($_SESSION['logged_in']) && ($_SESSION['logged_in'] == true)) {
    echo '          <form action="index.php" method="post">';
    echo '            <input type="submit" name="log_out" value="Log out">';
    echo '          </form>';
}

?>
                <p>HTML interlude ...</p>

<?php

// If the user is not logged in, print out a login form in HTML at the bottom of the page:

if ($_SESSION['logged_in'] != true) {

    echo '            <form action="index.php" method="post">';
    echo '              <label for="username">Username</label><br>';
    echo '              <input type="text" name="username" id="username"><br>';
    echo '              <label for="password">Password</label><br>';
    echo '              <input type="text" name="password" id="password"><br>';
    echo '              <input type="submit" name="submit" id="submit" value="Log in">';
    echo '            </form>';

}

?>
            </div>
<?php include("footer.php"); ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:24:45+00:00Added an answer on June 3, 2026 at 12:24 am
    if (($_POST['username'] == $_SESSION['user']) && ($_POST['password'] == $_SESSION['pass'])) { the static ones,
        $_SESSION['logged_in'] = true; // the user is logged in.
    } else { 
        echo 'Wrong password/username.';
    }
    

    This part will show “Wrong password/username” when the page loads the first time, because the condition will be false if nothing comes from $_POST['username'].

    Add the condition isset($_POST['username']) && isset($_POST['password']) to both options. Like so:

    if(isset($_POST['username']) && isset($_POST['password'])){
        if (($_POST['username'] == $_SESSION['user']) && ($_POST['password'] == $_SESSION['pass'])) { the static ones,
            $_SESSION['logged_in'] = true; // the user is logged in.
        } else { 
            echo 'Wrong password/username.';
        }
    }
    

    This way nothing of this will be evaluated the first time the page loads, but only when credentials have been posted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning C++ (coming from Java ) and am having some serious
Just started learning PySide and is having problem with QTimer I have this #!/usr/bin/python
I have just started learning MySQL and am having trouble extracting matching flights from
I just started learning PHP and I have some questions about the parameters. Consider
I just started learning how to use SwiftMailer and I am having trouble sending
I'm just getting started with learning jQuery, and I'm having a small CSS issue.
Just started learning Php on my own, and got stuck at the beginning. This
I just started learning C but I don't understand this part of the code.
I haven't officially started learning PHP, just skimming through a couple tutorials and I
I have just started learning GWT. My question is pretty simple: If I create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.