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

  • SEARCH
  • Home
  • 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 7399457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:03:47+00:00 2026-05-29T04:03:47+00:00

I use PHP code below for login without using database. It redirects to home.php

  • 0

I use PHP code below for login without using database.
It redirects to home.php in two cases:
1. When I input correct username and password
2. When I input any character/s as username (except real username) and leave password textbox blank.

In other cases, except the second, code works properly.
How to make the code work properly in the second case?

<?php
 session_start();

$userinfo = array(
                'user1'=>'pass1',
                );

if(isset($_GET['logout'])) {
    $_SESSION['username'] = '';
    header('Location:  ' . $_SERVER['PHP_SELF']);
}

if(isset($_POST['username'])) {
    if($userinfo[$_POST['username']] == $_POST['password']) {
        $_SESSION['username'] = $_POST['username'];

        header('Location: home.php');
    }else {
        header('Location: login.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-05-29T04:03:48+00:00Added an answer on May 29, 2026 at 4:03 am

    You could change

    if($userinfo[$_POST['username']] == $_POST['password']) {
    

    to

    if(isset($userinfo[$_POST['username']]) && $userinfo[$_POST['username']] == $_POST['password']) {
    

    The reason it is failing is because if the username entered is not in your array, $userinfo[‘idontexist’] returns null, which evaluates to an empty string, so you are ultimately comparing null == '' which is true.

    With the modified code, you are first checking to make sure the username entered actually exists in your array. If it does not, the password check is never run and they are redirected to the login page.

    You could alternatively use === to compare the two, but a new vulnerability could be introduced where someone edits the form and removes the password field so that $_POST['password'] becomes null and then entering a non existent user would end up comparing null to null which would allow them to log in. Use the isset check to make sure the username exists in the array, then compare the password.

    Just for the sake of security, you may want to check to see if the password was empty and skip the password check and immediately return to the login page.

    EDIT:
    To allow the 2 cases you want, try:

    if ((isset($userinfo[$_POST['username']]) && $userinfo[$_POST['username']] == $_POST['password']) || (!isset($userinfo[$_POST['username']]) && strlen($_POST['username']) > 0 && $_POST['password'] === '') {

    This checks if the username is in the array, if so make sure the password matches, OR check that the username is NOT in the array, and make sure the password is empty.

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

Sidebar

Related Questions

When I use a conditional statement targeting IE6 and below with some PHP code
I'm reworking some PHP code to use PDO for the database access, but I'm
I have a problem in php code inserting values into database (I use PHPMyAdmin).
Can I use the PHP code below if the PDF file is not on
I use PHP to generate XML files. I have use some code below to
I'm looking for good/working/simple to use PHP code for parsing raw email into parts.
Somebody said that when your PHP code and application use global variables then it
I need help to use jConfirm with this existing code (php & Jquery &
I'm a PHP developer and now I use Notepad++ for code editing, but lately
In my code, I use JavaScript for UI and PHP for back end. I

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.