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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:21:14+00:00 2026-06-08T02:21:14+00:00

My code has two forms that create a text field for both username and

  • 0

My code has two forms that create a text field for both username and password. The php script takes the form information and sends it to the server for it to decide if the strings from the form data correspond with the username and password I want. I also have a logout button form that goes to a logout.php script which logs the user out. If I click the logout button it sends the user to my login.php script and ends the session. I can’t get users to login to the website and I cant get users to go to the login.php page if I go to the site that has the logout.php. Here is my code:

Login php Code:

   <?php
               session_start();
               if($_SESSION['login'] == true)
               {
                header("Location:index.php");
               }
               else
               {
               if($_POST['username'] == 'username')
               {
                   if($_POST['pass'] == 'password')
                   {

                      $_SESSION['login'] = true;
                      header('Location:index.php');

                    }
               }
               }
       ?>

Login Form:

        <form action="login.php" method="post">
        Username: <input type="text" name="username" /> </br>
        Password: <input type="password" name="pass" /> 
        <input type="submit" value="submit"/>
        </form>

Logout PHP code:

    <?php 
     session_start();
     session_destroy();
     header('Location:index.php');
     ?>

index php code:

    <?php
      session_start();
      if($_SESSION['login'] =! true || $_SESSION['login'] == "")
      {
          header('Location:login.php');
      }
    ?>

index form code:

         <form action="logout.php" method="post">
         <input type="submit" name="logout" value="logout" />
         </form>

The PHP code is listed above the html code before anything is displayed thus why some form actions call back the page again. Thank you

  • 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-08T02:21:16+00:00Added an answer on June 8, 2026 at 2:21 am

    Your main problem is the incorrect operator =! in index.php — the correct use is != for “does not equal”. See more: http://php.net/manual/en/language.operators.comparison.php

    You can clean up your login.php file a bit:

    login.php

    session_start();
    $redirect = false;
    if($_SESSION['login'] == true) {
        $redirect = true;
    } elseif (
        isset($_POST['username']) && 
        $_POST['username'] == 'username' && 
        $_POST['pass'] == 'password'
    ){
        $_SESSION['login'] = true;
        $redirect = true;
    }
    if ($redirect) {
        header("location:http://www.mydomain.com/index.php");
        die();
    }
    

    Your login form is fine, except where you use : for properties. All HTML element properties are name="value" — note the equal sign. So your form:

    <form action="login.php" method="post">
    

    You have it correct in your logout form.

    One of your goals is to reduce the amount of code you use. So if you’re going to do the same thing many times, you should try to isolate that piece of code as a class, a function, or simply in another file that you include. To that end, you could put your check for login in a separate file:

    security.php

    session_start();
    if(!isset($_SESSION['login']) || $_SESSION['login'] !== true) {
        header('location:http://www.mydomain.com/login.php');
        die();
    }
    

    … then use require_once to pull that in on any page where you require the user to be logged in:

    index.php

    require_once('security.php');
    // the rest of your index.php code here
    

    Documentation

    • PHP comparison operators: http://php.net/manual/en/language.operators.comparison.php
    • require_once – http://php.net/manual/en/function.require-once.php
    • Some basic tips for code reuse – http://www.techrepublic.com/article/10-tips-for-php-scripts-reuse-code-with-the-include-and-require-functions/5077715
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple PHP script with a form that has two select fields,
In the last two days, ive run across code that has php echo'd variables
I have a chart (code to replicate will be below) that has two lines
So I have a form that has 4 inputs, 2 text, 2 hidden. I've
In a .net project, create two forms {Form1, Form2} and on each form create
I'm trying to create an Eclipse Form that has three foldable sections, one fixed
I have created a dynamic form using jQuery that has two sections, once the
The following VBA 6 in Excel 2000 code Resides in a form that has
This question has two parts. Part 1. Yesterday I had some code which would
Has Java always maintained source-code backward compatibility during its development? More precisely: given two

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.