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 6845695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:31:56+00:00 2026-05-27T00:31:56+00:00

I was looking for a simple PHP script + form that routed users to

  • 0

I was looking for a simple PHP script + form that routed users to specific URLs based on the username and password entered.

Code at the top of the page:

<?php 
session_start();
$data=array("user1"=>array("url"=>"file1.php","password"=>"pass1"),
"user2"=>array("url"=>"file2.php","password"=>"pass2"));

if(isset($_POST['username']) && isset($_POST['password'])) {
    if($data[$_POST['username']]['password'] == $_POST['password']) {
        $_SESSION['username'] = $_POST['username'] . " " . $_POST['password'];
        header('Location: ' . $data[$_POST['username']]['url']);

        login('<p class="alert">Incorrect username or password.</p> ');
    }
} else {
    login();
}
?><?php
function login($response='Please enter your username and password.') {
?>

This is in the body:

<p><?=$response?></p>
<form action="" method="post">
    <table width="400" border="0" cellspacing="0" cellpadding="4">
        <tr>
            <td width="90"><label class="loginform" for="username">Username:</label></td>
            <td width="294"><input name="username" type="text" /></td>
        </tr>
        <tr>
            <td><label for="password">Password:</label></td>
            <td><input name="password" type="password"></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" value="Login" /></td>
        </tr>
    </table>
</form>
<?php } ?>

This code works properly, but if a user enters an incorrect password, it redirects to a blank page (specifically, it only loads the page after the last:

<?php } ?>

I understand this code is not written well at all, so that is the first problem. I’d really love to see a well-written version (just to learn and study it). But if that’s not possible, how would I correct this code so an incorrect password doesn’t break the page?

It’s odd, because if you enter nothing (just hit Submit), it says “Incorrect username or password”, and if you just enter an incorrect username with no password, same thing. But with a username + wrong password, or only wrong password, it goes to the blank page.

Thanks in advance!

  • 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-27T00:31:56+00:00Added an answer on May 27, 2026 at 12:31 am

    Another lesson why indentation is important. Have a look:

    if(isset($_POST['username']) && isset($_POST['password'])) 
    {
        if($data[$_POST['username']]['password'] == $_POST['password']) 
        {
            $_SESSION['username'] = $_POST['username'] . " " . $_POST['password'];
            header('Location: ' . $data[$_POST['username']]['url']);
    
            login('<p class="alert">Incorrect username or password.</p> ');
        }
    } else {
        login();
    }
    

    Now you see. What happens if username and password are POST’ed, but not correct? Nothing, it’ll exit both if-blocks.

    This will do:

    if(isset($_POST['username']) && isset($_POST['password'])) 
    {
        if($data[$_POST['username']]['password'] == $_POST['password']) 
        {
            $_SESSION['username'] = $_POST['username'] . " " . $_POST['password'];
            header('Location: ' . $data[$_POST['username']]['url']);
            exit();
        }
        else
        {
            login('<p class="alert">Incorrect username or password.</p> ');
        }
    } else {
        login();
    }
    

    Or, even better imho:

    $warning = '';
    
    if(isset($_POST['username']) && isset($_POST['password'])) 
    {
        if($data[$_POST['username']]['password'] == $_POST['password']) 
        {
            $_SESSION['username'] = $_POST['username'] . " " . $_POST['password'];
            header('Location: ' . $data[$_POST['username']]['url']);
            exit();
        }
        else
        {
            $warning = '<p class="alert">Incorrect username or password.</p> ';
        }
    }
    
    login($warning);
    

    And I hope I don’t have to tell you that this is no protection whatsoever, unless you compare $_SESSION['username'] on every page after logging in. 🙂

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

Sidebar

Related Questions

I am looking to create a simple php script that based on the URI,
I'm looking for good/working/simple to use PHP code for parsing raw email into parts.
I'm looking for Ruby's Active record for PHP. Something that is so simple that
Im looking for a simple bit of code that will let me add the
I am looking for a simple script that I can use to send some
I looking for your help please I use simple php code like: $file =
I am looking for a PHP script that will simply display xxx subscribers who
I am looking for a really simple PHP image gallery system that allows file
I am looking for a form class that: Is standalone/doesnt need a framework to
I am looking for some help. I have found this script that sort of

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.