JimRomeFan
JimRomeFan

Reputation: 413

My PHP is working locally, but not on 1and1 server

I have tested the following script locally on my machine and everything works perfectly the way I want it. However, when I upload my files to my server at 1and1, on the log-in script when I click the "submit" button, it just stays on the log-in screen.

I'm also not sure, but perhapes the problem isn't with sessions, but with use of my header function.

<?php
    session_start();
    require ("login.php");
    include ("header.php");
    include ("subnav.php");

    if ((isset($_SESSION['user'])) && (isset($_SESSION['admin'])))
        header('Location: admin/index.php' );

    if ((isset($_SESSION['user'])) && (!isset($_SESSION['admin'])))
        header('Location: customer/index.php' );

    if ((isset($_GET['logout'])) == 1) {
        session_destroy();
        header('Location: index.php');
    }

    if (isset($_POST['submit'])) 
        if($_POST['username'] == 'jay') {
            $_SESSION['user'] = 'jay';
            $_SESSION['admin'] = 1;
            header('Location: admin/index.php' );
        }
        else if ($_POST['username'] == 'william'){
            $_SESSION['user'] = 'william';
            header('Location: customer/index.php' );
        }
        else {
            header('Location: http://www.google.com' );
        }
?>

    <h2>System Log-In</h2>

    <form action="" method="post">
        <ul id="login">
            <li>
                Username: <br>
                <input type="text" name="username"></li>
            <li>
                Password: <br>
                <input type="password" name="password">
            </li>
            <li>
                <input type="submit" value="Log-In" name ="submit" id="submit">
            </li>
            <li>
                <br><a href=#>Register Here.</a>
            </li>
            <li>
                If you are having problems with the log-in process, please send us an <a href="mailto:[email protected]">e-mail</a>.
            </li>
        </ul>
    </form>


<?php   
    include ("footer.php");
?>

enter image description here

Upvotes: 0

Views: 3070

Answers (2)

Marshmellow1328
Marshmellow1328

Reputation: 1265

Put curly braces at this point in the code to properly complete the block of code:

if( isset( $_POST['submit'] ) )

In general, it is best to always use curly braces for all if statements even if they are one line. This helps to prevent confusion problems like this.

Upvotes: 1

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

It may or may not help but there are a few things you should get in order:

  1. When using the Location: header you should always terminate the script using die(). Otherwise the rest of the script will keep running: header('Location: ...'); die;

  2. Also related to the Location: header, you really should put a forward slash in-front of the path. E.g. Location: /index.php.

  3. I hope the code is for practice. Giving a user admin status simply based on the value of a POST variable is not exactly very secure.

Upvotes: 0

Related Questions