Jump to content

PHP code stopped working


ronlaboa
Go to solution Solved by benanamen,

Recommended Posts

hello 

 

I am new to php and know a little HTML & CSS

 

I have copied some code from a tutorial just to test a home server I have setup.  the code was working and some of it still does but a part of it doesnt work and leaves me with a blank screen.

 

the part that doesnt seem to work is the else part of the statement.

 

the code is for a very basic login page.

 

here is the HTML for my index page

 

<!DOCTYPE html>


    <head>
        <title>Login Page</title>


    </head>
    
    <body>






        <!-- Output error message if any -->
        <?php echo $error; ?>
        
        <!-- form for login -->
        <form method="post" action="/php/login.php">
            <label for="username">Username:</label><br/>
            <input type="text" name="username" id="username"><br/>
            <label for="password">Password:</label><br/>
            <input type="password" name="password" id="password"><br/>
            <input type="submit" value="Log In!">
        </form>
    </body>
</html>

and the code for my php login page

 

<?php


    // Start the session
    session_start();


    // Defines username and password. Retrieve however you like,
    $username = "user";
    $password = "password";


    // Error message
    $error = "";


    // Checks to see if the user is already logged in. If so, refirect to correct page.
    if (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] == true) {
        $error = "success";
        header('Location: success.php');
    } 
        
    // Checks to see if the username and password have been entered.
    // If so and are equal to the username and password defined above, log them in.
    if (isset($_POST['username']) && isset($_POST['password'])) {
        if ($_POST['username'] == $username && $_POST['password'] == $password) {
            $_SESSION['loggedIn'] = true;
            header('Location: success.php');
        } else {
            $_SESSION['loggedIn'] = false;
            $error = "Invalid username and password!";
        }
    }
?>

the login page works when I type the correct user name and password and takes me to the success page/php file

which is

 

<?php
    // Start the session
    ob_start();
    session_start();


    // Check to see if actually logged in. If not, redirect to login page
    if (!isset($_SESSION['loggedIn']) || $_SESSION['loggedIn'] == false) {
        header("Location: index.html");
    }
?>


<h1>Logged In!</h1>
<form method="post" action="logout.php">
    <input type="submit" value="Logout">
</form>

but if i enter and incorrect username/password it redirects me to the login.php file but its blank.

 

it did work at first but now nothing.

 

as i said i am very new to php so any help would be very much appreciated 

 

thanks

 

matthew

 

Link to comment
Share on other sites

  • Solution

Of course it's blank. All your doing is setting $error. The script is done by the time you get to this point. Think this through, I am sure you can figure out what needs to be changed.

 

 

        } else {
            $_SESSION['loggedIn'] = false;
            $error = "Invalid username and password!";
        }

 

 

FYI: This is no kind of logging in code you should be using.

Edited by benanamen
Link to comment
Share on other sites

ah right ok so i need to display msg stating invalid login and link back to my index page 

thanks

 

i think thats right anyway,

 

the login.php and index.html were orignally one file called index.php

 

I just tried to separate them to try to link to external php file from my html file 

Edited by ronlaboa
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.